4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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.
29 #include "ail_private.h"
32 #define retv_with_dbmsg_if(expr, val) do { \
34 _E("db_info.dbro: %s", sqlite3_errmsg(db_info.dbro)); \
35 _E("db_info.dbrw: %s", sqlite3_errmsg(db_info.dbrw)); \
41 static __thread struct {
49 ail_error_e db_open(db_open_mode mode)
54 if(mode & DB_OPEN_RO) {
56 ret = db_util_open_with_options(APP_INFO_DB, &db_info.dbro, SQLITE_OPEN_READONLY, NULL);
57 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
61 if(mode & DB_OPEN_RW) {
63 ret = db_util_open(APP_INFO_DB, &db_info.dbrw, DB_UTIL_REGISTER_HOOK_METHOD);
64 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
73 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt)
77 retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
78 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
79 retv_if(!db_info.dbro, AIL_ERROR_DB_FAILED);
81 ret = sqlite3_prepare_v2(db_info.dbro, query, strlen(query), stmt, NULL);
82 if (ret != SQLITE_OK) {
83 _E("%s\n", sqlite3_errmsg(db_info.dbro));
84 return AIL_ERROR_DB_FAILED;
90 ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
94 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
96 ret = sqlite3_bind_int(stmt, idx, (int) value);
97 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
104 ail_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value)
108 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
110 ret = sqlite3_bind_int(stmt, idx, value);
111 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
118 ail_error_e db_step(sqlite3_stmt *stmt)
122 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
124 ret = sqlite3_step(stmt);
127 return AIL_ERROR_NO_DATA;
132 retv_with_dbmsg_if(1, AIL_ERROR_DB_FAILED);
137 ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
141 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
142 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
144 out_val = sqlite3_column_int(stmt, index);
145 *value = (out_val == 1)? true:false;
152 ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
154 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
155 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
157 *value = sqlite3_column_int(stmt, index);
164 ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
166 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
167 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
169 *str = (char *)sqlite3_column_text(stmt, index);
176 ail_error_e db_reset(sqlite3_stmt *stmt)
180 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
182 sqlite3_clear_bindings(stmt);
184 ret = sqlite3_reset(stmt);
185 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
192 ail_error_e db_finalize(sqlite3_stmt *stmt)
196 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
198 ret = sqlite3_finalize(stmt);
199 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
206 ail_error_e db_exec(const char *query)
211 retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
212 retv_if(!db_info.dbrw, AIL_ERROR_DB_FAILED);
214 ret = sqlite3_exec(db_info.dbrw, query, NULL, NULL, &errmsg);
215 if (ret != SQLITE_OK) {
216 _E("Cannot execute this query - %s. because %s",
217 query, errmsg? errmsg:"uncatched error");
218 sqlite3_free(errmsg);
219 return AIL_ERROR_DB_FAILED;
227 ail_error_e db_close(void)
232 ret = sqlite3_close(db_info.dbro);
233 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
238 ret = sqlite3_close(db_info.dbrw);
239 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
247 EXPORT_API ail_error_e ail_db_close(void)
252 int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
254 char *error_message = NULL;
256 sqlite3_exec(db_info.dbro, query, callback, data, &error_message)) {
257 _E("Don't execute query = %s error message = %s\n", query,
259 sqlite3_free(error_message);
262 sqlite3_free(error_message);