4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongHoo Park <donghoo.park@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
34 #ifndef PLUGIN_VERSION
35 #define PLUGIN_VERSION 1
38 static void *create_handle(Storage *strg, const char *path)
41 sqlite3 *handle = NULL;
43 rv = db_util_open(path, &handle, 0);
44 if (rv != SQLITE_OK) {
45 err("fail to connect database err(%d)", rv);
49 dbg("connected to %s", path);
53 static gboolean remove_handle(Storage *strg, void *handle)
58 db_util_close(handle);
60 dbg("disconnected from database");
64 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
67 sqlite3_stmt *stmt = NULL;
68 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
75 memset(szQuery, '\0', 1001);
76 strncpy(szQuery, query, 1000);
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);
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);
89 if (!value || g_strcmp0((const char *)value, "") == 0) {
91 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
94 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
98 if (rv != SQLITE_OK) {
99 dbg("fail to bind data (%d)", rv);
105 if (rv != SQLITE_OK) {
106 sqlite3_finalize(stmt);
110 rv = sqlite3_step(stmt);
111 dbg("update query executed (%d)", rv);
112 sqlite3_finalize(stmt);
114 if (rv != SQLITE_DONE)
120 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
121 gpointer out_param, int out_param_cnt, gboolean in_order)
123 int rv = 0, local_index = 0, outter_index = 0;
124 sqlite3_stmt *stmt = NULL;
125 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
132 memset(szQuery, '\0', 5001);
133 strncpy(szQuery, query, 5000);
135 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
136 if (rv != SQLITE_OK) {
137 err("fail to connect to table (%d)", rv);
142 g_hash_table_iter_init(&iter, in_param);
143 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
144 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
146 if (!value || g_strcmp0((const char *)value, "") == 0) {
148 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
151 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
155 if (rv != SQLITE_OK) {
156 dbg("fail to bind data (%d)", rv);
162 if (rv != SQLITE_OK) {
163 sqlite3_finalize(stmt);
167 rv = sqlite3_step(stmt);
168 dbg("read query executed (%d), in_order (%d)", rv, in_order);
170 while (rv == SQLITE_ROW) {
171 GHashTable *out_param_data;
173 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
175 for (local_index = 0; local_index < out_param_cnt; local_index++) {
177 const unsigned char *tmp;
178 tmp = sqlite3_column_text(stmt, local_index);
179 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
180 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
184 GSList **temp = out_param;
185 *temp = g_slist_append(*temp, out_param_data);
187 char tmp_key_outter[10];
188 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
189 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
192 rv = sqlite3_step(stmt);
195 sqlite3_finalize(stmt);
199 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
200 GHashTable **out_param, int out_param_cnt)
202 GHashTable *out_hash_table;
204 if (out_param == NULL)
207 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
208 g_free, (GDestroyNotify)g_hash_table_destroy);
210 if (_read_query_database_internal(strg,
211 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
212 g_hash_table_destroy(out_hash_table);
216 *out_param = out_hash_table;
221 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
222 GSList **out_param, int out_param_cnt)
224 if (_read_query_database_internal(strg,
225 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
231 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
234 sqlite3_stmt *stmt = NULL;
235 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
241 memset(szQuery, '\0', 5001);
242 strncpy(szQuery, query, 5000);
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);
251 g_hash_table_iter_init(&iter, in_param);
252 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
253 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
255 if (!value || g_strcmp0((const char *)value, "") == 0) {
257 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
260 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
264 if (rv != SQLITE_OK) {
265 dbg("fail to bind data (%d)", rv);
271 if (rv != SQLITE_OK) {
272 sqlite3_finalize(stmt);
276 rv = sqlite3_step(stmt);
277 dbg("insert query executed (%d)", rv);
278 sqlite3_finalize(stmt);
280 if (rv != SQLITE_DONE)
286 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
289 sqlite3_stmt *stmt = NULL;
290 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
296 memset(szQuery, '\0', 1001);
297 strncpy(szQuery, query, 1000);
299 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
300 if (rv != SQLITE_OK) {
301 err("fail to connect to table (%d)", rv);
306 g_hash_table_iter_init(&iter, in_param);
307 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
308 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
310 if (!value || g_strcmp0((const char *)value, "") == 0) {
312 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
315 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
319 if (rv != SQLITE_OK) {
320 dbg("fail to bind data (%d)", rv);
326 if (rv != SQLITE_OK) {
327 sqlite3_finalize(stmt);
331 rv = sqlite3_step(stmt);
332 dbg("remove query executed (%d)", rv);
333 sqlite3_finalize(stmt);
335 if (rv != SQLITE_DONE)
341 static struct storage_operations ops = {
342 .create_handle = create_handle,
343 .remove_handle = remove_handle,
344 .update_query_database = update_query_database,
345 .read_query_database = read_query_database,
346 .read_query_database_in_order = read_query_database_in_order,
347 .insert_query_database = insert_query_database,
348 .remove_query_database = remove_query_database,
351 static gboolean on_load()
357 static gboolean on_init(TcorePlugin *p)
362 tcore_storage_new(p, "database", &ops);
364 dbg("finish to initialize database plug-in");
368 static void on_unload(TcorePlugin *p)
374 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
376 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
377 .version = PLUGIN_VERSION,