diff options
author | Jin Yoon <jinny.yoon@samsung.com> | 2015-06-02 00:37:05 +0900 |
---|---|---|
committer | Jin Yoon <jinny.yoon@samsung.com> | 2015-06-02 00:37:05 +0900 |
commit | 83e94ac707959f362b2383dc6fda9feb8508b374 (patch) | |
tree | 85fbc9efe8f3320556976add06c3fd86a2c75937 | |
parent | 75eee16357ea847124e41423d31219f8ae7d829b (diff) | |
download | adventure-83e94ac707959f362b2383dc6fda9feb8508b374.tar.gz adventure-83e94ac707959f362b2383dc6fda9feb8508b374.tar.bz2 adventure-83e94ac707959f362b2383dc6fda9feb8508b374.zip |
DB : initial version
Change-Id: I491050cbc73643a11841cde83a777502d6483b4f
-rw-r--r-- | inc/db.h | 55 | ||||
-rw-r--r-- | inc/util.h | 46 | ||||
-rw-r--r-- | src/db.c | 519 |
3 files changed, 620 insertions, 0 deletions
diff --git a/inc/db.h b/inc/db.h new file mode 100644 index 0000000..279a6e3 --- /dev/null +++ b/inc/db.h @@ -0,0 +1,55 @@ +/* + * Samsung API + * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.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. + */ + +#ifndef __ADVENTURE_DB_H__ +#define __ADVENTURE_DB_H__ + +#include <sqlite3.h> +#include "util.h" + +extern appl_error_e db_open(void); +extern void db_close(void); + +extern sqlite3_stmt *db_prepare(const char *query); +extern appl_error_e db_next(sqlite3_stmt *stmt); +extern appl_error_e db_reset(sqlite3_stmt *stmt); + +extern appl_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value); +extern appl_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value); +extern appl_error_e db_bind_double(sqlite3_stmt *stmt, int idx, double value); +extern appl_error_e db_bind_str(sqlite3_stmt *stmt, int idx, const char *str); + +extern bool db_get_bool(sqlite3_stmt *stmt, int index); +extern int db_get_int(sqlite3_stmt *stmt, int index); +extern int db_get_double(sqlite3_stmt *stmt, int index); +extern const char *db_get_str(sqlite3_stmt *stmt, int index); + +extern appl_error_e db_finalize(sqlite3_stmt *stmt); +extern appl_error_e db_exec(const char *query); + +extern appl_error_e db_begin_transaction(void); +extern appl_error_e db_end_transaction(void); + +extern appl_error_e db_create_table(void); +extern appl_error_e db_drop_table(void); + +extern appl_error_e db_insert_version(int version); +extern appl_error_e db_remove_version(int version); +extern appl_error_e db_update_version(int version); +extern appl_error_e db_count_version(void); + +#endif // __ADVENTURE_DB_H__ diff --git a/inc/util.h b/inc/util.h new file mode 100644 index 0000000..042f340 --- /dev/null +++ b/inc/util.h @@ -0,0 +1,46 @@ +/* + * Samsung API + * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.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. + */ + +#ifndef __ADVENTURE_INTERNAL_H__ +#define __ADVENTURE_INTERNAL_H__ + +/* Multi-language */ +#if !defined(_) +#define _(str) gettext(str) +#endif +#define gettext_noop(str) (str) +#define N_(str) gettext_noop(str) +#define D_(str) dgettext("sys_string", str) + +/* SIZE */ +#define FILE_LEN 256 +#define BUFSZE 1024 + +/* Build */ +#define HAPI __attribute__((visibility("hidden"))) + +/* Return values */ +typedef enum { + APPL_ERROR_NONE = 0, + APPL_ERROR_FAIL = -1, + APPL_ERROR_DB_FAILED = -2, + APPL_ERROR_OUT_OF_MEMORY = -3, + APPL_ERROR_INVALID_PARAMETER = -4, + APPL_ERROR_NO_DATA = -5, +} appl_error_e; + +#endif /* __ADVENTURE_INTERNAL_H__ */ diff --git a/src/db.c b/src/db.c new file mode 100644 index 0000000..2ab79e0 --- /dev/null +++ b/src/db.c @@ -0,0 +1,519 @@ +/* + * Samsung API + * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.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 <Evas.h> +#include <sqlite3.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <app_common.h> + +#include "db.h" +#include "log.h" +#include "util.h" + +#define APP_DB_FILE ".app.db" + + + +static struct { + sqlite3 *db; +} db_info = { + .db = NULL, +}; + + + +HAPI appl_error_e db_open(void) +{ + char *path = NULL; + char db_file[FILE_LEN] = {0, }; + int ret = SQLITE_OK; + + path = app_get_data_path(); + retv_if(!path, APPL_ERROR_FAIL); + + snprintf(db_file, sizeof(db_file), "%s/%s", path, APP_DB_FILE); + + ret = sqlite3_open(db_file, &db_info.db); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + free(path); + return APPL_ERROR_FAIL; + } + + free(path); + return APPL_ERROR_NONE; +} + + + +HAPI void db_close(void) +{ + if (!db_info.db) { + _D("DB is already NULL"); + return; + } + + sqlite3_close(db_info.db); + db_info.db = NULL; +} + + + +HAPI sqlite3_stmt *db_prepare(const char *query) +{ + sqlite3_stmt *stmt = NULL; + int ret = SQLITE_OK; + + retv_if(!query, NULL); + + ret = sqlite3_prepare_v2(db_info.db, query, strlen(query), &stmt, NULL); + if (SQLITE_OK != ret) { + _E("%s, %s", query, sqlite3_errmsg(db_info.db)); + return NULL; + } + + return stmt; +} + + + +HAPI appl_error_e db_next(sqlite3_stmt *stmt) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_FAIL); + + ret = sqlite3_step(stmt); + switch (ret) { + case SQLITE_ROW: + return APPL_ERROR_NONE; + case SQLITE_DONE: + return APPL_ERROR_NO_DATA; + default: + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_reset(sqlite3_stmt *stmt) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_INVALID_PARAMETER); + + ret = sqlite3_reset(stmt); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + sqlite3_clear_bindings(stmt); + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_FAIL); + + ret = sqlite3_bind_int(stmt, idx, (int) value); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_FAIL); + + ret = sqlite3_bind_int(stmt, idx, value); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_bind_double(sqlite3_stmt *stmt, int idx, double value) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_FAIL); + + ret = sqlite3_bind_double(stmt, idx, value); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_bind_str(sqlite3_stmt *stmt, int idx, const char *str) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_FAIL); + retv_if(!str, APPL_ERROR_FAIL); + + ret = sqlite3_bind_text(stmt, idx, str, strlen(str), SQLITE_TRANSIENT); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI bool db_get_bool(sqlite3_stmt *stmt, int index) +{ + retv_if(!stmt, false); + return (bool) sqlite3_column_int(stmt, index); +} + + + +HAPI int db_get_int(sqlite3_stmt *stmt, int index) +{ + retv_if(!stmt, 0); + return sqlite3_column_int(stmt, index); +} + + + +HAPI int db_get_double(sqlite3_stmt *stmt, int index) +{ + retv_if(!stmt, 0); + return sqlite3_column_double(stmt, index); +} + + + +HAPI const char *db_get_str(sqlite3_stmt *stmt, int index) +{ + retv_if(!stmt, NULL); + return (const char *) sqlite3_column_text(stmt, index); +} + + + +HAPI appl_error_e db_finalize(sqlite3_stmt *stmt) +{ + int ret = SQLITE_OK; + + retv_if(!stmt, APPL_ERROR_INVALID_PARAMETER); + + ret = sqlite3_finalize(stmt); + if (SQLITE_OK != ret) { + _E("%s", sqlite3_errmsg(db_info.db)); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_exec(const char *query) +{ + sqlite3_stmt *stmt = NULL; + + retv_if(!query, APPL_ERROR_INVALID_PARAMETER); + + stmt = db_prepare(query); + retv_if(!stmt, APPL_ERROR_FAIL); + + goto_if(APPL_ERROR_FAIL == db_next(stmt), ERROR); + goto_if(APPL_ERROR_FAIL == db_finalize(stmt), ERROR); + + return APPL_ERROR_NONE; + +ERROR: + if (stmt) db_finalize(stmt); + return APPL_ERROR_FAIL; +} + + + +HAPI appl_error_e db_begin_transaction(void) +{ + int ret = SQLITE_BUSY; + + while (1) { + ret = sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL); + if (SQLITE_BUSY != ret) { + break; + } + /* FIXME : we have to fix this sleep */ + sleep(1); + } + + if (SQLITE_OK != ret) { + _E("sqlite3_exec() Failed(%d)", ret); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_end_transaction(void) +{ + int ret = SQLITE_OK; + + while (1) { + ret = sqlite3_exec(db_info.db, "COMMIT TRANSACTION", NULL, NULL, NULL); + if (SQLITE_BUSY != ret) { + break; + } + /* FIXME : we have to fix this sleep */ + sleep(1); + } + + if (SQLITE_OK != ret) { + _E("sqlite3_exec() Failed(%d)", ret); + return APPL_ERROR_FAIL; + } + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_create_table(void) +{ + const char *TABLES[] = { + "CREATE TABLE IF NOT EXIST db_checksum (version INT);", + "CREATE TABLE IF NOT EXIST path_information (" + "path_id INTEGER PRIMARY KEY" + ", user_id INTEGER NOT NULL" + ", latitude DOUBLE NOT NULL" + ", longitude DOUBLE NOT NULL" + ", title TEXT" + ", score INT" + ", price INT" + ", visible BOOL" + ", FOREIGH KEY(user_id) REFERENCES user(user_id)" + ");", + "CREATE TABLE IF NOT EXIST path (" + "path_id INTEGER NOT NULL" + ", order INTEGER NOT NULL" + ", content_id INTENGER NOT NULL" + ", PRIMARY KEY(path_id, order)" + ", FOREIGN KEY(path_id) REFERENCES path(path_id)" + ", FOREIGH KEY(content_id) REFERENCES content(contend_id)", + ");", + "CREATE TABLE IF NOT EXIST content (" + "content_id INTEGER PRIMARY KEY" + ", user_id INTEGER NOT NULL" + ", latitude DOUBLE NOT NULL" + ", longitude DOUBLE NOT NULL" + ", title TEXT" + ", content TEXT" + ", tag TEXT" + ", location TEXT" + ", icon TEXT" + ", attach_id INTEGER" + ", FOREIGN KEY(user_id) REFERENCES user(user_id)" + ", FOREIGN KEY(attach_id) REFERENCES attach(attach_id)" + ");", + "CREATE TABLE IF NOT EXIST attach (" + "attach_id INTEGER PRIMARY KEY" + ", path TEXT NOT NULL" + ", ref_count INT NOT NULL" + ");", + "CREATE TABLE IF NOT EXIST purchase (" + "user_id INTEGER NOT NULL" + ", content_id INTEGER NOT NULL" + ", PRIMARY KEY(user_id, content_id)" + ", FOREIGN KEY(user_id) REFERENCES user(user_id)" + ", FOREIGN KEY(content_id) REFERENCES content(content_id)" + ");", + "CREATE TABLE IF NOT EXIST user (" + "user_id INTEGER PRIMARY KEY" + ", name TEXT" + ");", + }; + int i = 0; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + for (; i < sizeof(char *) / sizeof(TABLES); i++) { + _D("Create a table[%s]", TABLES[i]); + break_if(db_exec(TABLES[i]) != APPL_ERROR_NONE); + } + + return APPL_ERROR_FAIL; +} + + + +HAPI appl_error_e db_drop_table(void) +{ + const char *TABLES[] = { + "DROP TABLE IF EXIST db_checksum;", + "DROP TABLE IF EXIST path_information;", + "DROP TABLE IF EXIST path;", + "DROP TABLE IF EXIST content;", + "DROP TABLE IF EXIST attach;", + "DROP TABLE IF EXIST purchase;", + "DROP TABLE IF EXIST user;", + }; + int i = 0; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + for (; i < sizeof(char *) / sizeof(TABLES); i++) { + _D("Create a table[%s]", TABLES[i]); + break_if(db_exec(TABLES[i]) != APPL_ERROR_NONE); + } + + return APPL_ERROR_FAIL; +} + + + +HAPI appl_error_e db_insert_version(int version) +{ + const char *QUERY_SYNTAX = "INSERT INTO db_checksum (version) values (%d);"; + char *query = NULL; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + query = sqlite3_mprintf(QUERY_SYNTAX, version); + retv_if(!query, APPL_ERROR_FAIL); + + if (db_exec(query) != APPL_ERROR_NONE) { + _E("Cannot execute query.[%s]", query); + sqlite3_free(query); + return APPL_ERROR_FAIL; + } + + sqlite3_free(query); + + /* keep the home DB opened */ + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_remove_version(int version) +{ + const char *QUERY_SYNTAX = "DELETE FROM db_checksum WHERE version=%d;"; + char *query = NULL; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + query = sqlite3_mprintf(QUERY_SYNTAX, version); + retv_if(!query, APPL_ERROR_FAIL); + + if (db_exec(query) != APPL_ERROR_NONE) { + _E("Cannot execute query.[%s]", query); + sqlite3_free(query); + return APPL_ERROR_FAIL; + } + + sqlite3_free(query); + + /* keep the home DB opened */ + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_update_version(int version) +{ + const char *QUERY_SYNTAX = "UPDATE db_checksum SET version=%d;"; + char *query = NULL; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + query = sqlite3_mprintf(QUERY_SYNTAX, version); + retv_if(!query, APPL_ERROR_FAIL); + + if (db_exec(query) != APPL_ERROR_NONE) { + _E("Cannot execute query.[%s]", query); + sqlite3_free(query); + return APPL_ERROR_FAIL; + } + + sqlite3_free(query); + + /* keep the home DB opened */ + + return APPL_ERROR_NONE; +} + + + +HAPI appl_error_e db_count_version(void) +{ + const char *QUERY_SYNTAX = "SELECT COUNT(*) FROM db_checksum;"; + sqlite3_stmt *st = NULL; + int count = 0; + + retv_if(APPL_ERROR_NONE != db_open(), APPL_ERROR_FAIL); + + st = db_prepare(QUERY_SYNTAX); + retv_if(!st, APPL_ERROR_FAIL); + + if (db_next(st) == APPL_ERROR_FAIL) { + _E("db_next error"); + db_finalize(st); + return -1; + } + + count = db_get_int(st, 0); + db_reset(st); + db_finalize(st); + + /* keep the home DB opened */ + + return count; +} + + + +// End of file. |