X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fdb.c;h=c7ee7b83742c576c3f6d8173a33e4b3aacdde0f0;hb=fc3c77853088c20529172d99664a89dd1c66d89f;hp=f9ca46b7984ee52c456a7eb1f3601353a5fe09df;hpb=707457edf2b9a3157f747423c1f5523ee0c00684;p=apps%2Fnative%2Fsample%2Fadventure.git diff --git a/src/db.c b/src/db.c index f9ca46b..c7ee7b8 100644 --- a/src/db.c +++ b/src/db.c @@ -22,7 +22,6 @@ #include #include #include -#include #include "db.h" #include "log.h" @@ -47,6 +46,7 @@ HAPI sqlite3 *db_open(void) snprintf(db_file, sizeof(db_file), "%s/%s", path, APP_DB_FILE); free(path); + _D("db_file : %s", db_file); ret = sqlite3_open(db_file, &db); if (SQLITE_OK != ret) { _E("%s", sqlite3_errmsg(db)); @@ -93,15 +93,15 @@ HAPI int db_next(sqlite3 *db, sqlite3_stmt *stmt) ret = sqlite3_step(stmt); switch (ret) { case SQLITE_ROW: /* SQLITE_ROW : 100 */ - return APPL_ERROR_NONE; + return SQLITE_ROW; case SQLITE_DONE: /* SQLITE_ROW : 101 */ - return APPL_ERROR_NO_DATA; + return SQLITE_DONE; default: _E("%s", sqlite3_errmsg(db)); - return APPL_ERROR_FAIL; + return -1; } - return APPL_ERROR_NONE; + return -1; } @@ -296,15 +296,15 @@ ERROR: HAPI int db_create_table(sqlite3 *db) { const char *TABLES[] = { - "CREATE TABLE IF NOT EXIST db_checksum (version INT);", - "CREATE TABLE IF NOT EXIST group (" - "time INTEGER PRIMARY KEY" + "CREATE TABLE IF NOT EXISTS db_checksum (version INT);", + "CREATE TABLE IF NOT EXISTS gr (" + "id INTEGER PRIMARY KEY AUTOINCREMENT" ", title TEXT" - ", city1 INT" - ", city2 INT" - ", city3 INT" - ", city4 INT" - ", city5 INT" + ", city1 INTEGER" + ", city2 INTEGER" + ", city3 INTEGER" + ", city4 INTEGER" + ", city5 INTEGER" ");", }; int count = 0; @@ -324,8 +324,8 @@ HAPI int db_create_table(sqlite3 *db) HAPI int db_drop_table(sqlite3 *db) { const char *TABLES[] = { - "DROP TABLE IF EXIST db_checksum;", - "DROP TABLE IF EXIST group;", + "DROP TABLE IF EXISTS db_checksum;", + "DROP TABLE IF EXISTS gr;", }; int count = 0; int i = 0; @@ -444,7 +444,7 @@ HAPI int db_count_version(sqlite3 *db) HAPI int db_count_group(sqlite3 *db, int *count) { - const char *const QUERY_SYNTAX = "SELECT COUNT(*) FROM group;"; + const char *const QUERY_SYNTAX = "SELECT COUNT(*) FROM gr;"; sqlite3_stmt *st = NULL; st = db_prepare(db, QUERY_SYNTAX); @@ -468,18 +468,45 @@ error: -HAPI long db_insert_group(sqlite3 *db, const char *title, int city1, int city2, int city3, int city4, int city5) +HAPI int db_max_group(sqlite3 *db, int *max) +{ + const char *const QUERY_SYNTAX = "SELECT MAX(id) FROM gr;"; + sqlite3_stmt *st = NULL; + + st = db_prepare(db, QUERY_SYNTAX); + retv_if(!st, APPL_ERROR_FAIL); + + goto_if(db_next(db, st) == -1, error); + + *max = db_get_int(st, 0); + + db_reset(db, st); + db_finalize(db, st); + + /* keep this DB opened */ + + return APPL_ERROR_NONE; + +error: + db_finalize(db, st); + return APPL_ERROR_FAIL; +} + + + +HAPI int db_insert_group(sqlite3 *db, const char *title, int city1, int city2, int city3, int city4, int city5) { - const char *const QUERY_INSERT = "INSERT INTO group (time, title, city1, city2, city3, city4, city5) VALUES (?, ?, ?, ?, ?, ?, ?);"; + const char *const QUERY_INSERT = "INSERT INTO gr (id, title, city1, city2, city3, city4, city5) VALUES (?, ?, ?, ?, ?, ?, ?);"; sqlite3_stmt *st = NULL; - time_t t; + int max = 0; - time(&t); + db_max_group(db, &max); + max++; st = db_prepare(db, QUERY_INSERT); - retv_if(!st, 0l); + retv_if(!st, -1); - goto_if(db_bind_long(db, st, 1, (long) t) != APPL_ERROR_NONE, error); + goto_if(db_bind_int(db, st, 1, max) != APPL_ERROR_NONE, error); goto_if(db_bind_str(db, st, 2, title) != APPL_ERROR_NONE, error); goto_if(db_bind_int(db, st, 3, city1) != APPL_ERROR_NONE, error); goto_if(db_bind_int(db, st, 4, city2) != APPL_ERROR_NONE, error); @@ -493,18 +520,18 @@ HAPI long db_insert_group(sqlite3 *db, const char *title, int city1, int city2, /* keep the sticker panel DB opened */ - return t; + return max; error: db_finalize(db, st); - return 0l; + return -1; } -HAPI int db_update_group(sqlite3 *db, int time, const char *title, int city1, int city2, int city3, int city4, int city5) +HAPI int db_update_group(sqlite3 *db, int id, const char *title, int city1, int city2, int city3, int city4, int city5) { - const char *const QUERY_UPDATE = "UPDATE group SET title = ?, city1 = ?, city2 = ?, city3 = ?, city4 = ?, city5 = ? WHERE time = ?;"; + const char *const QUERY_UPDATE = "UPDATE gr SET title = ?, city1 = ?, city2 = ?, city3 = ?, city4 = ?, city5 = ? WHERE id = ?;"; sqlite3_stmt *st = NULL; st = db_prepare(db, QUERY_UPDATE); @@ -516,7 +543,7 @@ HAPI int db_update_group(sqlite3 *db, int time, const char *title, int city1, in goto_if(db_bind_int(db, st, 4, city3) != APPL_ERROR_NONE, error); goto_if(db_bind_int(db, st, 5, city4) != APPL_ERROR_NONE, error); goto_if(db_bind_int(db, st, 6, city5) != APPL_ERROR_NONE, error); - goto_if(db_bind_int(db, st, 7, time) != APPL_ERROR_NONE, error); + goto_if(db_bind_int(db, st, 7, id) != APPL_ERROR_NONE, error); goto_if(db_next(db, st) == -1, error); db_reset(db, st); @@ -533,15 +560,15 @@ error: -HAPI int db_delete_group(sqlite3 *db, int time) +HAPI int db_delete_group(sqlite3 *db, int id) { - const char *const QUERY_SYNTAX = "DELETE FROM group WHERE time = ?;"; + const char *const QUERY_SYNTAX = "DELETE FROM gr WHERE id = ?;"; sqlite3_stmt *st = NULL; st = db_prepare(db, QUERY_SYNTAX); retv_if(!st, APPL_ERROR_FAIL); - goto_if(db_bind_int(db, st, 1, time) != APPL_ERROR_NONE, error); + goto_if(db_bind_int(db, st, 1, id) != APPL_ERROR_NONE, error); goto_if(db_next(db, st) == -1, error); db_reset(db, st); @@ -558,9 +585,9 @@ error: -HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit) +HAPI int db_list_group(sqlite3 *db, Eina_List **group_list) { - const char *const QUERY_LIST = "SELECT time, title, city1, city2, city3, city4, city5 FROM group ORDER BY time ASC"; + const char *const QUERY_LIST = "SELECT id, title, city1, city2, city3, city4, city5 FROM gr ORDER BY id ASC"; sqlite3_stmt *st = NULL; group_info_s *group_info = NULL; @@ -570,7 +597,7 @@ HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit) retv_if(!st, APPL_ERROR_FAIL); do { - long time = 0l; + int id = 0; const char *title = NULL; int city1 = 0, city2 = 0, city3 = 0, city4 = 0, city5 = 0; ret = db_next(db, st); @@ -581,7 +608,7 @@ HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit) goto error; } - time = db_get_long(st, 0); + id = db_get_int(st, 0); title = db_get_str(st, 1); city1 = db_get_int(st, 2); city2 = db_get_int(st, 3); @@ -589,7 +616,7 @@ HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit) city4 = db_get_int(st, 5); city5 = db_get_int(st, 6); - group_info = group_info_create(time, title, city1, city2, city3, city4, city5); + group_info = group_info_create(id, title, city1, city2, city3, city4, city5); continue_if(!group_info); *group_list = eina_list_append(*group_list, group_info);