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.
35 #ifndef PLUGIN_VERSION
36 #define PLUGIN_VERSION 1
39 static void *create_handle(Storage *strg, const char *path)
42 sqlite3 *handle = NULL;
44 rv = db_util_open(path, &handle, 0);
45 if (rv != SQLITE_OK) {
46 err("fail to connect database err(%d)", rv);
50 dbg("connected to %s", path);
54 static gboolean remove_handle(Storage *strg, void *handle)
61 rv = db_util_close(handle);
62 if (rv != SQLITE_OK) {
63 err("fail to close database err(%d)", rv);
68 dbg("disconnected from database");
72 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
75 sqlite3_stmt *stmt = NULL;
76 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
83 memset(szQuery, '\0', 1001);
84 strncpy(szQuery, query, 1000);
86 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
87 if (rv != SQLITE_OK) {
88 err("fail to connect to table (%d)", rv);
93 g_hash_table_iter_init(&iter, in_param);
94 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
95 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
97 if (!value || g_strcmp0((const char *)value, "") == 0) {
99 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
102 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
106 if (rv != SQLITE_OK) {
107 dbg("fail to bind data (%d)", rv);
113 if (rv != SQLITE_OK) {
114 sqlite3_finalize(stmt);
118 rv = sqlite3_step(stmt);
119 dbg("update query executed (%d)", rv);
120 sqlite3_finalize(stmt);
122 if (rv != SQLITE_DONE)
128 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
129 gpointer out_param, int out_param_cnt, gboolean in_order)
131 int rv = 0, local_index = 0, outter_index = 0;
132 sqlite3_stmt *stmt = NULL;
133 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
140 memset(szQuery, '\0', 5001);
141 strncpy(szQuery, query, 5000);
143 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
144 if (rv != SQLITE_OK) {
145 err("fail to connect to table (%d)", rv);
150 g_hash_table_iter_init(&iter, in_param);
151 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
152 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
154 if (!value || g_strcmp0((const char *)value, "") == 0) {
156 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
159 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
163 if (rv != SQLITE_OK) {
164 dbg("fail to bind data (%d)", rv);
170 if (rv != SQLITE_OK) {
171 sqlite3_finalize(stmt);
175 rv = sqlite3_step(stmt);
176 dbg("read query executed (%d), in_order (%d)", rv, in_order);
178 while (rv == SQLITE_ROW) {
179 GHashTable *out_param_data;
181 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
183 for (local_index = 0; local_index < out_param_cnt; local_index++) {
185 const unsigned char *tmp;
186 tmp = sqlite3_column_text(stmt, local_index);
187 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
188 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
192 GSList **temp = out_param;
193 *temp = g_slist_append(*temp, out_param_data);
195 char tmp_key_outter[10];
196 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
197 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
200 rv = sqlite3_step(stmt);
203 sqlite3_finalize(stmt);
207 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
208 GHashTable **out_param, int out_param_cnt)
210 GHashTable *out_hash_table;
212 if (out_param == NULL)
215 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
216 g_free, (GDestroyNotify)g_hash_table_destroy);
218 if (_read_query_database_internal(strg,
219 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
220 g_hash_table_destroy(out_hash_table);
224 *out_param = out_hash_table;
229 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
230 GSList **out_param, int out_param_cnt)
232 if (_read_query_database_internal(strg,
233 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
239 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
242 sqlite3_stmt *stmt = NULL;
243 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
249 memset(szQuery, '\0', 5001);
250 strncpy(szQuery, query, 5000);
252 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
253 if (rv != SQLITE_OK) {
254 err("fail to connect to table (%d)", rv);
259 g_hash_table_iter_init(&iter, in_param);
260 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
261 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
263 if (!value || g_strcmp0((const char *)value, "") == 0) {
265 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
268 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
272 if (rv != SQLITE_OK) {
273 dbg("fail to bind data (%d)", rv);
279 if (rv != SQLITE_OK) {
280 sqlite3_finalize(stmt);
284 rv = sqlite3_step(stmt);
285 dbg("insert query executed (%d)", rv);
286 sqlite3_finalize(stmt);
288 if (rv != SQLITE_DONE)
294 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
297 sqlite3_stmt *stmt = NULL;
298 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
304 memset(szQuery, '\0', 1001);
305 strncpy(szQuery, query, 1000);
307 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
308 if (rv != SQLITE_OK) {
309 err("fail to connect to table (%d)", rv);
314 g_hash_table_iter_init(&iter, in_param);
315 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
316 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
318 if (!value || g_strcmp0((const char *)value, "") == 0) {
320 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
323 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
327 if (rv != SQLITE_OK) {
328 dbg("fail to bind data (%d)", rv);
334 if (rv != SQLITE_OK) {
335 sqlite3_finalize(stmt);
339 rv = sqlite3_step(stmt);
340 dbg("remove query executed (%d)", rv);
341 sqlite3_finalize(stmt);
343 if (rv != SQLITE_DONE)
349 static struct storage_operations ops = {
350 .create_handle = create_handle,
351 .remove_handle = remove_handle,
352 .update_query_database = update_query_database,
353 .read_query_database = read_query_database,
354 .read_query_database_in_order = read_query_database_in_order,
355 .insert_query_database = insert_query_database,
356 .remove_query_database = remove_query_database,
359 static gboolean on_load()
365 static gboolean on_init(TcorePlugin *p)
370 tcore_storage_new(p, "database", &ops);
372 dbg("finish to initialize database plug-in");
376 static void on_unload(TcorePlugin *p)
385 strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
389 tcore_storage_free(strg);
394 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
396 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
397 .version = PLUGIN_VERSION,