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 #define BUSY_WAITING_USEC 50000 /* 0.05 sec */
41 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
44 static gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
47 sqlite3_stmt *stmt = NULL;
48 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
55 memset(szQuery, '\0', 1001);
56 strncpy(szQuery, query, 1000);
58 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
59 if (rv != SQLITE_OK) {
60 err("fail to connect to table (%d)", rv);
65 g_hash_table_iter_init(&iter, in_param);
66 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
67 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
69 if (!value || g_strcmp0((const char *)value, "") == 0) {
71 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
74 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
78 if (rv != SQLITE_OK) {
79 dbg("fail to bind data (%d)", rv);
85 if (rv != SQLITE_OK) {
86 sqlite3_finalize(stmt);
90 rv = sqlite3_step(stmt);
91 dbg("update query executed (%d)", rv);
92 sqlite3_finalize(stmt);
94 if (rv != SQLITE_DONE)
100 static int _busy_handler(void *pData, int count)
102 if (count < BUSY_WAITING_MAX) {
103 usleep(BUSY_WAITING_USEC);
107 dbg("Busy Handler will be returned SQLITE_BUSY error\n");
111 static void *create_handle(Storage *strg, const char *path)
114 sqlite3 *handle = NULL;
117 err("Invalid input param error");
121 rv = sqlite3_open(path, &handle);
122 if (rv != SQLITE_OK) {
123 err("fail to connect database err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
127 rv = sqlite3_busy_handler(handle, _busy_handler, NULL);
128 if (rv != SQLITE_OK) {
129 err("fail to register busy handler err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
133 dbg("connected to %s", path);
137 static gboolean remove_handle(Storage *strg, void *handle)
144 rv = sqlite3_close(handle);
145 if (rv != SQLITE_OK) {
146 err("fail to close database err(%d)", rv);
151 dbg("disconnected from database");
155 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
161 ret = __update_query_database(strg, handle, query, in_param);
166 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
167 gpointer out_param, int out_param_cnt, gboolean in_order)
169 int rv = 0, local_index = 0, outter_index = 0;
170 sqlite3_stmt *stmt = NULL;
171 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
178 memset(szQuery, '\0', 5001);
179 strncpy(szQuery, query, 5000);
181 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
182 if (rv != SQLITE_OK) {
183 err("fail to connect to table (%d)", rv);
188 g_hash_table_iter_init(&iter, in_param);
189 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
190 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
192 if (!value || g_strcmp0((const char *)value, "") == 0) {
194 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
197 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
201 if (rv != SQLITE_OK) {
202 dbg("fail to bind data (%d)", rv);
208 if (rv != SQLITE_OK) {
209 sqlite3_finalize(stmt);
213 rv = sqlite3_step(stmt);
214 dbg("read query executed (%d), in_order (%d)", rv, in_order);
216 while (rv == SQLITE_ROW) {
217 GHashTable *out_param_data;
219 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
221 for (local_index = 0; local_index < out_param_cnt; local_index++) {
223 const unsigned char *tmp;
224 tmp = sqlite3_column_text(stmt, local_index);
225 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
226 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
230 GSList **temp = out_param;
231 *temp = g_slist_append(*temp, out_param_data);
233 char tmp_key_outter[10];
234 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
235 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
238 rv = sqlite3_step(stmt);
241 sqlite3_finalize(stmt);
245 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
246 GHashTable **out_param, int out_param_cnt)
248 GHashTable *out_hash_table;
250 if (out_param == NULL)
253 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
254 g_free, (GDestroyNotify)g_hash_table_destroy);
256 if (_read_query_database_internal(strg,
257 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
258 g_hash_table_destroy(out_hash_table);
262 *out_param = out_hash_table;
267 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
268 GSList **out_param, int out_param_cnt)
270 if (_read_query_database_internal(strg,
271 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
277 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
280 sqlite3_stmt *stmt = NULL;
281 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
287 memset(szQuery, '\0', 5001);
288 strncpy(szQuery, query, 5000);
290 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
291 if (rv != SQLITE_OK) {
292 err("fail to connect to table (%d)", rv);
297 g_hash_table_iter_init(&iter, in_param);
298 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
299 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
301 if (!value || g_strcmp0((const char *)value, "") == 0) {
303 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
306 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
310 if (rv != SQLITE_OK) {
311 dbg("fail to bind data (%d)", rv);
317 if (rv != SQLITE_OK) {
318 sqlite3_finalize(stmt);
322 rv = sqlite3_step(stmt);
323 dbg("insert query executed (%d)", rv);
324 sqlite3_finalize(stmt);
326 if (rv != SQLITE_DONE)
332 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
338 ret = __update_query_database(strg, handle, query, in_param);
343 static struct storage_operations ops = {
344 .create_handle = create_handle,
345 .remove_handle = remove_handle,
346 .update_query_database = update_query_database,
347 .read_query_database = read_query_database,
348 .read_query_database_in_order = read_query_database_in_order,
349 .insert_query_database = insert_query_database,
350 .remove_query_database = remove_query_database,
353 static gboolean on_load()
359 static gboolean on_init(TcorePlugin *p)
364 tcore_storage_new(p, "database", &ops);
366 dbg("finish to initialize database plug-in");
370 static void on_unload(TcorePlugin *p)
379 strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
383 tcore_storage_free(strg);
388 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
390 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
391 .version = PLUGIN_VERSION,