Fix svace issue (WGID 309818)
[platform/core/telephony/tel-plugin-database.git] / src / database_main.c
1 /*
2  * tel-plugin-database
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongHoo Park <donghoo.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26
27 #include <glib.h>
28 #include <db-util.h>
29
30 #include <tcore.h>
31 #include <server.h>
32 #include <plugin.h>
33 #include <storage.h>
34
35 #ifndef PLUGIN_VERSION
36 #define PLUGIN_VERSION 1
37 #endif
38
39 static void *create_handle(Storage *strg, const char *path)
40 {
41         int rv = 0;
42         sqlite3 *handle = NULL;
43
44         rv = db_util_open(path, &handle, 0);
45         if (rv != SQLITE_OK) {
46                 err("fail to connect database err(%d)", rv);
47                 return NULL;
48         }
49
50         dbg("connected to %s", path);
51         return handle;
52 }
53
54 static gboolean remove_handle(Storage *strg, void *handle)
55 {
56         if (!handle)
57                 return FALSE;
58
59         db_util_close(handle);
60
61         dbg("disconnected from database");
62         return TRUE;
63 }
64
65 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
66 {
67         int rv = 0;
68         sqlite3_stmt *stmt = NULL;
69         char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
70
71         GHashTableIter iter;
72         gpointer key, value;
73
74         dbg("update query");
75
76         memset(szQuery, '\0', 1001);
77         strncpy(szQuery, query, 1000);
78
79         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
80         if (rv != SQLITE_OK) {
81                 err("fail to connect to table (%d)", rv);
82                 return FALSE;
83         }
84
85         if (in_param) {
86                 g_hash_table_iter_init(&iter, in_param);
87                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
88                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
89
90                         if (!value || g_strcmp0((const char *)value, "") == 0) {
91                                 dbg("bind null");
92                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
93                         } else {
94                                 dbg("bind value");
95                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
96                                                 SQLITE_STATIC);
97                         }
98
99                         if (rv != SQLITE_OK) {
100                                 dbg("fail to bind data (%d)", rv);
101                                 break;
102                         }
103                 }
104         }
105
106         if (rv != SQLITE_OK) {
107                 sqlite3_finalize(stmt);
108                 return FALSE;
109         }
110
111         rv = sqlite3_step(stmt);
112         dbg("update query executed (%d)", rv);
113         sqlite3_finalize(stmt);
114
115         if (rv != SQLITE_DONE)
116                 return FALSE;
117
118         return TRUE;
119 }
120
121 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
122                 gpointer out_param, int out_param_cnt, gboolean in_order)
123 {
124         int rv = 0, local_index = 0, outter_index = 0;
125         sqlite3_stmt *stmt = NULL;
126         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
127
128         GHashTableIter iter;
129         gpointer key, value;
130
131         dbg("read query");
132
133         memset(szQuery, '\0', 5001);
134         strncpy(szQuery, query, 5000);
135
136         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
137         if (rv != SQLITE_OK) {
138                 err("fail to connect to table (%d)", rv);
139                 return FALSE;
140         }
141
142         if (in_param) {
143                 g_hash_table_iter_init(&iter, in_param);
144                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
145                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
146
147                         if (!value || g_strcmp0((const char *)value, "") == 0) {
148                                 dbg("bind null");
149                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
150                         } else {
151                                 dbg("bind value");
152                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
153                                                 SQLITE_STATIC);
154                         }
155
156                         if (rv != SQLITE_OK) {
157                                 dbg("fail to bind data (%d)", rv);
158                                 break;
159                         }
160                 }
161         }
162
163         if (rv != SQLITE_OK) {
164                 sqlite3_finalize(stmt);
165                 return FALSE;
166         }
167
168         rv = sqlite3_step(stmt);
169         dbg("read query executed (%d), in_order (%d)", rv, in_order);
170
171         while (rv == SQLITE_ROW) {
172                 GHashTable *out_param_data;
173
174                 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
175
176                 for (local_index = 0; local_index < out_param_cnt; local_index++) {
177                         char tmp_key[10];
178                         const unsigned char *tmp;
179                         tmp = sqlite3_column_text(stmt, local_index);
180                         snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
181                         g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
182                 }
183
184                 if (in_order) {
185                         GSList **temp = out_param;
186                         *temp = g_slist_append(*temp, out_param_data);
187                 } else {
188                         char tmp_key_outter[10];
189                         snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
190                         g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
191                 }
192                 outter_index++;
193                 rv = sqlite3_step(stmt);
194         }
195
196         sqlite3_finalize(stmt);
197         return TRUE;
198 }
199
200 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
201                 GHashTable **out_param, int out_param_cnt)
202 {
203         GHashTable *out_hash_table;
204
205         if (out_param == NULL)
206                 return FALSE;
207
208         out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
209                         g_free, (GDestroyNotify)g_hash_table_destroy);
210
211         if (_read_query_database_internal(strg,
212                         handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
213                 g_hash_table_destroy(out_hash_table);
214                 return FALSE;
215         }
216
217         *out_param = out_hash_table;
218
219         return TRUE;
220 }
221
222 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
223                 GSList **out_param, int out_param_cnt)
224 {
225         if (_read_query_database_internal(strg,
226                         handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
227                 return FALSE;
228
229         return TRUE;
230 }
231
232 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
233 {
234         int rv = 0;
235         sqlite3_stmt *stmt = NULL;
236         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
237
238         GHashTableIter iter;
239         gpointer key, value;
240         dbg("insert query");
241
242         memset(szQuery, '\0', 5001);
243         strncpy(szQuery, query, 5000);
244
245         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
246         if (rv != SQLITE_OK) {
247                 err("fail to connect to table (%d)", rv);
248                 return FALSE;
249         }
250
251         if (in_param) {
252                 g_hash_table_iter_init(&iter, in_param);
253                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
254                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
255
256                         if (!value || g_strcmp0((const char *)value, "") == 0) {
257                                 dbg("bind null");
258                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
259                         } else {
260                                 dbg("bind value");
261                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
262                                                 SQLITE_STATIC);
263                         }
264
265                         if (rv != SQLITE_OK) {
266                                 dbg("fail to bind data (%d)", rv);
267                                 break;
268                         }
269                 }
270         }
271
272         if (rv != SQLITE_OK) {
273                 sqlite3_finalize(stmt);
274                 return FALSE;
275         }
276
277         rv = sqlite3_step(stmt);
278         dbg("insert query executed (%d)", rv);
279         sqlite3_finalize(stmt);
280
281         if (rv != SQLITE_DONE)
282                 return FALSE;
283
284         return TRUE;
285 }
286
287 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
288 {
289         int rv = 0;
290         sqlite3_stmt *stmt = NULL;
291         char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
292
293         GHashTableIter iter;
294         gpointer key, value;
295         dbg("remove query");
296
297         memset(szQuery, '\0', 1001);
298         strncpy(szQuery, query, 1000);
299
300         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
301         if (rv != SQLITE_OK) {
302                 err("fail to connect to table (%d)", rv);
303                 return FALSE;
304         }
305
306         if (in_param) {
307                 g_hash_table_iter_init(&iter, in_param);
308                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
309                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
310
311                         if (!value || g_strcmp0((const char *)value, "") == 0) {
312                                 dbg("bind null");
313                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
314                         } else {
315                                 dbg("bind value");
316                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
317                                                 SQLITE_STATIC);
318                         }
319
320                         if (rv != SQLITE_OK) {
321                                 dbg("fail to bind data (%d)", rv);
322                                 break;
323                         }
324                 }
325         }
326
327         if (rv != SQLITE_OK) {
328                 sqlite3_finalize(stmt);
329                 return FALSE;
330         }
331
332         rv = sqlite3_step(stmt);
333         dbg("remove query executed (%d)", rv);
334         sqlite3_finalize(stmt);
335
336         if (rv != SQLITE_DONE)
337                 return FALSE;
338
339         return TRUE;
340 }
341
342 static struct storage_operations ops = {
343         .create_handle = create_handle,
344         .remove_handle = remove_handle,
345         .update_query_database = update_query_database,
346         .read_query_database = read_query_database,
347         .read_query_database_in_order = read_query_database_in_order,
348         .insert_query_database = insert_query_database,
349         .remove_query_database = remove_query_database,
350 };
351
352 static gboolean on_load()
353 {
354         dbg("i'm load!");
355         return TRUE;
356 }
357
358 static gboolean on_init(TcorePlugin *p)
359 {
360         if (!p)
361                 return FALSE;
362
363         tcore_storage_new(p, "database", &ops);
364
365         dbg("finish to initialize database plug-in");
366         return TRUE;
367 }
368
369 static void on_unload(TcorePlugin *p)
370 {
371         Storage *strg;
372
373         if (!p)
374                 return;
375
376         dbg("i'm unload!");
377
378         strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
379         if (!strg)
380                 return;
381
382         tcore_storage_free(strg);
383         return;
384
385 }
386
387 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
388         .name = "DATABASE",
389         .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
390         .version = PLUGIN_VERSION,
391         .load = on_load,
392         .init = on_init,
393         .unload = on_unload
394 };