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.
36 #ifndef PLUGIN_VERSION
37 #define PLUGIN_VERSION 1
40 static gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
43 sqlite3_stmt *stmt = NULL;
44 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
51 memset(szQuery, '\0', 1001);
52 strncpy(szQuery, query, 1000);
54 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
55 if (rv != SQLITE_OK) {
56 err("fail to connect to table (%d)", rv);
61 g_hash_table_iter_init(&iter, in_param);
62 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
63 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
65 if (!value || g_strcmp0((const char *)value, "") == 0) {
67 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
70 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
74 if (rv != SQLITE_OK) {
75 dbg("fail to bind data (%d)", rv);
81 if (rv != SQLITE_OK) {
82 sqlite3_finalize(stmt);
86 rv = sqlite3_step(stmt);
87 dbg("query executed (%d)", rv);
88 sqlite3_finalize(stmt);
90 if (rv != SQLITE_DONE)
96 static int _busy_handler(void *pData, int count)
99 dbg("Busy Handler Called! : CNT(%d)\n", count + 1);
100 usleep((count + 1) * 100000);
103 dbg("Busy Handler will be returned SQLITE_BUSY error\n");
108 static void *create_handle(Storage *strg, const char *path)
111 sqlite3 *handle = NULL;
114 err("Invalid input param error");
118 rv = sqlite3_open(path, &handle);
119 if (rv != SQLITE_OK) {
120 err("fail to connect database err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
124 rv = sqlite3_busy_handler(handle, _busy_handler, NULL);
125 if (rv != SQLITE_OK) {
126 err("fail to register busy handler err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
130 dbg("connected to %s", path);
134 static gboolean remove_handle(Storage *strg, void *handle)
141 rv = sqlite3_close(handle);
142 if (rv != SQLITE_OK) {
143 err("fail to close database err(%d)", rv);
148 dbg("disconnected from database");
152 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
158 ret = __update_query_database(strg, handle, query, in_param);
163 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
164 gpointer out_param, int out_param_cnt, gboolean in_order)
166 int rv = 0, local_index = 0, outter_index = 0;
167 sqlite3_stmt *stmt = NULL;
168 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
175 memset(szQuery, '\0', 5001);
176 strncpy(szQuery, query, 5000);
178 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
179 if (rv != SQLITE_OK) {
180 err("fail to connect to table (%d)", rv);
185 g_hash_table_iter_init(&iter, in_param);
186 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
187 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
189 if (!value || g_strcmp0((const char *)value, "") == 0) {
191 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
194 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
198 if (rv != SQLITE_OK) {
199 dbg("fail to bind data (%d)", rv);
205 if (rv != SQLITE_OK) {
206 sqlite3_finalize(stmt);
210 rv = sqlite3_step(stmt);
211 dbg("read query executed (%d), in_order (%d)", rv, in_order);
213 while (rv == SQLITE_ROW) {
214 GHashTable *out_param_data;
216 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
218 for (local_index = 0; local_index < out_param_cnt; local_index++) {
220 const unsigned char *tmp;
221 tmp = sqlite3_column_text(stmt, local_index);
222 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
223 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
227 GSList **temp = out_param;
228 *temp = g_slist_append(*temp, out_param_data);
230 char tmp_key_outter[10];
231 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
232 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
235 rv = sqlite3_step(stmt);
238 sqlite3_finalize(stmt);
242 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
243 GHashTable **out_param, int out_param_cnt)
245 GHashTable *out_hash_table;
247 if (out_param == NULL)
250 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
251 g_free, (GDestroyNotify)g_hash_table_destroy);
253 if (_read_query_database_internal(strg,
254 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
255 g_hash_table_destroy(out_hash_table);
259 *out_param = out_hash_table;
264 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
265 GSList **out_param, int out_param_cnt)
267 if (_read_query_database_internal(strg,
268 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
274 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
277 sqlite3_stmt *stmt = NULL;
278 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
284 memset(szQuery, '\0', 5001);
285 strncpy(szQuery, query, 5000);
287 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
288 if (rv != SQLITE_OK) {
289 err("fail to connect to table (%d)", rv);
294 g_hash_table_iter_init(&iter, in_param);
295 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
296 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
298 if (!value || g_strcmp0((const char *)value, "") == 0) {
300 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
303 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
307 if (rv != SQLITE_OK) {
308 dbg("fail to bind data (%d)", rv);
314 if (rv != SQLITE_OK) {
315 sqlite3_finalize(stmt);
319 rv = sqlite3_step(stmt);
320 dbg("insert query executed (%d)", rv);
321 sqlite3_finalize(stmt);
323 if (rv != SQLITE_DONE)
329 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
335 ret = __update_query_database(strg, handle, query, in_param);
340 static struct storage_operations ops = {
341 .create_handle = create_handle,
342 .remove_handle = remove_handle,
343 .update_query_database = update_query_database,
344 .read_query_database = read_query_database,
345 .read_query_database_in_order = read_query_database_in_order,
346 .insert_query_database = insert_query_database,
347 .remove_query_database = remove_query_database,
350 static gboolean on_load()
356 static gboolean on_init(TcorePlugin *p)
361 tcore_storage_new(p, "database", &ops);
363 dbg("finish to initialize database plug-in");
367 static void on_unload(TcorePlugin *p)
376 strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
380 tcore_storage_free(strg);
385 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
387 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
388 .version = PLUGIN_VERSION,