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(Storage *strg, void *handle, const char *query, GHashTable *in_param,
116 GHashTable *out_param, int out_param_cnt)
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)", rv);
160 while (rv == SQLITE_ROW) {
162 char tmp_key_outter[10];
163 GHashTable *out_param_data;
165 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
167 for (local_index = 0; local_index < out_param_cnt; local_index++) {
169 const unsigned char *tmp;
170 tmp = sqlite3_column_text(stmt, local_index);
171 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
172 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
175 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
176 g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
178 rv = sqlite3_step(stmt);
181 sqlite3_finalize(stmt);
185 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
188 sqlite3_stmt *stmt = NULL;
189 char szQuery[5000+1]; /* +1 is for NULL Termination Character '\0' */
195 memset(szQuery, '\0', 5001);
196 strncpy(szQuery, query, 5000);
198 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
199 if (rv != SQLITE_OK) {
200 err("fail to connect to table (%d)", rv);
205 g_hash_table_iter_init(&iter, in_param);
206 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
207 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
209 if (!value || g_strcmp0((const char *)value, "") == 0) {
211 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
214 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
218 if (rv != SQLITE_OK) {
219 dbg("fail to bind data (%d)", rv);
225 rv = sqlite3_step(stmt);
226 dbg("insert query executed (%d)", rv);
227 sqlite3_finalize(stmt);
229 if (rv != SQLITE_DONE)
235 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
238 sqlite3_stmt *stmt = NULL;
239 char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */
245 memset(szQuery, '\0', 1001);
246 strncpy(szQuery, query, 1000);
248 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
249 if (rv != SQLITE_OK) {
250 err("fail to connect to table (%d)", rv);
255 g_hash_table_iter_init(&iter, in_param);
256 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
257 dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
259 if (!value || g_strcmp0((const char *)value, "") == 0) {
261 rv = sqlite3_bind_null(stmt, atoi((const char *)key));
264 rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
268 if (rv != SQLITE_OK) {
269 dbg("fail to bind data (%d)", rv);
275 rv = sqlite3_step(stmt);
276 dbg("remove query executed (%d)", rv);
277 sqlite3_finalize(stmt);
279 if (rv != SQLITE_DONE)
285 static struct storage_operations ops = {
286 .create_handle = create_handle,
287 .remove_handle = remove_handle,
288 .update_query_database = update_query_database,
289 .read_query_database = read_query_database,
290 .insert_query_database = insert_query_database,
291 .remove_query_database = remove_query_database,
294 static gboolean on_load()
300 static gboolean on_init(TcorePlugin *p)
305 tcore_storage_new(p, "database", &ops);
307 dbg("finish to initialize database plug-in");
311 static void on_unload(TcorePlugin *p)
317 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
319 .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
320 .version = PLUGIN_VERSION,