DB : initial version
authorJin Yoon <jinny.yoon@samsung.com>
Mon, 1 Jun 2015 15:42:20 +0000 (00:42 +0900)
committerJin Yoon <jinny.yoon@samsung.com>
Mon, 1 Jun 2015 15:42:20 +0000 (00:42 +0900)
Change-Id: I76ab17690159883cf401c7737a5db2a4ea5a521b

.gitignore [new file with mode: 0644]
.project
inc/db.h [new file with mode: 0644]
inc/util.h [new file with mode: 0644]
src/db.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..65fa71e
--- /dev/null
@@ -0,0 +1,2 @@
+/Debug
+/SA_Report
index dd65049..29a72db 100644 (file)
--- a/.project
+++ b/.project
                        <arguments>
                        </arguments>
                </buildCommand>
+               <buildCommand>
+                       <name>org.tizen.nativecore.apichecker.core.builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
        </buildSpec>
        <natures>
                <nature>org.eclipse.cdt.core.cnature</nature>
                <nature>org.eclipse.cdt.core.ccnature</nature>
                <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
                <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+               <nature>org.tizen.nativecore.apichecker.core.tizenCppNature</nature>
        </natures>
        <filteredResources>
                <filter>
diff --git a/inc/db.h b/inc/db.h
new file mode 100644 (file)
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 (file)
index 0000000..042f340
--- /dev/null
@@ -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 (file)
index 0000000..a30ec04
--- /dev/null
+++ b/src/db.c
@@ -0,0 +1,475 @@
+/*
+ * 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.