Changed data directory for tizen3.0
[apps/native/menu-screen.git] / src / db.c
1 /*
2  * MENU-SCREEN
3  *
4  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Jin Yoon <jinny.yoon@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <db-util.h>
24 #include <sqlite3.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "util.h"
31 #include "db.h"
32
33
34
35 #define retv_with_dbmsg_if(expr, val) do { \
36         if (expr) { \
37                 _E("%s", sqlite3_errmsg(db_info.db)); \
38                 return (val); \
39         } \
40 } while (0)
41
42 #define CREATE_APPS_DB_TABLE "CREATE TABLE IF NOT EXISTS shortcut (\
43         ROWID INTEGER PRIMARY KEY AUTOINCREMENT,\
44         appid TEXT,\
45         name TEXT,\
46         type INTEGER,\
47         content_info TEXT,\
48         icon TEXT );"
49
50
51
52 static struct {
53         sqlite3 *db;
54 } db_info = {
55         .db = NULL,
56 };
57
58 struct stmt {
59         sqlite3_stmt *stmt;
60 };
61
62
63
64 HAPI menu_screen_error_e db_open(const char *db_file)
65 {
66         _D("db open");
67         int ret;
68         char *errMsg;
69         char *res_path = NULL;
70         char db_file_path[1024];
71
72         res_path = app_get_data_path();
73         snprintf(db_file_path, sizeof(db_file_path), "%s%s", res_path, db_file);
74
75         retv_if(NULL == db_file, MENU_SCREEN_ERROR_INVALID_PARAMETER);
76         if (db_info.db) {
77                 _D("db is already exist");
78                 return MENU_SCREEN_ERROR_OK;
79         }
80
81         FILE *fp = fopen(db_file_path, "r");
82         if (fp) {
83                 fclose(fp);
84                 _D("Apps DB[%s] exist", db_file_path);
85                 return MENU_SCREEN_ERROR_OK;
86         }
87
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);
91         }
92
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);
96                 sqlite3_free(errMsg);
97                 return MENU_SCREEN_ERROR_FAIL;
98         }
99
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;
105         }
106         _D("Create DB[%s] : [%s] OK", db_file, CREATE_APPS_DB_TABLE);
107
108         return MENU_SCREEN_ERROR_OK;
109 }
110
111
112
113 HAPI stmt_h *db_prepare(const char *query)
114 {
115         int ret;
116         stmt_h *handle;
117
118         retv_if(NULL == query, NULL);
119
120         handle = calloc(1, sizeof(stmt_h));
121         retv_if(NULL == handle, NULL);
122
123         ret = sqlite3_prepare_v2(db_info.db, query, strlen(query), &(handle->stmt), NULL);
124         if (ret != SQLITE_OK) {
125                 free(handle);
126                 _E("%s", sqlite3_errmsg(db_info.db));
127                 return NULL;
128         }
129
130         return handle;
131 }
132
133
134
135 HAPI menu_screen_error_e db_bind_bool(stmt_h *handle, int idx, bool value)
136 {
137         int ret;
138
139         retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
140
141         ret = sqlite3_bind_int(handle->stmt, idx, (int) value);
142         retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
143
144         return MENU_SCREEN_ERROR_OK;
145 }
146
147
148
149 HAPI menu_screen_error_e db_bind_int(stmt_h *handle, int idx, int value)
150 {
151         int ret;
152
153         retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
154
155         ret = sqlite3_bind_int(handle->stmt, idx, value);
156         retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
157
158         return MENU_SCREEN_ERROR_OK;
159 }
160
161
162
163 HAPI menu_screen_error_e db_bind_str(stmt_h *handle, int idx, const char *str)
164 {
165         int ret;
166
167         retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
168
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);
171
172         return MENU_SCREEN_ERROR_OK;
173 }
174
175
176
177 HAPI menu_screen_error_e db_next(stmt_h *handle)
178 {
179         int ret;
180
181         retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
182
183         ret = sqlite3_step(handle->stmt);
184         switch (ret) {
185                 case SQLITE_ROW:
186                         return MENU_SCREEN_ERROR_OK;
187                 case SQLITE_DONE:
188                         return MENU_SCREEN_ERROR_NO_DATA;
189                 default:
190                         retv_with_dbmsg_if(1, MENU_SCREEN_ERROR_FAIL);
191         }
192 }
193
194
195
196 HAPI bool db_get_bool(stmt_h *handle, int index)
197 {
198         retv_if(NULL == handle, false);
199         return (bool) sqlite3_column_int(handle->stmt, index);
200 }
201
202
203
204 HAPI int db_get_int(stmt_h *handle, int index)
205 {
206         retv_if(NULL == handle, 0);
207         return sqlite3_column_int(handle->stmt, index);
208 }
209
210
211
212 HAPI long long db_get_long_long(stmt_h *handle, int index)
213 {
214         retv_if(NULL == handle, 0l);
215         return sqlite3_column_int64(handle->stmt, index);
216 }
217
218
219 HAPI const char *db_get_str(stmt_h *handle, int index)
220 {
221         retv_if(NULL == handle, NULL);
222         return (const char *) sqlite3_column_text(handle->stmt, index);
223 }
224
225
226
227 HAPI menu_screen_error_e db_reset(stmt_h *handle)
228 {
229         int ret;
230
231         retv_if(NULL == handle, MENU_SCREEN_ERROR_INVALID_PARAMETER);
232         retv_if(NULL == handle->stmt, MENU_SCREEN_ERROR_INVALID_PARAMETER);
233
234         ret = sqlite3_reset(handle->stmt);
235         retv_with_dbmsg_if(ret != SQLITE_OK, MENU_SCREEN_ERROR_FAIL);
236
237         sqlite3_clear_bindings(handle->stmt);
238
239         return MENU_SCREEN_ERROR_OK;
240 }
241
242
243
244 HAPI menu_screen_error_e db_finalize(stmt_h *handle)
245 {
246         int ret;
247
248         retv_if(NULL == handle, MENU_SCREEN_ERROR_INVALID_PARAMETER);
249         retv_if(NULL == handle->stmt, MENU_SCREEN_ERROR_INVALID_PARAMETER);
250
251         ret = sqlite3_finalize(handle->stmt);
252         if(ret != SQLITE_OK) {
253                 _E("Cannot finalize handle");
254         }
255         free(handle);
256
257         return MENU_SCREEN_ERROR_OK;
258 }
259
260
261
262 HAPI long long db_last_insert_rowid(void)
263 {
264         retv_if(NULL == db_info.db, -1l);
265
266         long long rowid = sqlite3_last_insert_rowid(db_info.db);
267
268         return rowid;
269 }
270
271
272
273 HAPI menu_screen_error_e db_exec(const char *query)
274 {
275         retv_if(NULL == query, MENU_SCREEN_ERROR_INVALID_PARAMETER);
276         retv_if(NULL == db_info.db, MENU_SCREEN_ERROR_FAIL);
277
278         stmt_h *handle = db_prepare(query);
279         retv_if(NULL == handle, MENU_SCREEN_ERROR_FAIL);
280
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;
283
284         return MENU_SCREEN_ERROR_OK;
285
286 ERROR:
287         if (handle) db_finalize(handle);
288         return MENU_SCREEN_ERROR_FAIL;
289 }
290
291
292
293 HAPI void db_close(void)
294 {
295         ret_if(!db_info.db);
296         sqlite3_close(db_info.db);
297         db_info.db = NULL;
298 }
299
300
301
302 HAPI menu_screen_error_e db_begin_transaction(void)
303 {
304         int ret = -1;
305
306         ret = sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL);
307
308         while (SQLITE_BUSY == ret) {
309                 sleep(1);
310                 ret = sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL);
311         }
312
313         if (SQLITE_OK != ret) {
314                 _E("sqlite3_exec() Failed(%d)", ret);
315                 return MENU_SCREEN_ERROR_FAIL;
316         }
317
318         return MENU_SCREEN_ERROR_OK;
319 }
320
321
322
323 #define MENU_SCREEN_COMMIT_TRY_MAX 3
324 HAPI menu_screen_error_e db_end_transaction(bool success)
325 {
326         int ret = -1;
327         int i = 0;
328         char *errmsg = NULL;
329
330         if (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);
335
336                         while (SQLITE_BUSY == ret && i < MENU_SCREEN_COMMIT_TRY_MAX) {
337                                 i++;
338                                 sleep(1);
339                                 ret = sqlite3_exec(db_info.db, "COMMIT TRANSACTION", NULL, NULL, NULL);
340                         }
341
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);
347                                 }
348
349                                 return MENU_SCREEN_ERROR_FAIL;
350                         }
351                 }
352         } else {
353                 ret = sqlite3_exec(db_info.db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
354                 if (SQLITE_OK != ret) {
355                         _E("sqlite3_exec() Faild(%d)", ret);
356
357                         return MENU_SCREEN_ERROR_FAIL;
358                 }
359         }
360
361         return MENU_SCREEN_ERROR_OK;
362 }
363
364
365
366 // End of file.