/* * * Copyright 2012 Samsung Electronics Co., Ltd * * Licensed under the Flora License, Version 1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://floralicense.org/license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include "db-define.h" #include "db-schema.h" #include "db.h" /********************************************************************** ******************define, struct ,typedef, union, enum, global val ************************************* ***********************************************************************/ #define QUERY_MAXLEN (4096) #define TEXT(s, n) (char *)sqlite3_column_text(s, n) #define INT(s, n) sqlite3_column_int(s, n) // #define ADLIST_ADD_AFTER(place, elem) {\ if ((place) == NULL) {\ (place) = (elem);\ (place)->next = (place)->prev = NULL;\ } else{\ (elem)->next = (place)->next;\ (elem)->prev = (place);\ (place)->next = (elem);\ if ((elem)->next != NULL)\ (elem)->next->prev = (elem);\ } \ } #define ADLIST_GET_FIRST(list, result) {\ if ((list) != NULL) {\ for ( ; (list)->prev != NULL; (list) = (list)->prev) ;\ } \ (result) = (list);\ } /********************************************************************** ******************Local function declear, extern function declear************************************* ***********************************************************************/ /********************************************************************** ******************Global val , static global val************************************* ***********************************************************************/ static const char *empty_str = ""; /********************************************************************** ******************Local function ref************************************* ***********************************************************************/ // static char *_s(char *str) { char *t; retv_if(str == NULL, (char *)empty_str); t = str; while (*t) { if (*t == '\"') *t = '\''; t++; } return str; } // static int _exec(sqlite3 * db, char * query) { int rc; char *errmsg = NULL; retvm_if(db == NULL, -1, "DB handler is NULL"); rc = sqlite3_exec(db, query, NULL, 0, &errmsg); if (rc != SQLITE_OK) { DB_INFO("Query: [%s]", query); DB_INFO_RED("SQL error: %s\n", errmsg); sqlite3_free(errmsg); return -1; } return 0; } // static int _create_table(sqlite3 * db) { int rc; rc = _exec(db, CREATE_ALARM_TABLE); retv_if(rc == -1, -1); return 0; } // static int _get_ad(sqlite3 * db, int cid, struct alarm_data * ad) { int rc; char query[QUERY_MAXLEN] = { 0, }; sqlite3_stmt *stmt; int idx; snprintf(query, sizeof(query), "select " "magic, alarm_mgr_id, enable, missed, author, name, " "stime, atime, etime, sdate, edate, timezone, " "repeat_once, repeat_every, repeat_weekly, " "snooze_enable, snooze_min, snooze_times, count," "type, tone, volume, auto_power_on " "from alarm where id = %d", cid); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: SQLITE_OK _get_ad(). Get data error"); retvm_if(stmt == NULL, -1, "DB: _get_ad(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) { idx = 0; ad->_magic = INT(stmt, idx++); ad->alarm_mgr_id = INT(stmt, idx++); ad->enable = INT(stmt, idx++); ad->missed = INT(stmt, idx++); ad->author = INT(stmt, idx++); strncpy(ad->name, _s(TEXT(stmt, idx++)), sizeof(ad->name) - 1); ad->stime = INT(stmt, idx++); ad->atime = INT(stmt, idx++); ad->etime = INT(stmt, idx++); ad->sdate = INT(stmt, idx++); ad->edate = INT(stmt, idx++); strncpy(ad->timezone, _s(TEXT(stmt, idx++)), sizeof(ad->timezone) - 1); ad->repeat_once = INT(stmt, idx++); ad->repeat_every = INT(stmt, idx++); ad->repeat_weekly = INT(stmt, idx++); ad->snooze_enable = INT(stmt, idx++); ad->snooze_min = INT(stmt, idx++); ad->snooze_times = INT(stmt, idx++); ad->count = INT(stmt, idx++); ad->type = INT(stmt, idx++); strncpy(ad->tone, _s(TEXT(stmt, idx++)), sizeof(ad->tone) - 1); ad->volume = INT(stmt, idx++); ad->auto_power_on = INT(stmt, idx++); } else { retvm_if(1, -1, "Alarm data %d does not exist", cid); } rc = sqlite3_finalize(stmt); return 0; } // static int _get_ad_by_author(sqlite3 * db, int cid, struct alarm_data * ad, char author) { int rc; char query[QUERY_MAXLEN] = { 0, }; sqlite3_stmt *stmt; int idx; snprintf(query, sizeof(query), "select " "magic, alarm_mgr_id, enable, missed, author, name, " "stime, atime, etime, sdate, edate, timezone, " "repeat_once, repeat_every, repeat_weekly, " "snooze_enable, snooze_min, snooze_times, count," "type, tone, volume, auto_power_on " "from alarm where id = ( select id = %d from alarm where author = %d)", cid, author); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: SQLITE_OK _get_ad_by_author(). Get data error"); retvm_if(stmt == NULL, -1, "DB: _get_ad_by_author(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) { idx = 0; ad->_magic = INT(stmt, idx++); ad->alarm_mgr_id = INT(stmt, idx++); ad->enable = INT(stmt, idx++); ad->missed = INT(stmt, idx++); ad->author = INT(stmt, idx++); strncpy(ad->name, _s(TEXT(stmt, idx++)), sizeof(ad->name) - 1); ad->stime = INT(stmt, idx++); ad->atime = INT(stmt, idx++); ad->etime = INT(stmt, idx++); ad->sdate = INT(stmt, idx++); ad->edate = INT(stmt, idx++); strncpy(ad->timezone, _s(TEXT(stmt, idx++)), sizeof(ad->timezone) - 1); ad->repeat_once = INT(stmt, idx++); ad->repeat_every = INT(stmt, idx++); ad->repeat_weekly = INT(stmt, idx++); ad->snooze_enable = INT(stmt, idx++); ad->snooze_min = INT(stmt, idx++); ad->snooze_times = INT(stmt, idx++); ad->count = INT(stmt, idx++); ad->type = INT(stmt, idx++); strncpy(ad->tone, _s(TEXT(stmt, idx++)), sizeof(ad->tone) - 1); ad->volume = INT(stmt, idx++); ad->auto_power_on = INT(stmt, idx++); } else { retvm_if(1, -1, "Alarm data %d does not exist", cid); } rc = sqlite3_finalize(stmt); return 0; } // static inline void _make_qry_i_ad(char *query, int len, struct alarm_data *ad) { snprintf(query, len, "insert into alarm (" "magic, alarm_mgr_id, enable, missed, author, name, " "stime, atime, etime, sdate, edate, timezone, " "repeat_once, repeat_every, repeat_weekly, " "snooze_enable, snooze_min, snooze_times, count," "type, tone, volume, auto_power_on) values ( " "\"%d\", \"%d\", \"%d\", \"%d\", \"%d\", \"%s\", " "\"%d\", \"%d\", \"%d\", \"%d\", \"%d\", \"%s\", " "\"%d\", \"%d\", \"%d\", " "\"%d\", \"%d\", \"%d\", \"%d\"," "\"%d\", \"%s\", \"%d\", \"%d\")", ad->_magic, ad->alarm_mgr_id, ad->enable, ad->missed, ad->author, _s(ad->name), (int)ad->stime, (int)ad->atime, (int)ad->etime, (int)ad->sdate, (int)ad->edate, _s(ad->timezone), ad->repeat_once, ad->repeat_every, ad->repeat_weekly, ad->snooze_enable, ad->snooze_min, ad->snooze_times, ad->count, ad->type, _s(ad->tone), ad->volume, ad->auto_power_on); } // static inline void _make_qry_u_ad(char *query, int len, struct alarm_data *ad) { snprintf(query, len, "update alarm set " "alarm_mgr_id = \"%d\", enable = \"%d\", missed = \"%d\",author = \"%d\", name = \"%s\", " "stime = \"%d\", atime = \"%d\", etime = \"%d\", sdate = \"%d\", edate = \"%d\", timezone = \"%s\", " "repeat_once = \"%d\", repeat_every = \"%d\", repeat_weekly = \"%d\", " "snooze_enable = \"%d\", snooze_min = \"%d\", snooze_times = \"%d\", count = \"%d\"," "type = \"%d\", tone = \"%s\", volume = \"%d\", auto_power_on = \"%d\" " "where id = %d", ad->alarm_mgr_id, ad->enable, ad->missed, ad->author, _s(ad->name), (int)ad->stime, (int)ad->atime, (int)ad->etime, (int)ad->sdate, (int)ad->edate, _s(ad->timezone), ad->repeat_once, ad->repeat_every, ad->repeat_weekly, ad->snooze_enable, ad->snooze_min, ad->snooze_times, ad->count, ad->type, _s(ad->tone), ad->volume, ad->auto_power_on, ad->id); } // static inline void _make_qry_u_enable(char *query, int len, int id, bool enable) { snprintf(query, len, "update alarm set " "enable = \"%d\" where id = %d", enable, id); } // static inline void _make_qry_u_snooze(char *query, int len, int id, bool enable) { snprintf(query, len, "update alarm set " "snooze_enable = \"%d\" where id = %d", enable, id); } /********************************************************************** ******************Global function ref************************************* ***********************************************************************/ // sqlite3 *db_init(char *root) { int rc; sqlite3 *db = NULL; rc = db_util_open(root, &db, 0); // sqlite3_open(root, &db); printf("db_util_open, rc=%d\n", rc); if (rc) { DB_INFO_RED("Can't open database: %s", sqlite3_errmsg(db)); db_util_close(db); // sqlite3_close(db); return NULL; } rc = _create_table(db); printf("_create_table, rc=%d\n", rc); if (rc) { DB_INFO_RED("Can't create table: %s", sqlite3_errmsg(db)); db_util_close(db); // sqlite3_close(db); return NULL; } return db; } // void db_fini(sqlite3 *db) { if (db) { db_util_close(db); // sqlite3_close(db); } } // int insert_data(sqlite3 *db, struct alarm_data *ad) { int rc = 0; char query[QUERY_MAXLEN] = { 0, }; retvm_if(!db, -1, "DB handler is NULL\n"); retvm_if(!ad, -1, "alarm data is NULL\n"); retvm_if(!MAGIC_VALUE_CHECK(ad->_magic), -1, "alarm data is error, ad->_magic=%d,ALARM_DB_MAGIC_VALUE=%d\n", ad->_magic, ALARM_DB_MAGIC_VALUE); _make_qry_i_ad(query, sizeof(query), ad); rc = _exec(db, query); retv_if(rc == -1, rc); ad->id = (int)sqlite3_last_insert_rowid(db); return ad->id; } // int update_data(sqlite3 *db, struct alarm_data *ad) { int rc; char query[QUERY_MAXLEN] = { 0, }; retvm_if(!db, -1, "DB handler is NULL\n"); retvm_if(!ad, -1, "alarm data is NULL\n"); retvm_if(!MAGIC_VALUE_CHECK(ad->_magic), -1, "alarm data is error, ad->_magic=%d,ALARM_DB_MAGIC_VALUE=%d\n", ad->_magic, ALARM_DB_MAGIC_VALUE); _make_qry_u_ad(query, sizeof(query), ad); rc = _exec(db, query); retv_if(rc == -1, rc); return 0; } // int update_enable(sqlite3 *db, int id, bool enable) { int rc; char query[QUERY_MAXLEN] = { 0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); _make_qry_u_enable(query, sizeof(query), id, enable); rc = _exec(db, query); retv_if(rc == -1, rc); return 0; } // int update_snooze(sqlite3 *db, int id, bool enable) { int rc; char query[QUERY_MAXLEN] = { 0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); _make_qry_u_snooze(query, sizeof(query), id, enable); rc = _exec(db, query); retv_if(rc == -1, rc); return 0; } // int remove_data(sqlite3 *db, int id) { int rc; char query[QUERY_MAXLEN] = { 0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); snprintf(query, sizeof(query), "delete from alarm where id = %d", id); rc = _exec(db, query); retv_if(rc == -1, rc); return 0; } // int remove_data_all(sqlite3 *db) { int rc; char query[QUERY_MAXLEN] = { 0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); snprintf(query, sizeof(query), "delete from alarm"); rc = _exec(db, query); retv_if(rc == -1, rc); return 0; } // int get_data(sqlite3 *db, int cid, struct alarm_data *ad) { int rc; //char query[QUERY_MAXLEN] = {0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); retvm_if(cid < 1, -1, "Invalid argument: alarm id is NULL\n"); retvm_if(ad == NULL, -1, "alarm data is NULL\n"); rc = _get_ad(db, cid, ad); retv_if(rc == -1, rc); ad->id = cid; return 0; } // int get_data_by_author(sqlite3 *db, int cid, struct alarm_data *ad, char author) { int rc; // char query[QUERY_MAXLEN] = {0, }; retvm_if(db == NULL, -1, "DB handler is NULL\n"); retvm_if(cid < 1, -1, "Invalid argument: alarm id is NULL\n"); retvm_if(ad == NULL, -1, "alarm data is NULL\n"); rc = _get_ad_by_author(db, cid, ad, author); retv_if(rc == -1, rc); ad->id = cid; return 0; } // struct alarm_data_list *get_data_list_all(sqlite3 *db) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN]; struct alarm_data_list *adl = NULL; struct alarm_data_list *t; struct alarm_data_list *first; int idx; retvm_if(db == NULL, adl, "DB handler is NULL"); snprintf(query, sizeof(query), "select " "id, magic, alarm_mgr_id, enable, missed, author, name, " "stime, atime, etime, sdate, edate, timezone, " "repeat_once, repeat_every, repeat_weekly, " "snooze_enable, snooze_min, snooze_times, count, " "type, tone, volume, auto_power_on " "from alarm order by id"); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, adl, "DB: get_data_list_all(). Get data error"); retvm_if(stmt == NULL, adl, "DB: get_data_list_all(). Get data error"); rc = sqlite3_step(stmt); while (rc == SQLITE_ROW) { idx = 0; t = (struct alarm_data_list *) malloc(sizeof(struct alarm_data_list)); retvm_if(!t, adl, "malloc null"); t->ad.id = INT(stmt, idx++); t->ad._magic = INT(stmt, idx++); t->ad.alarm_mgr_id = INT(stmt, idx++); t->ad.enable = INT(stmt, idx++); t->ad.missed = INT(stmt, idx++); t->ad.author = INT(stmt, idx++); strncpy(t->ad.name, _s(TEXT(stmt, idx++)), sizeof(t->ad.name) - 1); t->ad.stime = INT(stmt, idx++); t->ad.atime = INT(stmt, idx++); t->ad.etime = INT(stmt, idx++); t->ad.sdate = INT(stmt, idx++); t->ad.edate = INT(stmt, idx++); strncpy(t->ad.timezone, _s(TEXT(stmt, idx++)), sizeof(t->ad.timezone) - 1); t->ad.repeat_once = INT(stmt, idx++); t->ad.repeat_every = INT(stmt, idx++); t->ad.repeat_weekly = INT(stmt, idx++); t->ad.snooze_enable = INT(stmt, idx++); t->ad.snooze_min = INT(stmt, idx++); t->ad.snooze_times = INT(stmt, idx++); t->ad.count = INT(stmt, idx++); t->ad.type = INT(stmt, idx++); strncpy(t->ad.tone, _s(TEXT(stmt, idx++)), sizeof(t->ad.tone) - 1); t->ad.volume = INT(stmt, idx++); t->ad.auto_power_on = INT(stmt, idx++); t->next = NULL; ADLIST_ADD_AFTER(adl, t); rc = sqlite3_step(stmt); } rc = sqlite3_finalize(stmt); ADLIST_GET_FIRST(adl, first); return first; } // struct alarm_data_list *get_data_list_by_author(sqlite3 *db, char author) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN]; struct alarm_data_list *adl = NULL; struct alarm_data_list *t; struct alarm_data_list *first; int idx; retvm_if(db == NULL, adl, "DB handler is NULL"); snprintf(query, sizeof(query), "select " "id, magic, alarm_mgr_id, enable, missed, name, " "stime, atime, etime, sdate, edate, timezone, " "repeat_once, repeat_every, repeat_weekly, " "snooze_enable, snooze_min, snooze_times, count, " "type, tone, volume, auto_power_on " "from alarm where author = %d order by id", author); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, adl, "DB: Get data list error"); retvm_if(stmt == NULL, adl, "DB: Get data list error"); rc = sqlite3_step(stmt); while (rc == SQLITE_ROW) { idx = 0; t = (struct alarm_data_list *) malloc(sizeof(struct alarm_data_list)); retvm_if(!t, adl, "malloc null"); t->ad.id = INT(stmt, idx++); t->ad._magic = INT(stmt, idx++); t->ad.alarm_mgr_id = INT(stmt, idx++); t->ad.enable = INT(stmt, idx++); t->ad.missed = INT(stmt, idx++); strncpy(t->ad.name, _s(TEXT(stmt, idx++)), sizeof(t->ad.name) - 1); t->ad.stime = INT(stmt, idx++); t->ad.atime = INT(stmt, idx++); t->ad.etime = INT(stmt, idx++); t->ad.sdate = INT(stmt, idx++); t->ad.edate = INT(stmt, idx++); strncpy(t->ad.timezone, _s(TEXT(stmt, idx++)), sizeof(t->ad.timezone) - 1); t->ad.repeat_once = INT(stmt, idx++); t->ad.repeat_every = INT(stmt, idx++); t->ad.repeat_weekly = INT(stmt, idx++); t->ad.snooze_enable = INT(stmt, idx++); t->ad.snooze_min = INT(stmt, idx++); t->ad.snooze_times = INT(stmt, idx++); t->ad.count = INT(stmt, idx++); t->ad.type = INT(stmt, idx++); strncpy(t->ad.tone, _s(TEXT(stmt, idx++)), sizeof(t->ad.tone) - 1); t->ad.volume = INT(stmt, idx++); t->ad.auto_power_on = INT(stmt, idx++); t->next = NULL; t->ad.author = author; ADLIST_ADD_AFTER(adl, t); rc = sqlite3_step(stmt); } rc = sqlite3_finalize(stmt); ADLIST_GET_FIRST(adl, first); return first; } // int get_num_of_enable(sqlite3 *db) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN] = { 0, }; int num_of_enable = 0; snprintf(query, sizeof(query), "select count(*) from alarm where enable = 1"); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: get_num_of_enable(). Get data error"); retvm_if(stmt == NULL, -1, "DB: get_num_of_enable(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) num_of_enable = INT(stmt, 0); rc = sqlite3_finalize(stmt); return num_of_enable; } // int get_last_id(sqlite3 *db) { return (int)sqlite3_last_insert_rowid(db); } // int get_last_id_by_author(sqlite3 *db, char author) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN] = { 0, }; int id = 0; snprintf(query, sizeof(query), "select max(id) from alarm where author = %d", author); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: get_last_id_by_author(). Get data error"); retvm_if(stmt == NULL, -1, "DB: get_last_id_by_author(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) id = INT(stmt, 0); rc = sqlite3_finalize(stmt); return id; } // int get_number_of_data_by_author(sqlite3 *db, char author) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN] = { 0, }; int id = 0; snprintf(query, sizeof(query), "select count(1) from alarm where author = %d", author); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: get_last_id_by_author(). Get data error"); retvm_if(stmt == NULL, -1, "DB: get_last_id_by_author(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) id = INT(stmt, 0); rc = sqlite3_finalize(stmt); return id; } // int get_power_onoff_by_author(sqlite3 *db, char author) { int rc; sqlite3_stmt *stmt; char query[QUERY_MAXLEN] = { 0, }; int id = 0; snprintf(query, sizeof(query), "select auto_power_on from alarm where author=%d", author); rc = sqlite3_prepare(db, query, -1, &stmt, NULL); retvm_if(rc != SQLITE_OK, -1, "DB: get_poweron_by_author(). Get data error"); retvm_if(stmt == NULL, -1, "DB: get_poweron_by_author(). Get data error"); rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) id = INT(stmt, 0); rc = sqlite3_finalize(stmt); return id; }