4 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Jin Yoon <jinny.yoon@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
35 #define retv_with_dbmsg_if(expr, val) do { \
37 _E("%s", sqlite3_errmsg(db_info.db)); \
42 #define CREATE_APPS_DB_TABLE "CREATE TABLE IF NOT EXISTS shortcut (\
43 ROWID INTEGER PRIMARY KEY AUTOINCREMENT,\
64 HAPI menu_screen_error_e db_open(const char *db_file)
69 char *res_path = NULL;
70 char db_file_path[1024];
72 res_path = app_get_data_path();
73 snprintf(db_file_path, sizeof(db_file_path), "%s%s", res_path, db_file);
75 retv_if(NULL == db_file, MENU_SCREEN_ERROR_INVALID_PARAMETER);
77 _D("db is already exist");
78 return MENU_SCREEN_ERROR_OK;
81 FILE *fp = fopen(db_file_path, "r");
84 _D("Apps DB[%s] exist", db_file_path);
85 return MENU_SCREEN_ERROR_OK;
88 ret = sqlite3_open(db_file_path, &db_info.db);
89 if (ret != SQLITE_OK) {
90 _E("SQL error(%d) : %s", ret, db_file_path);
93 ret = sqlite3_exec(db_info.db, "PRAGMA journal_mode = PERSIST", NULL, NULL, &errMsg);
94 if (ret != SQLITE_OK) {
95 _E("SQL error(%d) : %s", ret, errMsg);
97 return MENU_SCREEN_ERROR_FAIL;
100 ret = sqlite3_exec(db_info.db, CREATE_APPS_DB_TABLE, NULL, NULL, &errMsg);
101 if (ret != SQLITE_OK) {
102 _E("SQL error(%d) : %s", ret, errMsg);
103 sqlite3_free(errMsg);
104 return MENU_SCREEN_ERROR_FAIL;
106 _D("Create DB[%s] : [%s] OK", db_file, CREATE_APPS_DB_TABLE);
108 return MENU_SCREEN_ERROR_OK;
113 HAPI stmt_h *db_prepare(const char *query)
118 retv_if(NULL == query, NULL);
120 handle = calloc(1, sizeof(stmt_h));
121 retv_if(NULL == handle, NULL);
123 ret = sqlite3_prepare_v2(db_info.db, query, strlen(query), &(handle->stmt), NULL);
124 if (ret != SQLITE_OK) {
126 _E("%s", sqlite3_errmsg(db_info.db));
135 HAPI menu_screen_error_e db_bind_bool(stmt_h *handle, int idx, bool value)
139 retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
141 ret = sqlite3_bind_int(handle->stmt, idx, (int) value);
142 retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
144 return MENU_SCREEN_ERROR_OK;
149 HAPI menu_screen_error_e db_bind_int(stmt_h *handle, int idx, int value)
153 retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
155 ret = sqlite3_bind_int(handle->stmt, idx, value);
156 retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
158 return MENU_SCREEN_ERROR_OK;
163 HAPI menu_screen_error_e db_bind_str(stmt_h *handle, int idx, const char *str)
167 retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
169 ret = sqlite3_bind_text(handle->stmt, idx, str, strlen(str), SQLITE_TRANSIENT);
170 retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
172 return MENU_SCREEN_ERROR_OK;
177 HAPI menu_screen_error_e db_next(stmt_h *handle)
181 retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
183 ret = sqlite3_step(handle->stmt);
186 return MENU_SCREEN_ERROR_OK;
188 return MENU_SCREEN_ERROR_NO_DATA;
190 retv_with_dbmsg_if(1, MENU_SCREEN_ERROR_FAIL);
196 HAPI bool db_get_bool(stmt_h *handle, int index)
198 retv_if(NULL == handle, false);
199 return (bool) sqlite3_column_int(handle->stmt, index);
204 HAPI int db_get_int(stmt_h *handle, int index)
206 retv_if(NULL == handle, 0);
207 return sqlite3_column_int(handle->stmt, index);
212 HAPI long long db_get_long_long(stmt_h *handle, int index)
214 retv_if(NULL == handle, 0l);
215 return sqlite3_column_int64(handle->stmt, index);
219 HAPI const char *db_get_str(stmt_h *handle, int index)
221 retv_if(NULL == handle, NULL);
222 return (const char *) sqlite3_column_text(handle->stmt, index);
227 HAPI menu_screen_error_e db_reset(stmt_h *handle)
231 retv_if(NULL == handle, MENU_SCREEN_ERROR_INVALID_PARAMETER);
232 retv_if(NULL == handle->stmt, MENU_SCREEN_ERROR_INVALID_PARAMETER);
234 ret = sqlite3_reset(handle->stmt);
235 retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
237 sqlite3_clear_bindings(handle->stmt);
239 return MENU_SCREEN_ERROR_OK;
244 HAPI menu_screen_error_e db_finalize(stmt_h *handle)
248 retv_if(NULL == handle, MENU_SCREEN_ERROR_INVALID_PARAMETER);
249 retv_if(NULL == handle->stmt, MENU_SCREEN_ERROR_INVALID_PARAMETER);
251 ret = sqlite3_finalize(handle->stmt);
252 if(ret != SQLITE_OK) {
253 _E("Cannot finalize handle");
257 return MENU_SCREEN_ERROR_OK;
262 HAPI long long db_last_insert_rowid(void)
264 retv_if(NULL == db_info.db, -1l);
266 long long rowid = sqlite3_last_insert_rowid(db_info.db);
273 HAPI menu_screen_error_e db_exec(const char *query)
275 retv_if(NULL == query, MENU_SCREEN_ERROR_INVALID_PARAMETER);
276 retv_if(NULL == db_info.db, MENU_SCREEN_ERROR_FAIL);
278 stmt_h *handle = db_prepare(query);
279 retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
281 goto_if(MENU_SCREEN_ERROR_FAIL == db_next(handle), ERROR);
282 if (MENU_SCREEN_ERROR_OK != db_finalize(handle)) return MENU_SCREEN_ERROR_FAIL;
284 return MENU_SCREEN_ERROR_OK;
287 if (handle) db_finalize(handle);
288 return MENU_SCREEN_ERROR_FAIL;
293 HAPI void db_close(void)
296 sqlite3_close(db_info.db);
302 HAPI menu_screen_error_e db_begin_transaction(void)
306 ret = sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL);
308 while (SQLITE_BUSY == ret) {
310 ret = sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL);
313 if (SQLITE_OK != ret) {
314 _E("sqlite3_exec() Failed(%d)", ret);
315 return MENU_SCREEN_ERROR_FAIL;
318 return MENU_SCREEN_ERROR_OK;
323 #define MENU_SCREEN_COMMIT_TRY_MAX 3
324 HAPI menu_screen_error_e db_end_transaction(bool success)
331 ret = sqlite3_exec(db_info.db, "COMMIT TRANSACTION", NULL, NULL, &errmsg);
332 if (SQLITE_OK != ret) {
333 _E("sqlite3_exec(COMMIT) Failed(%d, %s)", ret, errmsg);
334 sqlite3_free(errmsg);
336 while (SQLITE_BUSY == ret && i < MENU_SCREEN_COMMIT_TRY_MAX) {
339 ret = sqlite3_exec(db_info.db, "COMMIT TRANSACTION", NULL, NULL, NULL);
342 if (SQLITE_OK != ret) {
343 _E("sqlite3_exec() Failed(%d)", ret);
344 ret = sqlite3_exec(db_info.db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
345 if (SQLITE_OK != ret) {
346 _E("sqlite3_exec() Failed(%d)", ret);
349 return MENU_SCREEN_ERROR_FAIL;
353 ret = sqlite3_exec(db_info.db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
354 if (SQLITE_OK != ret) {
355 _E("sqlite3_exec() Faild(%d)", ret);
357 return MENU_SCREEN_ERROR_FAIL;
361 return MENU_SCREEN_ERROR_OK;