--- /dev/null
+/*
+ * 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__
--- /dev/null
+/*
+ * 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 score ("
+ "user_id INTEGER NOT NULL"
+ ", score INTEGER NOT NULL"
+ ", FOREIGN KEY(user_id) REFERENCES user(user_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 score;",
+ "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.