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