[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_profile_helper.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include "contacts.h"
20 #include "ctsvc_internal.h"
21 #include "ctsvc_db_schema.h"
22 #include "ctsvc_db_sqlite.h"
23 #include "ctsvc_db_init.h"
24 #include "ctsvc_db_query.h"
25 #include "ctsvc_db_plugin_profile_helper.h"
26 #include "ctsvc_record.h"
27 #include "ctsvc_notification.h"
28
29
30 int ctsvc_db_profile_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record, int start_count)
31 {
32         int ret;
33         char *temp;
34         ctsvc_profile_s *profile;
35
36         ret = contacts_record_create(_contacts_profile._uri, (contacts_record_h*)&profile);
37         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
38
39         profile->id = ctsvc_stmt_get_int(stmt, start_count++);
40         profile->contact_id = ctsvc_stmt_get_int(stmt, start_count++);
41         start_count++;
42         start_count++;
43         start_count++;
44         temp = ctsvc_stmt_get_text(stmt, start_count++);
45         profile->uid = SAFE_STRDUP(temp);
46         temp = ctsvc_stmt_get_text(stmt, start_count++);
47         profile->text = SAFE_STRDUP(temp);
48         profile->order = ctsvc_stmt_get_int(stmt, start_count++);
49         temp = ctsvc_stmt_get_text(stmt, start_count++);
50         profile->service_operation = SAFE_STRDUP(temp);
51         temp = ctsvc_stmt_get_text(stmt, start_count++);
52         profile->mime = SAFE_STRDUP(temp);
53         temp = ctsvc_stmt_get_text(stmt, start_count++);
54         profile->app_id = SAFE_STRDUP(temp);
55         temp = ctsvc_stmt_get_text(stmt, start_count++);
56         profile->uri = SAFE_STRDUP(temp);
57         temp = ctsvc_stmt_get_text(stmt, start_count++);
58         profile->category = SAFE_STRDUP(temp);
59         temp = ctsvc_stmt_get_text(stmt, start_count++);
60         profile->extra_data = SAFE_STRDUP(temp);
61
62         *record = (contacts_record_h)profile;
63         return CONTACTS_ERROR_NONE;
64 }
65
66 static inline int __ctsvc_profile_bind_stmt(cts_stmt stmt, ctsvc_profile_s *profile, int start_cnt)
67 {
68         if (profile->uid)
69                 ctsvc_stmt_bind_text(stmt, start_cnt, profile->uid);
70         if (profile->text)
71                 ctsvc_stmt_bind_text(stmt, start_cnt+1, profile->text);
72         ctsvc_stmt_bind_int(stmt, start_cnt+2, profile->order);
73         if (profile->service_operation)
74                 ctsvc_stmt_bind_text(stmt, start_cnt+3, profile->service_operation);
75         if (profile->mime)
76                 ctsvc_stmt_bind_text(stmt, start_cnt+4, profile->mime);
77         if (profile->app_id)
78                 ctsvc_stmt_bind_text(stmt, start_cnt+5, profile->app_id);
79         if (profile->uri)
80                 ctsvc_stmt_bind_text(stmt, start_cnt+6, profile->uri);
81         if (profile->category)
82                 ctsvc_stmt_bind_text(stmt, start_cnt+7, profile->category);
83         if (profile->extra_data)
84                 ctsvc_stmt_bind_text(stmt, start_cnt+8, profile->extra_data);
85         return CONTACTS_ERROR_NONE;
86 }
87
88 int ctsvc_db_profile_insert(contacts_record_h record, int contact_id, bool is_my_profile, int *id)
89 {
90         int ret;
91         cts_stmt stmt = NULL;
92         char query[CTS_SQL_MAX_LEN] = {0};
93         ctsvc_profile_s *profile = (ctsvc_profile_s*)record;
94
95         RETV_IF(NULL == profile->text, CONTACTS_ERROR_NONE);
96         RETVM_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
97                         "contact_id(%d) is mandatory field to insert profile record ", profile->contact_id);
98         RETVM_IF(0 < profile->id, CONTACTS_ERROR_INVALID_PARAMETER,
99                         "id(%d), This record is already inserted", profile->id);
100
101         snprintf(query, sizeof(query),
102                         "INSERT INTO "CTS_TABLE_DATA"(contact_id, is_my_profile, datatype, data3, data4, data5, "
103                         "data6, data7, data8, data9, data10, data11) "
104                         "VALUES(%d, %d, %d, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
105                         contact_id, is_my_profile, CONTACTS_DATA_TYPE_PROFILE);
106
107         ret = ctsvc_query_prepare(query, &stmt);
108         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
109
110         __ctsvc_profile_bind_stmt(stmt, profile, 1);
111
112         ret = ctsvc_stmt_step(stmt);
113         if (CONTACTS_ERROR_NONE != ret) {
114                 /* LCOV_EXCL_START */
115                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
116                 ctsvc_stmt_finalize(stmt);
117                 return ret;
118                 /* LCOV_EXCL_STOP */
119         }
120
121         /* profile->id = ctsvc_db_get_last_insert_id(); */
122         if (id)
123                 *id = ctsvc_db_get_last_insert_id();
124         ctsvc_stmt_finalize(stmt);
125
126         if (false == is_my_profile)
127                 ctsvc_set_profile_noti();
128
129         return CONTACTS_ERROR_NONE;
130
131 }
132
133 int ctsvc_db_profile_update(contacts_record_h record, bool is_my_profile)
134 {
135         int id;
136         int ret = CONTACTS_ERROR_NONE;
137         char *set = NULL;
138         GSList *bind_text = NULL;
139         GSList *cursor = NULL;
140         ctsvc_profile_s *profile = (ctsvc_profile_s*)record;
141         char query[CTS_SQL_MAX_LEN] = {0};
142
143         RETVM_IF(0 == profile->id, CONTACTS_ERROR_INVALID_PARAMETER, "profile of contact has no ID.");
144
145         snprintf(query, sizeof(query),
146                         "SELECT id FROM "CTS_TABLE_DATA" WHERE id = %d", profile->id);
147         ret = ctsvc_query_get_first_int_result(query, &id);
148         RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
149
150         RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (profile->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
151
152         do {
153                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
154                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_DATA, profile->id))) break;
155                 if (false == is_my_profile)
156                         ctsvc_set_profile_noti();
157         } while (0);
158
159         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
160         free(set);
161
162         if (bind_text) {
163                 for (cursor = bind_text; cursor; cursor = cursor->next) {
164                         free(cursor->data);
165                         cursor->data = NULL;
166                 }
167                 g_slist_free(bind_text);
168         }
169
170         return ret;
171 }
172
173
174 int ctsvc_db_profile_delete(int id, bool is_my_profile)
175 {
176         int ret;
177         char query[CTS_SQL_MIN_LEN] = {0};
178
179         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_DATA" WHERE id = %d AND datatype = %d",
180                         id, CONTACTS_DATA_TYPE_PROFILE);
181
182         ret = ctsvc_query_exec(query);
183         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
184
185         if (false == is_my_profile)
186                 ctsvc_set_profile_noti();
187
188         return CONTACTS_ERROR_NONE;
189 }
190