Merge with chun and jin
[apps/native/sample/adventure.git] / src / db.c
index 6825022..f9ca46b 100644 (file)
--- a/src/db.c
+++ b/src/db.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <app_common.h>
+#include <time.h>
 
 #include "db.h"
 #include "log.h"
@@ -158,6 +159,23 @@ HAPI int db_bind_int(sqlite3 *db, sqlite3_stmt *stmt, int idx, int value)
 
 
 
+HAPI int db_bind_long(sqlite3 *db, sqlite3_stmt *stmt, int idx, long value)
+{
+       int ret = SQLITE_OK;
+
+       retv_if(!stmt, APPL_ERROR_FAIL);
+
+       ret = sqlite3_bind_int64(stmt, idx, value);
+       if (SQLITE_OK != ret) {
+               _E("%s", sqlite3_errmsg(db));
+               return APPL_ERROR_FAIL;
+       }
+
+       return APPL_ERROR_NONE;
+}
+
+
+
 HAPI int db_bind_double(sqlite3 *db, sqlite3_stmt *stmt, int idx, double value)
 {
        int ret = SQLITE_OK;
@@ -213,6 +231,14 @@ HAPI int db_get_int(sqlite3_stmt *stmt, int index)
 
 
 
+HAPI int db_get_long(sqlite3_stmt *stmt, int index)
+{
+       retv_if(!stmt, 0);
+       return sqlite3_column_int64(stmt, index);
+}
+
+
+
 HAPI int db_get_double(sqlite3_stmt *stmt, int index)
 {
        retv_if(!stmt, 0);
@@ -442,20 +468,24 @@ error:
 
 
 
-HAPI int db_insert_group(sqlite3 *db, const char *title, int city1, int city2, int city3, int city4, int city5)
+HAPI long 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 (DATETIME('now'), ?, ?, ?, ?, ?, ?);";
+       const char *const QUERY_INSERT = "INSERT INTO group (time, title, city1, city2, city3, city4, city5) VALUES (?, ?, ?, ?, ?, ?, ?);";
        sqlite3_stmt *st = NULL;
+       time_t t;
 
-       st = db_prepare(db, QUERY_INSERT);
-       retv_if(!st, APPL_ERROR_FAIL);
+       time(&t);
 
-       goto_if(db_bind_str(db, st, 1, title) != APPL_ERROR_NONE, error);
-       goto_if(db_bind_int(db, st, 2, city1) != APPL_ERROR_NONE, error);
-       goto_if(db_bind_int(db, st, 3, city2) != APPL_ERROR_NONE, error);
-       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);
+       st = db_prepare(db, QUERY_INSERT);
+       retv_if(!st, 0l);
+
+       goto_if(db_bind_long(db, st, 1, (long) t) != 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);
+       goto_if(db_bind_int(db, st, 5, city3) != APPL_ERROR_NONE, error);
+       goto_if(db_bind_int(db, st, 6, city4) != APPL_ERROR_NONE, error);
+       goto_if(db_bind_int(db, st, 7, city5) != APPL_ERROR_NONE, error);
        goto_if(db_next(db, st) == -1, error);
 
        db_reset(db, st);
@@ -463,11 +493,11 @@ HAPI int db_insert_group(sqlite3 *db, const char *title, int city1, int city2, i
 
        /* keep the sticker panel DB opened */
 
-       return APPL_ERROR_NONE;
+       return t;
 
 error:
        db_finalize(db, st);
-       return APPL_ERROR_FAIL;
+       return 0l;
 }
 
 
@@ -528,19 +558,6 @@ error:
 
 
 
-struct _group_s {
-       char *title;
-       int time;
-       int city1;
-       int city2;
-       int city3;
-       int city4;
-       int city5;
-};
-typedef struct _group_s group_s;
-
-
-
 HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit)
 {
        const char *const QUERY_LIST = "SELECT time, title, city1, city2, city3, city4, city5 FROM group ORDER BY time ASC";
@@ -553,7 +570,7 @@ HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit)
        retv_if(!st, APPL_ERROR_FAIL);
 
        do {
-               int time = 0;
+               long time = 0l;
                const char *title = NULL;
                int city1 = 0, city2 = 0, city3 = 0, city4 = 0, city5 = 0;
                ret = db_next(db, st);
@@ -564,7 +581,7 @@ HAPI int db_list_group(sqlite3 *db, Eina_List **group_list, int limit)
                        goto error;
                }
 
-               time = db_get_int(st, 0);
+               time = db_get_long(st, 0);
                title = db_get_str(st, 1);
                city1 = db_get_int(st, 2);
                city2 = db_get_int(st, 3);