[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_extension_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_extension_helper.h"
26 #include "ctsvc_record.h"
27 #include "ctsvc_notification.h"
28
29
30 int ctsvc_db_extension_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record, int start_count)
31 {
32         int ret;
33         char *temp;
34         ctsvc_extension_s *extension;
35
36         ret = contacts_record_create(_contacts_extension._uri, (contacts_record_h*)&extension);
37         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
38
39         extension->id = ctsvc_stmt_get_int(stmt, start_count++);
40         extension->contact_id = ctsvc_stmt_get_int(stmt, start_count++);
41         start_count++;
42         extension->data1 = ctsvc_stmt_get_int(stmt, start_count++);
43         temp = ctsvc_stmt_get_text(stmt, start_count++);
44         extension->data2 = SAFE_STRDUP(temp);
45         temp = ctsvc_stmt_get_text(stmt, start_count++);
46         extension->data3 = SAFE_STRDUP(temp);
47         temp = ctsvc_stmt_get_text(stmt, start_count++);
48         extension->data4 = SAFE_STRDUP(temp);
49         temp = ctsvc_stmt_get_text(stmt, start_count++);
50         extension->data5 = SAFE_STRDUP(temp);
51         temp = ctsvc_stmt_get_text(stmt, start_count++);
52         extension->data6 = SAFE_STRDUP(temp);
53         temp = ctsvc_stmt_get_text(stmt, start_count++);
54         extension->data7 = SAFE_STRDUP(temp);
55         temp = ctsvc_stmt_get_text(stmt, start_count++);
56         extension->data8 = SAFE_STRDUP(temp);
57         temp = ctsvc_stmt_get_text(stmt, start_count++);
58         extension->data9 = SAFE_STRDUP(temp);
59         temp = ctsvc_stmt_get_text(stmt, start_count++);
60         extension->data10 = SAFE_STRDUP(temp);
61         temp = ctsvc_stmt_get_text(stmt, start_count++);
62         extension->data11 = SAFE_STRDUP(temp);
63         temp = ctsvc_stmt_get_text(stmt, start_count++);
64         extension->data12 = SAFE_STRDUP(temp);
65
66         *record = (contacts_record_h)extension;
67         return CONTACTS_ERROR_NONE;
68 }
69
70 static inline int __ctsvc_extension_bind_stmt(cts_stmt stmt, ctsvc_extension_s *extension, int start_cnt)
71 {
72         if (extension->data2)
73                 ctsvc_stmt_bind_text(stmt, start_cnt, extension->data2);
74         if (extension->data3)
75                 ctsvc_stmt_bind_text(stmt, start_cnt+1, extension->data3);
76         if (extension->data4)
77                 ctsvc_stmt_bind_text(stmt, start_cnt+2, extension->data4);
78         if (extension->data5)
79                 ctsvc_stmt_bind_text(stmt, start_cnt+3, extension->data5);
80         if (extension->data6)
81                 ctsvc_stmt_bind_text(stmt, start_cnt+4, extension->data6);
82         if (extension->data7)
83                 ctsvc_stmt_bind_text(stmt, start_cnt+5, extension->data7);
84         if (extension->data8)
85                 ctsvc_stmt_bind_text(stmt, start_cnt+6, extension->data8);
86         if (extension->data9)
87                 ctsvc_stmt_bind_text(stmt, start_cnt+7, extension->data9);
88         if (extension->data10)
89                 ctsvc_stmt_bind_text(stmt, start_cnt+8, extension->data10);
90         if (extension->data11)
91                 ctsvc_stmt_bind_text(stmt, start_cnt+9, extension->data11);
92         if (extension->data12)
93                 ctsvc_stmt_bind_text(stmt, start_cnt+10, extension->data12);
94         return CONTACTS_ERROR_NONE;
95 }
96
97 int ctsvc_db_extension_insert(contacts_record_h record, int contact_id, bool is_my_profile, int *id)
98 {
99         int ret;
100         cts_stmt stmt = NULL;
101         char query[CTS_SQL_MAX_LEN] = {0};
102         ctsvc_extension_s *extension = (ctsvc_extension_s*)record;
103
104         RETVM_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
105                         "contact_id(%d) is mandatory field to insert extension record ", extension->contact_id);
106         RETVM_IF(0 < extension->id, CONTACTS_ERROR_INVALID_PARAMETER,
107                         "id(%d), This record is already inserted", extension->id);
108
109         if (extension->data2 || extension->data3 || extension->data4 || extension->data5
110                         || extension->data6 || extension->data7 || extension->data8 || extension->data9
111                         || extension->data10 || extension->data11 || extension->data12) {
112                 snprintf(query, sizeof(query),
113                                 "INSERT INTO "CTS_TABLE_DATA" (contact_id, is_my_profile, datatype, data1, data2, data3, data4, "
114                                 "data5, data6, data7, data8, data9, data10, data11, data12) "
115                                 "VALUES(%d, %d, %d, %d, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
116                                 contact_id, is_my_profile, CONTACTS_DATA_TYPE_EXTENSION, extension->data1);
117
118                 ret = ctsvc_query_prepare(query, &stmt);
119                 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
120
121                 __ctsvc_extension_bind_stmt(stmt, extension, 1);
122
123                 ret = ctsvc_stmt_step(stmt);
124                 if (CONTACTS_ERROR_NONE != ret) {
125                         /* LCOV_EXCL_START */
126                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
127                         ctsvc_stmt_finalize(stmt);
128                         return ret;
129                         /* LCOV_EXCL_STOP */
130                 }
131
132                 /* extension->id = ctsvc_db_get_last_insert_id(); */
133                 if (id)
134                         *id = ctsvc_db_get_last_insert_id();
135                 ctsvc_stmt_finalize(stmt);
136
137                 if (false == is_my_profile)
138                         ctsvc_set_data_noti();
139         }
140
141         return CONTACTS_ERROR_NONE;
142 }
143
144 int ctsvc_db_extension_update(contacts_record_h record)
145 {
146         int id;
147         int ret = CONTACTS_ERROR_NONE;
148         char *set = NULL;
149         GSList *bind_text = NULL;
150         GSList *cursor = NULL;
151         ctsvc_extension_s *extension = (ctsvc_extension_s*)record;
152         char query[CTS_SQL_MAX_LEN] = {0};
153
154         RETVM_IF(0 == extension->id, CONTACTS_ERROR_INVALID_PARAMETER, "extension of contact has no ID.");
155         RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (extension->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
156
157         snprintf(query, sizeof(query),
158                         "SELECT id FROM "CTS_TABLE_DATA" WHERE id = %d", extension->id);
159         ret = ctsvc_query_get_first_int_result(query, &id);
160         RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
161
162         do {
163                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
164                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_DATA, extension->id))) break;
165         } while (0);
166
167         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
168         free(set);
169
170         if (bind_text) {
171                 for (cursor = bind_text; cursor; cursor = cursor->next) {
172                         free(cursor->data);
173                         cursor->data = NULL;
174                 }
175                 g_slist_free(bind_text);
176         }
177         return ret;
178 }
179
180 int ctsvc_db_extension_delete(int id, bool is_my_profile)
181 {
182         int ret;
183         char query[CTS_SQL_MIN_LEN] = {0};
184
185         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_DATA" WHERE id = %d AND datatype = %d",
186                         id, CONTACTS_DATA_TYPE_EXTENSION);
187
188         ret = ctsvc_query_exec(query);
189         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
190
191         if (false == is_my_profile)
192                 ctsvc_set_data_noti();
193
194         return CONTACTS_ERROR_NONE;
195 }
196