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)
59 db_util_close(handle);
61 dbg("disconnected from database");
65 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
68 sqlite3_stmt *stmt = NULL;
69 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
76 memset(szQuery, '\0', 1001);
77 strncpy(szQuery, query, 1000);
79 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
80 if (rv != SQLITE_OK) {
81 err("fail to connect to table (%d)", rv);
86 g_hash_table_iter_init(&iter, in_param);
87 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
88 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
90 if (!value || g_strcmp0((const char *)value, "") == 0) {
92 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
95 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
99 if (rv != SQLITE_OK) {
100 dbg("fail to bind data (%d)", rv);
106 if (rv != SQLITE_OK) {
107 sqlite3_finalize(stmt);
111 rv = sqlite3_step(stmt);
112 dbg("update query executed (%d)", rv);
113 sqlite3_finalize(stmt);
115 if (rv != SQLITE_DONE)
121 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
122 gpointer out_param, int out_param_cnt, gboolean in_order)
124 int rv = 0, local_index = 0, outter_index = 0;
125 sqlite3_stmt *stmt = NULL;
126 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
133 memset(szQuery, '\0', 5001);
134 strncpy(szQuery, query, 5000);
136 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
137 if (rv != SQLITE_OK) {
138 err("fail to connect to table (%d)", rv);
143 g_hash_table_iter_init(&iter, in_param);
144 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
145 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
147 if (!value || g_strcmp0((const char *)value, "") == 0) {
149 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
152 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
156 if (rv != SQLITE_OK) {
157 dbg("fail to bind data (%d)", rv);
163 if (rv != SQLITE_OK) {
164 sqlite3_finalize(stmt);
168 rv = sqlite3_step(stmt);
169 dbg("read query executed (%d), in_order (%d)", rv, in_order);
171 while (rv == SQLITE_ROW) {
172 GHashTable *out_param_data;
174 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
176 for (local_index = 0; local_index < out_param_cnt; local_index++) {
178 const unsigned char *tmp;
179 tmp = sqlite3_column_text(stmt, local_index);
180 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
181 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
185 GSList **temp = out_param;
186 *temp = g_slist_append(*temp, out_param_data);
188 char tmp_key_outter[10];
189 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
190 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
193 rv = sqlite3_step(stmt);
196 sqlite3_finalize(stmt);
200 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
201 GHashTable **out_param, int out_param_cnt)
203 GHashTable *out_hash_table;
205 if (out_param == NULL)
208 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
209 g_free, (GDestroyNotify)g_hash_table_destroy);
211 if (_read_query_database_internal(strg,
212 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
213 g_hash_table_destroy(out_hash_table);
217 *out_param = out_hash_table;
222 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
223 GSList **out_param, int out_param_cnt)
225 if (_read_query_database_internal(strg,
226 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
232 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
235 sqlite3_stmt *stmt = NULL;
236 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
242 memset(szQuery, '\0', 5001);
243 strncpy(szQuery, query, 5000);
245 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
246 if (rv != SQLITE_OK) {
247 err("fail to connect to table (%d)", rv);
252 g_hash_table_iter_init(&iter, in_param);
253 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
254 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
256 if (!value || g_strcmp0((const char *)value, "") == 0) {
258 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
261 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
265 if (rv != SQLITE_OK) {
266 dbg("fail to bind data (%d)", rv);
272 if (rv != SQLITE_OK) {
273 sqlite3_finalize(stmt);
277 rv = sqlite3_step(stmt);
278 dbg("insert query executed (%d)", rv);
279 sqlite3_finalize(stmt);
281 if (rv != SQLITE_DONE)
287 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
290 sqlite3_stmt *stmt = NULL;
291 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
297 memset(szQuery, '\0', 1001);
298 strncpy(szQuery, query, 1000);
300 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
301 if (rv != SQLITE_OK) {
302 err("fail to connect to table (%d)", rv);
307 g_hash_table_iter_init(&iter, in_param);
308 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
309 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
311 if (!value || g_strcmp0((const char *)value, "") == 0) {
313 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
316 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
320 if (rv != SQLITE_OK) {
321 dbg("fail to bind data (%d)", rv);
327 if (rv != SQLITE_OK) {
328 sqlite3_finalize(stmt);
332 rv = sqlite3_step(stmt);
333 dbg("remove query executed (%d)", rv);
334 sqlite3_finalize(stmt);
336 if (rv != SQLITE_DONE)
342 static struct storage_operations ops = {
343 .create_handle = create_handle,
344 .remove_handle = remove_handle,
345 .update_query_database = update_query_database,
346 .read_query_database = read_query_database,
347 .read_query_database_in_order = read_query_database_in_order,
348 .insert_query_database = insert_query_database,
349 .remove_query_database = remove_query_database,
352 static gboolean on_load()
358 static gboolean on_init(TcorePlugin *p)
363 tcore_storage_new(p, "database", &ops);
365 dbg("finish to initialize database plug-in");
369 static void on_unload(TcorePlugin *p)
378 strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
382 tcore_storage_free(strg);
387 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
389 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
390 .version = PLUGIN_VERSION,