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 rv = sqlite3_step(stmt);
106 dbg("update query executed (%d)", rv);
107 sqlite3_finalize(stmt);
109 if (rv != SQLITE_DONE)
115 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
116 gpointer out_param, int out_param_cnt, gboolean in_order)
118 int rv = 0, local_index = 0, outter_index = 0;
119 sqlite3_stmt *stmt = NULL;
120 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
127 memset(szQuery, '\0', 5001);
128 strncpy(szQuery, query, 5000);
130 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
131 if (rv != SQLITE_OK) {
132 err("fail to connect to table (%d)", rv);
137 g_hash_table_iter_init(&iter, in_param);
138 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
139 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
141 if (!value || g_strcmp0((const char *)value, "") == 0) {
143 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
146 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
150 if (rv != SQLITE_OK) {
151 dbg("fail to bind data (%d)", rv);
157 rv = sqlite3_step(stmt);
158 dbg("read query executed (%d), in_order (%d)", rv, in_order);
160 while (rv == SQLITE_ROW) {
161 GHashTable *out_param_data;
163 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
165 for (local_index = 0; local_index < out_param_cnt; local_index++) {
167 const unsigned char *tmp;
168 tmp = sqlite3_column_text(stmt, local_index);
169 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
170 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
174 GSList **temp = out_param;
175 *temp = g_slist_append(*temp, out_param_data);
177 char tmp_key_outter[10];
178 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
179 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
182 rv = sqlite3_step(stmt);
185 sqlite3_finalize(stmt);
189 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
190 GHashTable **out_param, int out_param_cnt)
192 GHashTable *out_hash_table;
194 if (out_param == NULL)
197 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
198 g_free, (GDestroyNotify)g_hash_table_destroy);
200 if (_read_query_database_internal(strg,
201 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
202 g_hash_table_destroy(out_hash_table);
206 *out_param = out_hash_table;
211 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
212 GSList **out_param, int out_param_cnt)
214 if (_read_query_database_internal(strg,
215 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
221 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
224 sqlite3_stmt *stmt = NULL;
225 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
231 memset(szQuery, '\0', 5001);
232 strncpy(szQuery, query, 5000);
234 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
235 if (rv != SQLITE_OK) {
236 err("fail to connect to table (%d)", rv);
241 g_hash_table_iter_init(&iter, in_param);
242 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
243 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
245 if (!value || g_strcmp0((const char *)value, "") == 0) {
247 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
250 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
254 if (rv != SQLITE_OK) {
255 dbg("fail to bind data (%d)", rv);
261 rv = sqlite3_step(stmt);
262 dbg("insert query executed (%d)", rv);
263 sqlite3_finalize(stmt);
265 if (rv != SQLITE_DONE)
271 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
274 sqlite3_stmt *stmt = NULL;
275 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
281 memset(szQuery, '\0', 1001);
282 strncpy(szQuery, query, 1000);
284 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
285 if (rv != SQLITE_OK) {
286 err("fail to connect to table (%d)", rv);
291 g_hash_table_iter_init(&iter, in_param);
292 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
293 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
295 if (!value || g_strcmp0((const char *)value, "") == 0) {
297 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
300 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
304 if (rv != SQLITE_OK) {
305 dbg("fail to bind data (%d)", rv);
311 rv = sqlite3_step(stmt);
312 dbg("remove query executed (%d)", rv);
313 sqlite3_finalize(stmt);
315 if (rv != SQLITE_DONE)
321 static struct storage_operations ops = {
322 .create_handle = create_handle,
323 .remove_handle = remove_handle,
324 .update_query_database = update_query_database,
325 .read_query_database = read_query_database,
326 .read_query_database_in_order = read_query_database_in_order,
327 .insert_query_database = insert_query_database,
328 .remove_query_database = remove_query_database,
331 static gboolean on_load()
337 static gboolean on_init(TcorePlugin *p)
342 tcore_storage_new(p, "database", &ops);
344 dbg("finish to initialize database plug-in");
348 static void on_unload(TcorePlugin *p)
354 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
356 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
357 .version = PLUGIN_VERSION,