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, NULL, 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 _read_query_database_internal(strg, handle, query, in_param, out_param, out_param_cnt, FALSE);
196 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
197 GSList **out_param, int out_param_cnt)
199 _read_query_database_internal(strg, handle, query, in_param, out_param, out_param_cnt, TRUE);
203 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
206 sqlite3_stmt *stmt = NULL;
207 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
213 memset(szQuery, '\0', 5001);
214 strncpy(szQuery, query, 5000);
216 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
217 if (rv != SQLITE_OK) {
218 err("fail to connect to table (%d)", rv);
223 g_hash_table_iter_init(&iter, in_param);
224 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
225 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
227 if (!value || g_strcmp0((const char *)value, "") == 0) {
229 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
232 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
236 if (rv != SQLITE_OK) {
237 dbg("fail to bind data (%d)", rv);
243 rv = sqlite3_step(stmt);
244 dbg("insert query executed (%d)", rv);
245 sqlite3_finalize(stmt);
247 if (rv != SQLITE_DONE)
253 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
256 sqlite3_stmt *stmt = NULL;
257 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
263 memset(szQuery, '\0', 1001);
264 strncpy(szQuery, query, 1000);
266 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
267 if (rv != SQLITE_OK) {
268 err("fail to connect to table (%d)", rv);
273 g_hash_table_iter_init(&iter, in_param);
274 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
275 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
277 if (!value || g_strcmp0((const char *)value, "") == 0) {
279 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
282 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
286 if (rv != SQLITE_OK) {
287 dbg("fail to bind data (%d)", rv);
293 rv = sqlite3_step(stmt);
294 dbg("remove query executed (%d)", rv);
295 sqlite3_finalize(stmt);
297 if (rv != SQLITE_DONE)
303 static struct storage_operations ops = {
304 .create_handle = create_handle,
305 .remove_handle = remove_handle,
306 .update_query_database = update_query_database,
307 .read_query_database = read_query_database,
308 .read_query_database_in_order = read_query_database_in_order,
309 .insert_query_database = insert_query_database,
310 .remove_query_database = remove_query_database,
313 static gboolean on_load()
319 static gboolean on_init(TcorePlugin *p)
324 tcore_storage_new(p, "database", &ops);
326 dbg("finish to initialize database plug-in");
330 static void on_unload(TcorePlugin *p)
336 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
338 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
339 .version = PLUGIN_VERSION,