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);
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));
96 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
100 if (rv != SQLITE_OK) {
101 dbg("fail to bind data (%d)", rv);
107 rv = sqlite3_step(stmt);
108 dbg("update query executed (%d)", rv);
109 sqlite3_finalize(stmt);
111 if (rv != SQLITE_DONE) {
118 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
119 GHashTable *out_param, int out_param_cnt)
121 int rv = 0, local_index = 0, outter_index = 0;
122 sqlite3_stmt* stmt = NULL;
123 char szQuery[5000+1]; // +1 is for NULL Termination Character '\0'
130 memset(szQuery, '\0', 5001);
131 strncpy(szQuery, query, 5000);
133 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
134 if (rv != SQLITE_OK) {
135 err("fail to connect to table (%d)", rv);
140 g_hash_table_iter_init(&iter, in_param);
141 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
142 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
144 if (!value || g_strcmp0((const char*) value, "") == 0) {
146 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
150 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
154 if (rv != SQLITE_OK) {
155 dbg("fail to bind data (%d)", rv);
161 rv = sqlite3_step(stmt);
162 dbg("read query executed (%d)", rv);
164 while (rv == SQLITE_ROW) {
166 char tmp_key_outter[10];
167 GHashTable *out_param_data;
169 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
171 for (local_index = 0; local_index < out_param_cnt; local_index++) {
173 const unsigned char *tmp;
174 tmp = sqlite3_column_text(stmt, local_index);
175 snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
176 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const gchar *) tmp));
179 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
180 g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
182 rv = sqlite3_step(stmt);
185 sqlite3_finalize(stmt);
189 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
192 sqlite3_stmt* stmt = NULL;
193 char szQuery[5000+1]; // +1 is for NULL Termination Character '\0'
199 memset(szQuery, '\0', 5001);
200 strncpy(szQuery, query, 5000);
202 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
203 if (rv != SQLITE_OK) {
204 err("fail to connect to table (%d)", rv);
209 g_hash_table_iter_init(&iter, in_param);
210 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
211 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
213 if (!value || g_strcmp0((const char*) value, "") == 0) {
215 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
219 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
223 if (rv != SQLITE_OK) {
224 dbg("fail to bind data (%d)", rv);
230 rv = sqlite3_step(stmt);
231 dbg("insert query executed (%d)", rv);
232 sqlite3_finalize(stmt);
234 if (rv != SQLITE_DONE) {
240 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
243 sqlite3_stmt* stmt = NULL;
244 char szQuery[1000+1]; // +1 is for NULL Termination Character '\0'
250 memset(szQuery, '\0', 1001);
251 strncpy(szQuery, query, 1000);
253 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
254 if (rv != SQLITE_OK) {
255 err("fail to connect to table (%d)", rv);
260 g_hash_table_iter_init(&iter, in_param);
261 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
262 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
264 if (!value || g_strcmp0((const char*) value, "") == 0) {
266 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
270 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
274 if (rv != SQLITE_OK) {
275 dbg("fail to bind data (%d)", rv);
281 rv = sqlite3_step(stmt);
282 dbg("remove query executed (%d)", rv);
283 sqlite3_finalize(stmt);
285 if (rv != SQLITE_DONE) {
292 static struct storage_operations ops = {
293 .create_handle = create_handle,
294 .remove_handle = remove_handle,
295 .update_query_database = update_query_database,
296 .read_query_database = read_query_database,
297 .insert_query_database = insert_query_database,
298 .remove_query_database = remove_query_database,
301 static gboolean on_load()
307 static gboolean on_init(TcorePlugin *p)
312 tcore_storage_new(p, "database", &ops);
314 dbg("finish to initialize database plug-in");
318 static void on_unload(TcorePlugin *p)
324 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc =
327 .priority = TCORE_PLUGIN_PRIORITY_HIGH -1,
328 .version = PLUGIN_VERSION,