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 gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
42 sqlite3_stmt *stmt = NULL;
43 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
50 memset(szQuery, '\0', 1001);
51 strncpy(szQuery, query, 1000);
53 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
54 if (rv != SQLITE_OK) {
55 err("fail to connect to table (%d)", rv);
60 g_hash_table_iter_init(&iter, in_param);
61 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
62 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
64 if (!value || g_strcmp0((const char *)value, "") == 0) {
66 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
69 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
73 if (rv != SQLITE_OK) {
74 dbg("fail to bind data (%d)", rv);
80 if (rv != SQLITE_OK) {
81 sqlite3_finalize(stmt);
85 rv = sqlite3_step(stmt);
86 dbg("query executed (%d)", rv);
87 sqlite3_finalize(stmt);
89 if (rv != SQLITE_DONE)
95 static void *create_handle(Storage *strg, const char *path)
98 sqlite3 *handle = NULL;
100 rv = db_util_open(path, &handle, 0);
101 if (rv != SQLITE_OK) {
102 err("fail to connect database err(%d)", rv);
106 dbg("connected to %s", path);
110 static gboolean remove_handle(Storage *strg, void *handle)
117 rv = db_util_close(handle);
118 if (rv != SQLITE_OK) {
119 err("fail to close database err(%d)", rv);
124 dbg("disconnected from database");
128 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
134 ret = __update_query_database(strg, handle, query, in_param);
139 static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
140 gpointer out_param, int out_param_cnt, gboolean in_order)
142 int rv = 0, local_index = 0, outter_index = 0;
143 sqlite3_stmt *stmt = NULL;
144 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
151 memset(szQuery, '\0', 5001);
152 strncpy(szQuery, query, 5000);
154 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
155 if (rv != SQLITE_OK) {
156 err("fail to connect to table (%d)", rv);
161 g_hash_table_iter_init(&iter, in_param);
162 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
163 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
165 if (!value || g_strcmp0((const char *)value, "") == 0) {
167 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
170 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
174 if (rv != SQLITE_OK) {
175 dbg("fail to bind data (%d)", rv);
181 if (rv != SQLITE_OK) {
182 sqlite3_finalize(stmt);
186 rv = sqlite3_step(stmt);
187 dbg("read query executed (%d), in_order (%d)", rv, in_order);
189 while (rv == SQLITE_ROW) {
190 GHashTable *out_param_data;
192 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
194 for (local_index = 0; local_index < out_param_cnt; local_index++) {
196 const unsigned char *tmp;
197 tmp = sqlite3_column_text(stmt, local_index);
198 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
199 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
203 GSList **temp = out_param;
204 *temp = g_slist_append(*temp, out_param_data);
206 char tmp_key_outter[10];
207 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
208 g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
211 rv = sqlite3_step(stmt);
214 sqlite3_finalize(stmt);
218 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
219 GHashTable **out_param, int out_param_cnt)
221 GHashTable *out_hash_table;
223 if (out_param == NULL)
226 out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
227 g_free, (GDestroyNotify)g_hash_table_destroy);
229 if (_read_query_database_internal(strg,
230 handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
231 g_hash_table_destroy(out_hash_table);
235 *out_param = out_hash_table;
240 static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
241 GSList **out_param, int out_param_cnt)
243 if (_read_query_database_internal(strg,
244 handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
250 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
253 sqlite3_stmt *stmt = NULL;
254 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
260 memset(szQuery, '\0', 5001);
261 strncpy(szQuery, query, 5000);
263 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
264 if (rv != SQLITE_OK) {
265 err("fail to connect to table (%d)", rv);
270 g_hash_table_iter_init(&iter, in_param);
271 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
272 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
274 if (!value || g_strcmp0((const char *)value, "") == 0) {
276 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
279 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
283 if (rv != SQLITE_OK) {
284 dbg("fail to bind data (%d)", rv);
290 if (rv != SQLITE_OK) {
291 sqlite3_finalize(stmt);
295 rv = sqlite3_step(stmt);
296 dbg("insert query executed (%d)", rv);
297 sqlite3_finalize(stmt);
299 if (rv != SQLITE_DONE)
305 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
311 ret = __update_query_database(strg, handle, query, in_param);
316 static struct storage_operations ops = {
317 .create_handle = create_handle,
318 .remove_handle = remove_handle,
319 .update_query_database = update_query_database,
320 .read_query_database = read_query_database,
321 .read_query_database_in_order = read_query_database_in_order,
322 .insert_query_database = insert_query_database,
323 .remove_query_database = remove_query_database,
326 static gboolean on_load()
332 static gboolean on_init(TcorePlugin *p)
337 tcore_storage_new(p, "database", &ops);
339 dbg("finish to initialize database plug-in");
343 static void on_unload(TcorePlugin *p)
352 strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
356 tcore_storage_free(strg);
361 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
363 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
364 .version = PLUGIN_VERSION,