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 _E("db_open_ro ret=%d", ret);
58 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
62 if(mode & DB_OPEN_RW) {
64 ret = db_util_open(APP_INFO_DB, &db_info.dbrw, DB_UTIL_REGISTER_HOOK_METHOD);
65 _E("db_open_rw ret=%d", ret);
66 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
75 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt)
79 retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
80 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
81 retv_if(!db_info.dbro, AIL_ERROR_DB_FAILED);
83 ret = sqlite3_prepare_v2(db_info.dbro, query, strlen(query), stmt, NULL);
84 if (ret != SQLITE_OK) {
85 _E("%s\n", sqlite3_errmsg(db_info.dbro));
86 return AIL_ERROR_DB_FAILED;
92 ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
96 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
98 ret = sqlite3_bind_int(stmt, idx, (int) value);
99 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
106 ail_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value)
110 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
112 ret = sqlite3_bind_int(stmt, idx, value);
113 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
120 ail_error_e db_step(sqlite3_stmt *stmt)
124 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
126 ret = sqlite3_step(stmt);
129 return AIL_ERROR_NO_DATA;
134 retv_with_dbmsg_if(1, AIL_ERROR_DB_FAILED);
139 ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
143 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
144 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
146 out_val = sqlite3_column_int(stmt, index);
147 *value = (out_val == 1)? true:false;
154 ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
156 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
157 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
159 *value = sqlite3_column_int(stmt, index);
166 ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
168 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
169 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
171 *str = (char *)sqlite3_column_text(stmt, index);
178 ail_error_e db_reset(sqlite3_stmt *stmt)
182 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
184 sqlite3_clear_bindings(stmt);
186 ret = sqlite3_reset(stmt);
187 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
194 ail_error_e db_finalize(sqlite3_stmt *stmt)
198 retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
200 ret = sqlite3_finalize(stmt);
201 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
208 ail_error_e db_exec(const char *query)
213 retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
214 retv_if(!db_info.dbrw, AIL_ERROR_DB_FAILED);
216 ret = sqlite3_exec(db_info.dbrw, query, NULL, NULL, &errmsg);
217 if (ret != SQLITE_OK) {
218 _E("Cannot execute this query - %s. because %s",
219 query, errmsg? errmsg:"uncatched error");
220 sqlite3_free(errmsg);
221 return AIL_ERROR_DB_FAILED;
229 ail_error_e db_close(void)
234 ret = sqlite3_close(db_info.dbro);
235 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
240 ret = sqlite3_close(db_info.dbrw);
241 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
249 EXPORT_API ail_error_e ail_db_close(void)
254 int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
256 char *error_message = NULL;
258 sqlite3_exec(db_info.dbro, query, callback, data, &error_message)) {
259 _E("Don't execute query = %s error message = %s\n", query,
261 sqlite3_free(error_message);
264 sqlite3_free(error_message);