#include <string.h>
#include <unistd.h>
#include <app_common.h>
-#include <time.h>
#include "db.h"
#include "log.h"
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));
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;
}
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;
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;
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);
-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);
/* 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);
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);
-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);
-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;
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);
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);
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);