504421cf2019bbad78c5134062d8ff20fa9979ed
[apps/native/sample/adventure.git] / new / src / db.c
1 /*
2  * Samsung API
3  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <Evas.h>
19 #include <sqlite3.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <app_common.h>
25
26 #include "db.h"
27 #include "log.h"
28 #include "util.h"
29
30 #define APP_DB_FILE ".app.db"
31
32
33
34
35 HAPI sqlite3 *db_open(void)
36 {
37         sqlite3 *db = NULL;
38         char *path = NULL;
39         char db_file[FILE_LEN] = {0, };
40         int ret = SQLITE_OK;
41
42         path = app_get_data_path();
43         retv_if(!path, NULL);
44         
45         snprintf(db_file, sizeof(db_file), "%s/%s", path, APP_DB_FILE);
46         free(path);
47
48         ret = sqlite3_open(db_file, &db);
49         if (SQLITE_OK != ret) {
50                 _E("%s", sqlite3_errmsg(db));
51                 return NULL;
52         }
53
54         return db;
55 }
56
57
58
59 HAPI void db_close(sqlite3 *db)
60 {
61         ret_if(!db);
62         sqlite3_close(db);
63 }
64
65
66
67 HAPI sqlite3_stmt *db_prepare(sqlite3 *db, const char *query)
68 {
69         sqlite3_stmt *stmt = NULL;
70         int ret = SQLITE_OK;
71
72         retv_if(!query, NULL);
73
74         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
75         if (SQLITE_OK != ret) {
76                 _E("%s, %s", query, sqlite3_errmsg(db));
77                 return NULL;
78         }
79
80         return stmt;
81 }
82
83
84
85 HAPI int db_next(sqlite3 *db, sqlite3_stmt *stmt)
86 {
87         int ret = SQLITE_OK;
88
89         retv_if(!stmt, APPL_ERROR_FAIL);
90
91         ret = sqlite3_step(stmt);
92         switch (ret) {
93         case SQLITE_ROW: /* SQLITE_ROW : 100 */
94                 return APPL_ERROR_NONE;
95         case SQLITE_DONE: /* SQLITE_ROW : 101 */
96                 return APPL_ERROR_NO_DATA;
97         default:
98                 _E("%s", sqlite3_errmsg(db));
99                 return APPL_ERROR_FAIL;
100         }
101
102         return APPL_ERROR_NONE;
103 }
104
105
106
107 HAPI int db_reset(sqlite3 *db, sqlite3_stmt *stmt)
108 {
109         int ret = SQLITE_OK;
110
111         retv_if(!stmt, APPL_ERROR_INVALID_PARAMETER);
112
113         ret = sqlite3_reset(stmt);
114         if (SQLITE_OK != ret) {
115                 _E("%s", sqlite3_errmsg(db));
116                 return APPL_ERROR_FAIL;
117         }
118
119         sqlite3_clear_bindings(stmt);
120
121         return APPL_ERROR_NONE;
122 }
123
124
125
126 HAPI int db_bind_bool(sqlite3 *db, sqlite3_stmt *stmt, int idx, bool value)
127 {
128         int ret = SQLITE_OK;
129
130         retv_if(!stmt, APPL_ERROR_FAIL);
131
132         ret = sqlite3_bind_int(stmt, idx, (int) value);
133         if (SQLITE_OK != ret) {
134                 _E("%s", sqlite3_errmsg(db));
135                 return APPL_ERROR_FAIL;
136         }
137
138         return APPL_ERROR_NONE;
139 }
140
141
142
143 HAPI int db_bind_int(sqlite3 *db, sqlite3_stmt *stmt, int idx, int value)
144 {
145         int ret = SQLITE_OK;
146
147         retv_if(!stmt, APPL_ERROR_FAIL);
148
149         ret = sqlite3_bind_int(stmt, idx, value);
150         if (SQLITE_OK != ret) {
151                 _E("%s", sqlite3_errmsg(db));
152                 return APPL_ERROR_FAIL;
153         }
154
155         return APPL_ERROR_NONE;
156 }
157
158
159
160 HAPI int db_bind_double(sqlite3 *db, sqlite3_stmt *stmt, int idx, double value)
161 {
162         int ret = SQLITE_OK;
163
164         retv_if(!stmt, APPL_ERROR_FAIL);
165
166         ret = sqlite3_bind_double(stmt, idx, value);
167         if (SQLITE_OK != ret) {
168                 _E("%s", sqlite3_errmsg(db));
169                 return APPL_ERROR_FAIL;
170         }
171
172         return APPL_ERROR_NONE;
173 }
174
175
176
177 HAPI int db_bind_str(sqlite3 *db, sqlite3_stmt *stmt, int idx, const char *str)
178 {
179         int ret = SQLITE_OK;
180
181         retv_if(!stmt, APPL_ERROR_FAIL);
182
183         if (str) {
184                 ret = sqlite3_bind_text(stmt, idx, str, -1, SQLITE_TRANSIENT);
185         } else {
186                 ret = sqlite3_bind_null(stmt, idx);
187         }
188
189         if (SQLITE_OK != ret) {
190                 _E("%s", sqlite3_errmsg(db));
191                 return APPL_ERROR_FAIL;
192         }
193
194         return APPL_ERROR_NONE;
195 }
196
197
198
199 HAPI bool db_get_bool(sqlite3_stmt *stmt, int index)
200 {
201         retv_if(!stmt, false);
202         return (bool) (!!sqlite3_column_int(stmt, index));
203 }
204
205
206
207 HAPI int db_get_int(sqlite3_stmt *stmt, int index)
208 {
209         retv_if(!stmt, 0);
210         return sqlite3_column_int(stmt, index);
211 }
212
213
214
215 HAPI int db_get_double(sqlite3_stmt *stmt, int index)
216 {
217         retv_if(!stmt, 0);
218         return sqlite3_column_double(stmt, index);
219 }
220
221
222
223 HAPI const char *db_get_str(sqlite3_stmt *stmt, int index)
224 {
225         retv_if(!stmt, NULL);
226         return (const char *) sqlite3_column_text(stmt, index);
227 }
228
229
230
231 HAPI int db_finalize(sqlite3 *db, sqlite3_stmt *stmt)
232 {
233         int ret = SQLITE_OK;
234
235         retv_if(!stmt, APPL_ERROR_INVALID_PARAMETER);
236
237         ret = sqlite3_finalize(stmt);
238         if (SQLITE_OK != ret) {
239                 _E("%s", sqlite3_errmsg(db));
240                 return APPL_ERROR_FAIL;
241         }
242
243         return APPL_ERROR_NONE;
244 }
245
246
247
248 HAPI int db_exec(sqlite3 *db, const char *query)
249 {
250         sqlite3_stmt *stmt = NULL;
251
252         retv_if(!query, APPL_ERROR_INVALID_PARAMETER);
253
254         stmt = db_prepare(db, query);
255         retv_if(!stmt, APPL_ERROR_FAIL);
256
257         goto_if(APPL_ERROR_FAIL == db_next(db, stmt), ERROR);
258         goto_if(APPL_ERROR_FAIL == db_finalize(db, stmt), ERROR);
259
260         return APPL_ERROR_NONE;
261
262 ERROR:
263         if (stmt) db_finalize(db, stmt);
264         return APPL_ERROR_FAIL;
265 }
266
267
268
269 HAPI int db_create_table(sqlite3 *db)
270 {
271         const char *TABLES[] = {
272                 "CREATE TABLE IF NOT EXIST db_checksum (version INT);",
273                 "CREATE TABLE IF NOT EXIST path_information ("
274                         "path_id INTEGER PRIMARY KEY"
275                         ", user_id INTEGER NOT NULL"
276                         ", latitude DOUBLE NOT NULL"
277                         ", longitude DOUBLE NOT NULL"
278                         ", title TEXT"
279                         ", score INT"
280                         ", price INT"
281                         ", visible BOOL"
282                         ", FOREIGH KEY(user_id) REFERENCES user(user_id)"
283                         ");",
284                 "CREATE TABLE IF NOT EXIST path ("
285                         "path_id INTEGER NOT NULL"
286                         ", order INTEGER NOT NULL"
287                         ", content_id INTENGER NOT NULL"
288                         ", PRIMARY KEY(path_id, order)"
289                         ", FOREIGN KEY(path_id) REFERENCES path(path_id)"
290                         ", FOREIGH KEY(content_id) REFERENCES content(contend_id)",
291                         ");",
292                 "CREATE TABLE IF NOT EXIST content ("
293                         "content_id INTEGER PRIMARY KEY"
294                         ", user_id INTEGER NOT NULL"
295                         ", latitude DOUBLE NOT NULL"
296                         ", longitude DOUBLE NOT NULL"
297                         ", title TEXT"
298                         ", content TEXT"
299                         ", tag TEXT"
300                         ", location TEXT"
301                         ", icon TEXT"
302                         ", attach_id INTEGER"
303                         ", FOREIGN KEY(user_id) REFERENCES user(user_id)"
304                         ", FOREIGN KEY(attach_id) REFERENCES attach(attach_id)"
305                         ");",
306                 "CREATE TABLE IF NOT EXIST attach ("
307                         "attach_id INTEGER PRIMARY KEY"
308                         ", path TEXT NOT NULL"
309                         ", ref_count INT NOT NULL"
310                         ");",
311                 "CREATE TABLE IF NOT EXIST purchase ("
312                         "user_id INTEGER NOT NULL"
313                         ", content_id INTEGER NOT NULL"
314                         ", PRIMARY KEY(user_id, content_id)"
315                         ", FOREIGN KEY(user_id) REFERENCES user(user_id)"
316                         ", FOREIGN KEY(content_id) REFERENCES content(content_id)"
317                         ");",
318                 "CREATE TABLE IF NOT EXIST user ("
319                         "user_id INTEGER PRIMARY KEY"
320                         ", name TEXT"
321                         ");",
322         };
323         int count = 0;
324         int i = 0;
325
326         count = sizeof(TABLES) / sizeof(char *);
327         for (; i < count; i++) {
328                 _D("Create a table[%s]", TABLES[i]);
329                 break_if(db_exec(db, TABLES[i]) != APPL_ERROR_NONE);
330         }
331
332         return APPL_ERROR_FAIL;
333 }
334
335
336
337 HAPI int db_drop_table(sqlite3 *db)
338 {
339         const char *TABLES[] = {
340                 "DROP TABLE IF EXIST db_checksum;",
341                 "DROP TABLE IF EXIST path_information;",
342                 "DROP TABLE IF EXIST path;",
343                 "DROP TABLE IF EXIST content;",
344                 "DROP TABLE IF EXIST attach;",
345                 "DROP TABLE IF EXIST purchase;",
346                 "DROP TABLE IF EXIST user;",
347         };
348         int count = 0;
349         int i = 0;
350
351         count = sizeof(TABLES) / sizeof(char *);
352         for (; i < count; i++) {
353                 _D("Drop a table[%s]", TABLES[i]);
354                 break_if(db_exec(db, TABLES[i]) != APPL_ERROR_NONE);
355         }
356
357         return APPL_ERROR_FAIL;
358 }
359
360
361
362 HAPI int db_insert_version(sqlite3 *db, int version)
363 {
364         const char *QUERY_SYNTAX = "INSERT INTO db_checksum (version) values (%d);";
365         char *query = NULL;
366
367         query = sqlite3_mprintf(QUERY_SYNTAX, version);
368         retv_if(!query, APPL_ERROR_FAIL);
369
370         if (db_exec(db, query) != APPL_ERROR_NONE) {
371                 _E("Cannot execute query.[%s]", query);
372                 sqlite3_free(query);
373                 return APPL_ERROR_FAIL;
374         }
375
376         sqlite3_free(query);
377
378         /* keep the home DB opened */
379
380         return APPL_ERROR_NONE;
381 }
382
383
384
385 HAPI int db_remove_version(sqlite3 *db, int version)
386 {
387         const char *QUERY_SYNTAX = "DELETE FROM db_checksum WHERE version=%d;";
388         char *query = NULL;
389
390         query = sqlite3_mprintf(QUERY_SYNTAX, version);
391         retv_if(!query, APPL_ERROR_FAIL);
392
393         if (db_exec(db, query) != APPL_ERROR_NONE) {
394                 _E("Cannot execute query.[%s]", query);
395                 sqlite3_free(query);
396                 return APPL_ERROR_FAIL;
397         }
398
399         sqlite3_free(query);
400
401         /* keep the home DB opened */
402
403         return APPL_ERROR_NONE;
404 }
405
406
407
408 HAPI int db_update_version(sqlite3 *db, int version)
409 {
410         const char *QUERY_SYNTAX = "UPDATE db_checksum SET version=%d;";
411         char *query = NULL;
412
413         query = sqlite3_mprintf(QUERY_SYNTAX, version);
414         retv_if(!query, APPL_ERROR_FAIL);
415
416         if (db_exec(db, query) != APPL_ERROR_NONE) {
417                 _E("Cannot execute query.[%s]", query);
418                 sqlite3_free(query);
419                 return APPL_ERROR_FAIL;
420         }
421
422         sqlite3_free(query);
423
424         /* keep the home DB opened */
425
426         return APPL_ERROR_NONE;
427 }
428
429
430
431 HAPI int db_count_version(sqlite3 *db)
432 {
433         const char *QUERY_SYNTAX = "SELECT COUNT(*) FROM db_checksum;";
434         sqlite3_stmt *st = NULL;
435         int count = 0;
436
437         st = db_prepare(db, QUERY_SYNTAX);
438         retv_if(!st, APPL_ERROR_FAIL);
439
440         if (db_next(db, st) == APPL_ERROR_FAIL) {
441                 _E("db_next error");
442                 db_finalize(db, st);
443                 return -1;
444         }
445
446         count = db_get_int(st, 0);
447         db_reset(db, st);
448         db_finalize(db, st);
449
450         /* keep the home DB opened */
451
452         return count;
453 }
454
455
456
457 // End of file.