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