Fix svace issue
[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 <sqlite3.h>
29 #include <unistd.h>
30
31 #include <tcore.h>
32 #include <server.h>
33 #include <plugin.h>
34 #include <storage.h>
35
36 #ifndef PLUGIN_VERSION
37 #define PLUGIN_VERSION 1
38 #endif
39
40 #define BUSY_WAITING_USEC 50000 /* 0.05 sec */
41 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
42
43
44 static gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
45 {
46         int rv = 0;
47         sqlite3_stmt *stmt = NULL;
48         char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
49
50         GHashTableIter iter;
51         gpointer key, value;
52
53         dbg("update query");
54
55         memset(szQuery, '\0', 1001);
56         strncpy(szQuery, query, 1000);
57
58         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
59         if (rv != SQLITE_OK) {
60                 err("fail to connect to table (%d)", rv);
61                 return FALSE;
62         }
63
64         if (in_param) {
65                 g_hash_table_iter_init(&iter, in_param);
66                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
67                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
68
69                         if (!value || g_strcmp0((const char *)value, "") == 0) {
70                                 dbg("bind null");
71                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
72                         } else {
73                                 dbg("bind value");
74                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
75                                                 SQLITE_STATIC);
76                         }
77
78                         if (rv != SQLITE_OK) {
79                                 dbg("fail to bind data (%d)", rv);
80                                 break;
81                         }
82                 }
83         }
84
85         if (rv != SQLITE_OK) {
86                 sqlite3_finalize(stmt);
87                 return FALSE;
88         }
89
90         rv = sqlite3_step(stmt);
91         dbg("update query executed (%d)", rv);
92         sqlite3_finalize(stmt);
93
94         if (rv != SQLITE_DONE)
95                 return FALSE;
96
97         return TRUE;
98 }
99
100 static int _busy_handler(void *pData, int count)
101 {
102          if (count < BUSY_WAITING_MAX) {
103                 usleep(BUSY_WAITING_USEC);
104                 return 1;
105         }
106
107         dbg("Busy Handler will be returned SQLITE_BUSY error\n");
108         return 0;
109 }
110
111 static void *create_handle(Storage *strg, const char *path)
112 {
113         int rv = 0;
114         sqlite3 *handle = NULL;
115
116         if (path == NULL) {
117                 err("Invalid input param error");
118                 return NULL;
119         }
120
121         rv = sqlite3_open(path, &handle);
122         if (rv != SQLITE_OK) {
123                 err("fail to connect database err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
124                 return NULL;
125         }
126
127         rv = sqlite3_busy_handler(handle, _busy_handler, NULL);
128         if (rv != SQLITE_OK) {
129                 err("fail to register busy handler err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
130                 sqlite3_close(handle);
131                 return NULL;
132         }
133
134         dbg("connected to %s", path);
135         return handle;
136 }
137
138 static gboolean remove_handle(Storage *strg, void *handle)
139 {
140         int rv = 0;
141
142         if (!handle)
143                 return FALSE;
144
145         rv = sqlite3_close(handle);
146         if (rv != SQLITE_OK) {
147                 err("fail to close database err(%d)", rv);
148                 handle = NULL;
149                 return FALSE;
150         }
151
152         dbg("disconnected from database");
153         return TRUE;
154 }
155
156 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
157 {
158         gboolean ret = TRUE;
159
160         dbg("update query");
161
162         ret = __update_query_database(strg, handle, query, in_param);
163
164         return ret;
165 }
166
167 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
168                 gpointer out_param, int out_param_cnt, gboolean in_order)
169 {
170         int rv = 0, local_index = 0, outter_index = 0;
171         sqlite3_stmt *stmt = NULL;
172         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
173
174         GHashTableIter iter;
175         gpointer key, value;
176
177         dbg("read query");
178
179         memset(szQuery, '\0', 5001);
180         strncpy(szQuery, query, 5000);
181
182         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
183         if (rv != SQLITE_OK) {
184                 err("fail to connect to table (%d)", rv);
185                 return FALSE;
186         }
187
188         if (in_param) {
189                 g_hash_table_iter_init(&iter, in_param);
190                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
191                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
192
193                         if (!value || g_strcmp0((const char *)value, "") == 0) {
194                                 dbg("bind null");
195                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
196                         } else {
197                                 dbg("bind value");
198                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
199                                                 SQLITE_STATIC);
200                         }
201
202                         if (rv != SQLITE_OK) {
203                                 dbg("fail to bind data (%d)", rv);
204                                 break;
205                         }
206                 }
207         }
208
209         if (rv != SQLITE_OK) {
210                 sqlite3_finalize(stmt);
211                 return FALSE;
212         }
213
214         rv = sqlite3_step(stmt);
215         dbg("read query executed (%d), in_order (%d)", rv, in_order);
216
217         while (rv == SQLITE_ROW) {
218                 GHashTable *out_param_data;
219
220                 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
221
222                 for (local_index = 0; local_index < out_param_cnt; local_index++) {
223                         char tmp_key[32];
224                         const unsigned char *tmp;
225                         tmp = sqlite3_column_text(stmt, local_index);
226                         snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
227                         g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
228                 }
229
230                 if (in_order) {
231                         GSList **temp = out_param;
232                         *temp = g_slist_append(*temp, out_param_data);
233                 } else {
234                         char tmp_key_outter[32];
235                         snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
236                         g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
237                 }
238                 outter_index++;
239                 rv = sqlite3_step(stmt);
240         }
241
242         sqlite3_finalize(stmt);
243         return TRUE;
244 }
245
246 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
247                 GHashTable **out_param, int out_param_cnt)
248 {
249         GHashTable *out_hash_table;
250
251         if (out_param == NULL)
252                 return FALSE;
253
254         out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
255                         g_free, (GDestroyNotify)g_hash_table_destroy);
256
257         if (_read_query_database_internal(strg,
258                         handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
259                 g_hash_table_destroy(out_hash_table);
260                 return FALSE;
261         }
262
263         *out_param = out_hash_table;
264
265         return TRUE;
266 }
267
268 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
269                 GSList **out_param, int out_param_cnt)
270 {
271         if (_read_query_database_internal(strg,
272                         handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
273                 return FALSE;
274
275         return TRUE;
276 }
277
278 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
279 {
280         int rv = 0;
281         sqlite3_stmt *stmt = NULL;
282         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
283
284         GHashTableIter iter;
285         gpointer key, value;
286         dbg("insert query");
287
288         memset(szQuery, '\0', 5001);
289         strncpy(szQuery, query, 5000);
290
291         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
292         if (rv != SQLITE_OK) {
293                 err("fail to connect to table (%d)", rv);
294                 return FALSE;
295         }
296
297         if (in_param) {
298                 g_hash_table_iter_init(&iter, in_param);
299                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
300                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
301
302                         if (!value || g_strcmp0((const char *)value, "") == 0) {
303                                 dbg("bind null");
304                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
305                         } else {
306                                 dbg("bind value");
307                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
308                                                 SQLITE_STATIC);
309                         }
310
311                         if (rv != SQLITE_OK) {
312                                 dbg("fail to bind data (%d)", rv);
313                                 break;
314                         }
315                 }
316         }
317
318         if (rv != SQLITE_OK) {
319                 sqlite3_finalize(stmt);
320                 return FALSE;
321         }
322
323         rv = sqlite3_step(stmt);
324         dbg("insert query executed (%d)", rv);
325         sqlite3_finalize(stmt);
326
327         if (rv != SQLITE_DONE)
328                 return FALSE;
329
330         return TRUE;
331 }
332
333 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
334 {
335         gboolean ret = TRUE;
336
337         dbg("remove query");
338
339         ret = __update_query_database(strg, handle, query, in_param);
340
341         return ret;
342 }
343
344 static struct storage_operations ops = {
345         .create_handle = create_handle,
346         .remove_handle = remove_handle,
347         .update_query_database = update_query_database,
348         .read_query_database = read_query_database,
349         .read_query_database_in_order = read_query_database_in_order,
350         .insert_query_database = insert_query_database,
351         .remove_query_database = remove_query_database,
352 };
353
354 static gboolean on_load()
355 {
356         dbg("i'm load!");
357         return TRUE;
358 }
359
360 static gboolean on_init(TcorePlugin *p)
361 {
362         if (!p)
363                 return FALSE;
364
365         tcore_storage_new(p, "database", &ops);
366
367         dbg("finish to initialize database plug-in");
368         return TRUE;
369 }
370
371 static void on_unload(TcorePlugin *p)
372 {
373         Storage *strg;
374
375         if (!p)
376                 return;
377
378         dbg("i'm unload!");
379
380         strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
381         if (!strg)
382                 return;
383
384         tcore_storage_free(strg);
385         return;
386
387 }
388
389 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
390         .name = "DATABASE",
391         .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
392         .version = PLUGIN_VERSION,
393         .load = on_load,
394         .init = on_init,
395         .unload = on_unload
396 };