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 static void* create_handle(Storage *strg, const char *path)
37 sqlite3 *handle = NULL;
39 rv = db_util_open(path, &handle, 0);
40 if (rv != SQLITE_OK) {
41 err("fail to connect database err(%d)", rv);
45 dbg("connected to %s", path);
49 static gboolean remove_handle(Storage *strg, void *handle)
54 db_util_close(handle);
57 dbg("disconnected from database");
61 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
64 sqlite3_stmt* stmt = NULL;
72 memset(szQuery, '\0', 1000);
73 strcpy(szQuery, query);
75 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
76 if (rv != SQLITE_OK) {
77 err("fail to connect to table (%d)", rv);
81 g_hash_table_iter_init(&iter, in_param);
82 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
83 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
85 if (!value || g_strcmp0((const char*) value, "") == 0) {
87 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
91 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
95 if (rv != SQLITE_OK) {
96 dbg("fail to bind data (%d)", rv);
101 rv = sqlite3_step(stmt);
102 dbg("update query executed (%d)", rv);
103 sqlite3_finalize(stmt);
105 if (rv != SQLITE_DONE) {
112 static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
113 GHashTable *out_param, int out_param_cnt)
115 int rv = 0, index = 0, outter_index = 0;
116 sqlite3_stmt* stmt = NULL;
124 memset(szQuery, '\0', 5000);
125 strcpy(szQuery, query);
127 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
128 if (rv != SQLITE_OK) {
129 err("fail to connect to table (%d)", rv);
134 g_hash_table_iter_init(&iter, in_param);
135 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
136 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
138 if (!value || g_strcmp0((const char*) value, "") == 0) {
140 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
144 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
148 if (rv != SQLITE_OK) {
149 dbg("fail to bind data (%d)", rv);
155 rv = sqlite3_step(stmt);
156 dbg("read query executed (%d)", rv);
158 while (rv == SQLITE_ROW) {
160 char tmp_key_outter[10];
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 (index = 0; index < out_param_cnt; index++) {
166 char *tmp = NULL, tmp_key[10];
167 tmp = (char *) sqlite3_column_text(stmt, index);
168 snprintf(tmp_key, sizeof(tmp_key), "%d", index);
169 g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup(tmp));
172 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
173 g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
175 rv = sqlite3_step(stmt);
178 sqlite3_finalize(stmt);
182 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
185 sqlite3_stmt* stmt = NULL;
192 memset(szQuery, '\0', 5000);
193 strcpy(szQuery, query);
195 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
196 if (rv != SQLITE_OK) {
197 err("fail to connect to table (%d)", rv);
201 g_hash_table_iter_init(&iter, in_param);
202 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
203 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
205 if (!value || g_strcmp0((const char*) value, "") == 0) {
207 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
211 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
215 if (rv != SQLITE_OK) {
216 dbg("fail to bind data (%d)", rv);
221 rv = sqlite3_step(stmt);
222 dbg("insert query executed (%d)", rv);
223 sqlite3_finalize(stmt);
225 if (rv != SQLITE_DONE) {
231 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
234 sqlite3_stmt* stmt = NULL;
241 memset(szQuery, '\0', 1000);
242 strcpy(szQuery, query);
244 rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
245 if (rv != SQLITE_OK) {
246 err("fail to connect to table (%d)", rv);
250 g_hash_table_iter_init(&iter, in_param);
251 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
252 dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
254 if (!value || g_strcmp0((const char*) value, "") == 0) {
256 rv = sqlite3_bind_null(stmt, atoi((const char*) key));
260 rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
264 if (rv != SQLITE_OK) {
265 dbg("fail to bind data (%d)", rv);
270 rv = sqlite3_step(stmt);
271 dbg("remove query executed (%d)", rv);
272 sqlite3_finalize(stmt);
274 if (rv != SQLITE_DONE) {
281 struct storage_operations ops = {
282 .create_handle = create_handle,
283 .remove_handle = remove_handle,
284 .update_query_database = update_query_database,
285 .read_query_database = read_query_database,
286 .insert_query_database = insert_query_database,
287 .remove_query_database = remove_query_database,
290 static gboolean on_load()
296 static gboolean on_init(TcorePlugin *p)
303 strg = tcore_storage_new(p, "database", &ops);
305 dbg("finish to initialize database plug-in");
309 static void on_unload(TcorePlugin *p)
315 struct tcore_plugin_define_desc plugin_define_desc =
318 .priority = TCORE_PLUGIN_PRIORITY_HIGH -1,