Code sync from tizen_2.4
[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 <plugin.h>
32 #include <storage.h>
33
34 #ifndef PLUGIN_VERSION
35 #define PLUGIN_VERSION 1
36 #endif
37
38 static void *create_handle(Storage *strg, const char *path)
39 {
40         int rv = 0;
41         sqlite3 *handle = NULL;
42
43         rv = db_util_open(path, &handle, 0);
44         if (rv != SQLITE_OK) {
45                 err("fail to connect database err(%d)", rv);
46                 return NULL;
47         }
48
49         dbg("connected to %s", path);
50         return handle;
51 }
52
53 static gboolean remove_handle(Storage *strg, void *handle)
54 {
55         if (!handle)
56                 return FALSE;
57
58         db_util_close(handle);
59
60         dbg("disconnected from database");
61         return TRUE;
62 }
63
64 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
65 {
66         int rv = 0;
67         sqlite3_stmt *stmt = NULL;
68         char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
69
70         GHashTableIter iter;
71         gpointer key, value;
72
73         dbg("update query");
74
75         memset(szQuery, '\0', 1001);
76         strncpy(szQuery, query, 1000);
77
78         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
79         if (rv != SQLITE_OK) {
80                 err("fail to connect to table (%d)", rv);
81                 return FALSE;
82         }
83
84         if (in_param) {
85                 g_hash_table_iter_init(&iter, in_param);
86                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
87                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
88
89                         if (!value || g_strcmp0((const char *)value, "") == 0) {
90                                 dbg("bind null");
91                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
92                         } else {
93                                 dbg("bind value");
94                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
95                                                 SQLITE_STATIC);
96                         }
97
98                         if (rv != SQLITE_OK) {
99                                 dbg("fail to bind data (%d)", rv);
100                                 return FALSE;
101                         }
102                 }
103         }
104
105         rv = sqlite3_step(stmt);
106         dbg("update query executed (%d)", rv);
107         sqlite3_finalize(stmt);
108
109         if (rv != SQLITE_DONE)
110                 return FALSE;
111
112         return TRUE;
113 }
114
115 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
116         GHashTable *out_param, int out_param_cnt)
117 {
118         int rv = 0, local_index = 0, outter_index = 0;
119         sqlite3_stmt *stmt = NULL;
120         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
121
122         GHashTableIter iter;
123         gpointer key, value;
124
125         dbg("read query");
126
127         memset(szQuery, '\0', 5001);
128         strncpy(szQuery, query, 5000);
129
130         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
131         if (rv != SQLITE_OK) {
132                 err("fail to connect to table (%d)", rv);
133                 return FALSE;
134         }
135
136         if (in_param) {
137                 g_hash_table_iter_init(&iter, in_param);
138                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
139                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
140
141                         if (!value || g_strcmp0((const char *)value, "") == 0) {
142                                 dbg("bind null");
143                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
144                         } else {
145                                 dbg("bind value");
146                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
147                                                 SQLITE_STATIC);
148                         }
149
150                         if (rv != SQLITE_OK) {
151                                 dbg("fail to bind data (%d)", rv);
152                                 return FALSE;
153                         }
154                 }
155         }
156
157         rv = sqlite3_step(stmt);
158         dbg("read query executed (%d)", rv);
159
160         while (rv == SQLITE_ROW) {
161
162                 char tmp_key_outter[10];
163                 GHashTable *out_param_data;
164
165                 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
166
167                 for (local_index = 0; local_index < out_param_cnt; local_index++) {
168                         char tmp_key[10];
169                         const unsigned char *tmp;
170                         tmp = sqlite3_column_text(stmt, local_index);
171                         snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
172                         g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
173                 }
174
175                 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
176                 g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
177                 outter_index++;
178                 rv = sqlite3_step(stmt);
179         }
180
181         sqlite3_finalize(stmt);
182         return TRUE;
183 }
184
185 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
186 {
187         int rv = 0;
188         sqlite3_stmt *stmt = NULL;
189         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
190
191         GHashTableIter iter;
192         gpointer key, value;
193         dbg("insert query");
194
195         memset(szQuery, '\0', 5001);
196         strncpy(szQuery, query, 5000);
197
198         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
199         if (rv != SQLITE_OK) {
200                 err("fail to connect to table (%d)", rv);
201                 return FALSE;
202         }
203
204         if (in_param) {
205                 g_hash_table_iter_init(&iter, in_param);
206                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
207                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
208
209                         if (!value || g_strcmp0((const char *)value, "") == 0) {
210                                 dbg("bind null");
211                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
212                         } else {
213                                 dbg("bind value");
214                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
215                                                 SQLITE_STATIC);
216                         }
217
218                         if (rv != SQLITE_OK) {
219                                 dbg("fail to bind data (%d)", rv);
220                                 return FALSE;
221                         }
222                 }
223         }
224
225         rv = sqlite3_step(stmt);
226         dbg("insert query executed (%d)", rv);
227         sqlite3_finalize(stmt);
228
229         if (rv != SQLITE_DONE)
230                 return FALSE;
231
232         return TRUE;
233 }
234
235 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
236 {
237         int rv = 0;
238         sqlite3_stmt *stmt = NULL;
239         char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
240
241         GHashTableIter iter;
242         gpointer key, value;
243         dbg("remove query");
244
245         memset(szQuery, '\0', 1001);
246         strncpy(szQuery, query, 1000);
247
248         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
249         if (rv != SQLITE_OK) {
250                 err("fail to connect to table (%d)", rv);
251                 return FALSE;
252         }
253
254         if (in_param) {
255                 g_hash_table_iter_init(&iter, in_param);
256                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
257                         dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
258
259                         if (!value || g_strcmp0((const char *)value, "") == 0) {
260                                 dbg("bind null");
261                                 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
262                         } else {
263                                 dbg("bind value");
264                                 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
265                                                 SQLITE_STATIC);
266                         }
267
268                         if (rv != SQLITE_OK) {
269                                 dbg("fail to bind data (%d)", rv);
270                                 return FALSE;
271                         }
272                 }
273         }
274
275         rv = sqlite3_step(stmt);
276         dbg("remove query executed (%d)", rv);
277         sqlite3_finalize(stmt);
278
279         if (rv != SQLITE_DONE)
280                 return FALSE;
281
282         return TRUE;
283 }
284
285 static struct storage_operations ops = {
286         .create_handle = create_handle,
287         .remove_handle = remove_handle,
288         .update_query_database = update_query_database,
289         .read_query_database = read_query_database,
290         .insert_query_database = insert_query_database,
291         .remove_query_database = remove_query_database,
292 };
293
294 static gboolean on_load()
295 {
296         dbg("i'm load!");
297         return TRUE;
298 }
299
300 static gboolean on_init(TcorePlugin *p)
301 {
302         if (!p)
303                 return FALSE;
304
305         tcore_storage_new(p, "database", &ops);
306
307         dbg("finish to initialize database plug-in");
308         return TRUE;
309 }
310
311 static void on_unload(TcorePlugin *p)
312 {
313         dbg("i'm unload!");
314         return;
315 }
316
317 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
318         .name = "DATABASE",
319         .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
320         .version = PLUGIN_VERSION,
321         .load = on_load,
322         .init = on_init,
323         .unload = on_unload
324 };