Rewrite db operation code 26/124726/2
authorSangyoon Jang <s89.jang@samsung.com>
Wed, 12 Apr 2017 07:58:23 +0000 (16:58 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Thu, 13 Apr 2017 07:45:48 +0000 (16:45 +0900)
- Remove db_util dependency
- Use prepared statement and binding routines

Change-Id: I8c3adb255f0a29edf590932f209a2229fe20a595
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
CMakeLists.txt
packaging/librua.spec
src/rua.c
src/rua_internal.c
src/rua_private.h
src/rua_stat.c
src/rua_stat_internal.c
src/rua_util.c
src/rua_util.h

index 3a9639f..62af4a8 100644 (file)
@@ -10,7 +10,7 @@ AUX_SOURCE_DIRECTORY(src SRCS)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED sqlite3 db-util libtzplatform-config bundle aul gio-2.0 glib-2.0)
+pkg_check_modules(pkgs REQUIRED sqlite3 libtzplatform-config bundle aul gio-2.0 glib-2.0)
 
 FOREACH(flag ${pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index d8cbdb5..4732227 100644 (file)
@@ -10,7 +10,6 @@ BuildRequires:  cmake
 BuildRequires:  sqlite3
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(aul)
-BuildRequires:  pkgconfig(db-util)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(gio-2.0)
index 7fef47a..2612817 100644 (file)
--- a/src/rua.c
+++ b/src/rua.c
@@ -19,7 +19,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <db-util.h>
+#include <sqlite3.h>
 #include <aul.h>
 #include <dlog.h>
 
@@ -133,18 +133,19 @@ API int rua_clear_history(void)
 
 API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
 {
+       static const char query[] =
+               "SELECT pkg_name, app_path, arg, launch_time, instance_id,"
+               "  instance_name, icon, uri "
+               "FROM rua_history ORDER BY launch_time DESC";
        int r;
-       char query[QUERY_MAXLEN];
        char *db_err = NULL;
        char **db_result = NULL;
        sqlite3 *db = NULL;
 
-       if (table == NULL)
-               return -1;
-       if (nrows == NULL)
-               return -1;
-       if (ncols == NULL)
+       if (table == NULL || nrows == NULL || ncols == NULL) {
+               LOGE("invalid parameter");
                return -1;
+       }
 
        r = _rua_util_check_uid(uid);
        if (r == -1)
@@ -154,19 +155,16 @@ API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t
        if (r != SQLITE_OK)
                return -1;
 
-       snprintf(query, sizeof(query),
-                "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
-                "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
-                RUA_HISTORY);
-
        r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
+       if (r != SQLITE_OK) {
+               LOGE("get table failed: %s", sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return -1;
+       }
 
-       if (r == SQLITE_OK)
-               *table = db_result;
-       else
-               sqlite3_free_table(db_result);
+       *table = db_result;
 
-       db_util_close(db);
+       sqlite3_close_v2(db);
 
        return r;
 }
@@ -286,11 +284,13 @@ API int rua_history_unload_db(char ***table)
 
 API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
 {
+       static const char query[] =
+               "SELECT pkg_name FROM rua_history "
+               "ORDER BY launch_time DESC LIMIT 1";
        int r = -1;
        sqlite3_stmt *stmt;
        const unsigned char *ct;
        sqlite3 *db = NULL;
-       char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
 
        if (!pkg_name)
                return -1;
@@ -303,31 +303,31 @@ API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
        if (r != SQLITE_OK)
                return -1;
 
-       r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
+       r = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return -1;
        }
 
        r = sqlite3_step(stmt);
-       if (r == SQLITE_ROW) {
-               ct = sqlite3_column_text(stmt, 0);
-               if (ct == NULL || ct[0] == '\0') {
-                       r = -1;
-                       goto out;
-               }
-
-               if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
-                       r = 0;
-                       goto out;
-               }
+       if (r != SQLITE_ROW) {
+               if (r != SQLITE_DONE)
+                       LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               sqlite3_close_v2(db);
+               return -1;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
-       if (db)
-               db_util_close(db);
+       ct = sqlite3_column_text(stmt, 0);
+       if (ct == NULL || ct[0] == '\0')
+               r = -1;
+       else if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0)
+               r = 0;
+       else
+               r = -1;
+
+       sqlite3_finalize(stmt);
+       sqlite3_close_v2(db);
 
        return r;
 }
index 4aa2250..b3b94af 100644 (file)
@@ -18,7 +18,6 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <db-util.h>
 #include <aul.h>
 #include <dlog.h>
 #include <sqlite3.h>
@@ -70,28 +69,82 @@ static sqlite3 *__db_init(uid_t uid)
 
        r = __create_table(db);
        if (r) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return NULL;
        }
 
        return db;
 }
 
-API int rua_db_delete_history(bundle *b)
+static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
+               const char *instance_id)
 {
-       return rua_usr_db_delete_history(b, getuid());
+       static const char query[] =
+               "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
+       sqlite3_stmt *stmt;
+       int r;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, pkg_name);
+       __BIND_TEXT(db, stmt, idx++, instance_id);
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+static int __delete_history_with_app_path(sqlite3 *db, const char *app_path,
+               const char *instance_id)
+{
+       static const char query[] =
+               "DELETE FROM rua_history WHERE app_path=? AND instance_id=?";
+       sqlite3_stmt *stmt;
+       int r;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, app_path);
+       __BIND_TEXT(db, stmt, idx++, instance_id);
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
 }
 
 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
 {
        int r;
-       sqlite3 *db = NULL;
-       char query[QUERY_MAXLEN];
+       sqlite3 *db;
        char *pkg_name = NULL;
        char *app_path = NULL;
        char *instance_id = NULL;
-       char *errmsg = NULL;
-       int result = 0;
 
        db = __db_init(uid);
        if (db == NULL) {
@@ -106,50 +159,105 @@ API int rua_usr_db_delete_history(bundle *b, uid_t uid)
        }
 
        if (pkg_name) {
-               snprintf(query, sizeof(query),
-                               "DELETE FROM rua_history WHERE pkg_name = '%s' "
-                               "AND instance_id = '%s';",
-                               pkg_name, instance_id ? instance_id : "");
+               LOGI("rua_delete_history_from_db : %s", pkg_name);
+               r = __delete_history_with_pkg_name(db, pkg_name, instance_id);
        } else if (app_path) {
-               snprintf(query, sizeof(query),
-                               "DELETE FROM rua_history WHERE app_path = '%s' "
-                               "AND instance_id = '%s';",
-                               app_path, instance_id ? instance_id : "");
+               LOGI("rua_delete_history_from_db : %s", app_path);
+               r = __delete_history_with_app_path(db, app_path, instance_id);
        } else {
-               snprintf(query, sizeof(query), "DELETE FROM rua_history;");
+               LOGE("No data to delete history");
+               return -1;
        }
 
-       LOGI("rua_delete_history_from_db : %s", query);
-       r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
-
-       if (r != SQLITE_OK) {
-               LOGE("fail to exec delete query %s : %s", query, errmsg);
-               sqlite3_free(errmsg);
-               result = -1;
-       }
+       sqlite3_close_v2(db);
 
        r = rua_dbus_send_update_signal(DELETE);
        if (r == -1) {
                LOGE("[RUA SEND SIGNAL ERROR] \n");
-               db_util_close(db);
                return -1;
        }
 
-       if (db != NULL)
-               db_util_close(db);
+       return 0;
+}
+
+API int rua_db_delete_history(bundle *b)
+{
+       return rua_usr_db_delete_history(b, getuid());
+}
+
+static int __insert_history(sqlite3 *db, struct rua_rec *rec)
+{
+       static const char query[] =
+               "INSERT OR REPLACE INTO rua_history ("
+               "  pkg_name, app_path, arg, launch_time,"
+               "  instance_id, instance_name, icon, uri) "
+               "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
+       int r;
+       sqlite3_stmt *stmt;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
+       __BIND_TEXT(db, stmt, idx++, rec->app_path);
+       __BIND_TEXT(db, stmt, idx++, rec->arg ? rec->arg : "");
+       __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
+       __BIND_TEXT(db, stmt, idx++, rec->instance_id ? rec->instance_id : "");
+       __BIND_TEXT(db, stmt, idx++,
+                       rec->instance_name ? rec->instance_name : "");
+       __BIND_TEXT(db, stmt, idx++, rec->icon ? rec->icon : "");
+       __BIND_TEXT(db, stmt, idx++, rec->uri ? rec->uri : "");
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
 
-       return result;
+       return 0;
 }
 
-API int rua_db_add_history(struct rua_rec *rec)
+static int __update_history(sqlite3 *db, struct rua_rec *rec)
 {
-       return rua_usr_db_add_history(rec, getuid());
+       static const char query[] =
+               "UPDATE rua_history SET launch_time=? "
+               "WHERE pkg_name=? AND instance_id=?";
+       int r;
+       sqlite3_stmt *stmt;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
+       __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
+       __BIND_TEXT(db, stmt, idx++, rec->instance_id);
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
 }
 
 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
 {
        int r;
-       char query[QUERY_MAXLEN];
        sqlite3 *db;
 
        if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
@@ -165,46 +273,23 @@ API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
 
        if (rec->instance_id &&
                        (rec->instance_name == NULL || rec->icon == NULL ||
-                        rec->uri == NULL)) {
-               snprintf(query, sizeof(query),
-                               "UPDATE %s SET launch_time = %d "
-                               "WHERE pkg_name = %s AND instance_id = %s;",
-                               RUA_HISTORY,
-                               (int)rec->launch_time,
-                               rec->pkg_name,
-                               rec->instance_id);
-       } else {
-               snprintf(query, sizeof(query),
-                               "INSERT OR REPLACE INTO %s "
-                               "(pkg_name, app_path, arg, launch_time, "
-                               "instance_id, instance_name, icon, uri) "
-                               "VALUES (\"%s\", \"%s\", \"%s\", %d, "
-                               "\"%s\", \"%s\", \"%s\", \"%s\");",
-                               RUA_HISTORY,
-                               rec->pkg_name,
-                               rec->app_path,
-                               rec->arg ? rec->arg : "",
-                               (int)rec->launch_time,
-                               rec->instance_id ? rec->instance_id : "",
-                               rec->instance_name ? rec->instance_name : "",
-                               rec->icon ? rec->icon : "",
-                               rec->uri ? rec->uri : "");
-       }
-
-       r = __exec(db, query);
-       if (r == -1) {
-               LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
-               db_util_close(db);
-               return -1;
-       }
+                        rec->uri == NULL))
+               r = __update_history(db, rec);
+       else
+               r = __insert_history(db, rec);
 
        r = rua_dbus_send_update_signal(ADD);
        if (r == -1) {
                LOGE("[RUA SEND SIGNAL ERROR] \n");
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return -1;
        }
 
-       db_util_close(db);
+       sqlite3_close_v2(db);
        return r;
 }
+
+API int rua_db_add_history(struct rua_rec *rec)
+{
+       return rua_usr_db_add_history(rec, getuid());
+}
index 5dec197..08bbd00 100644 (file)
@@ -17,8 +17,6 @@
 #ifndef __RUA_PRIVATE_H__
 #define __RUA_PRIVATE_H__
 
-#include <sqlite3.h>
-
 #ifdef LOG_TAG
 #undef LOG_TAG
 #endif
 #endif
 
 #define RUA_DB_NAME ".rua.db"
-#define RUA_HISTORY "rua_history"
 #define RUA_STAT_DB_NAME ".rua_stat.db"
-#define QUERY_MAXLEN 4096
-
-int _rua_stat_init(sqlite3 **db, char *db_name, int flags, uid_t uid);
 
 #endif /*__RUA_H__*/
index 1c5e8ed..e47db46 100644 (file)
@@ -26,7 +26,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 
-#include <db-util.h>
+#include <sqlite3.h>
 #include <aul.h>
 #include <dlog.h>
 
@@ -60,40 +60,25 @@ API int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid)
        return r;
 }
 
-API int rua_stat_get_stat_tags_for_uid(char *caller,
-               int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
+static int __get_stat_tags_for_uid(sqlite3 *db, const char *caller,
+               int (*rua_stat_tag_iter_fn)(
+                       const char *rua_stat_tag, void *data),
                void *data, uid_t uid)
 {
+       static const char query[] =
+               "SELECT rua_stat_tag FROM rua_panel_stat "
+               "WHERE caller_panel=? ORDER BY score DESC";
        int r;
        sqlite3_stmt *stmt;
-       char query[QUERY_MAXLEN];
        const unsigned char *ct;
-       sqlite3 *db = NULL;
-
-       r = _rua_util_check_uid(uid);
-       if (r == -1)
-               return r;
-
-       r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_READONLY, uid);
-       if (r == -1) {
-               LOGE("__rua_stat_init fail");
-               return -1;
-       }
 
-       sqlite3_snprintf(QUERY_MAXLEN, query,
-               "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC");
-
-       r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
+       r = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
-               goto out;
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
        }
 
-       r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("caller bind error(%d) \n", r);
-               goto out;
-       }
+       __BIND_TEXT(db, stmt, 1, caller);
 
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                ct = sqlite3_column_text(stmt, 0);
@@ -103,12 +88,32 @@ API int rua_stat_get_stat_tags_for_uid(char *caller,
                rua_stat_tag_iter_fn((const char *)ct, data);
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
+
+       return r;
+}
+
+API int rua_stat_get_stat_tags_for_uid(char *caller,
+               int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
+               void *data, uid_t uid)
+{
+       int r;
+       sqlite3 *db;
+
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
+
+       r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_STAT_DB_NAME);
+       if (r == -1) {
+               LOGE("open rua stat db failed");
+               return -1;
+       }
+
+       r = __get_stat_tags_for_uid(db, caller, rua_stat_tag_iter_fn, data,
+                       uid);
 
-       if (db)
-               db_util_close(db);
+       sqlite3_close_v2(db);
 
        return r;
 }
index 438117b..3f8110b 100644 (file)
@@ -19,7 +19,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <db-util.h>
 #include <dlog.h>
 #include <sqlite3.h>
 
 
 int __rua_stat_insert(sqlite3 *db, char *caller, char *rua_stat_tag)
 {
+       static const char query[] =
+               "INSERT INTO rua_panel_stat ("
+               "  caller_panel, rua_stat_tag, score) "
+               "VALUES (?, ?, ?)";
        int r;
-       char query[QUERY_MAXLEN];
-       sqlite3_stmt *stmt = NULL;
-
-       sqlite3_snprintf(QUERY_MAXLEN, query,
-               "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)");
+       sqlite3_stmt *stmt;
+       int idx = 1;
 
        r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
-               goto out;
-       }
-
-       r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("caller bind error(%d) \n", r);
-               goto out;
-       }
-
-       r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("rua_stat_tag bind error(%d) \n", r);
-               goto out;
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
        }
 
-       r = sqlite3_bind_int(stmt, 3, WIN_SCORE);
-       if (r != SQLITE_OK) {
-               LOGE("arg bind error(%d) \n", r);
-               goto out;
-       }
+       __BIND_TEXT(db, stmt, idx++, caller);
+       __BIND_TEXT(db, stmt, idx++, rua_stat_tag);
+       __BIND_INT(db, stmt, idx++, WIN_SCORE);
 
        r = sqlite3_step(stmt);
        if (r != SQLITE_DONE) {
-               LOGE("step error(%d) \n", r);
-               goto out;
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
 
        return r;
 }
 
 int __rua_stat_lose_score_update(sqlite3 *db, char *caller, char *rua_stat_tag)
 {
+       static const char query[] =
+               "UPDATE rua_panel_stat SET score=score*? "
+               "WHERE caller_panel=? AND rua_stat_tag!=?";
        int r;
-       char query[QUERY_MAXLEN];
-       sqlite3_stmt *stmt = NULL;
-
-       sqlite3_snprintf(QUERY_MAXLEN, query,
-               "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?",
-               LOSE_SCORE_RATE);
+       sqlite3_stmt *stmt;
+       int idx = 1;
 
-       LOGD("lose score update sql : %s", query);
+       LOGD("lose score update sql: %s, %s", caller, rua_stat_tag);
        r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
-               goto out;
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return r;
        }
 
-       r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("caller bind error(%d) \n", r);
-               goto out;
-       }
-
-       r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("rua_stat_tag bind error(%d) \n", r);
-               goto out;
-       }
+       __BIND_DOUBLE(db, stmt, idx++, LOSE_SCORE_RATE);
+       __BIND_TEXT(db, stmt, idx++, caller);
+       __BIND_TEXT(db, stmt, idx++, rua_stat_tag);
 
        r = sqlite3_step(stmt);
        if (r != SQLITE_DONE) {
-               LOGE("step error(%d) \n", r);
-               goto out;
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return r;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
 
        return r;
 }
 
 int __rua_stat_win_score_update(sqlite3 *db, char *caller, char *rua_stat_tag)
 {
+       static const char query[] =
+               "UPDATE rua_panel_stat SET score=score+? "
+               "WHERE caller_panel=? AND rua_stat_tag=?";
        int r;
-       char query[QUERY_MAXLEN];
-       sqlite3_stmt *stmt = NULL;
-
-       sqlite3_snprintf(QUERY_MAXLEN, query,
-               "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?",
-               WIN_SCORE);
+       sqlite3_stmt *stmt;
+       int idx = 1;
 
-       LOGD("win score update sql : %s", query);
+       LOGD("win score update sql: %s, %s", caller, rua_stat_tag);
 
        r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
-               goto out;
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return r;
        }
 
-       r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("caller bind error(%d) \n", r);
-               goto out;
-       }
-
-       r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
-       if (r != SQLITE_OK) {
-               LOGE("rua_stat_tag bind error(%d) \n", r);
-               goto out;
-       }
+       __BIND_INT(db, stmt, idx++, WIN_SCORE);
+       __BIND_TEXT(db, stmt, idx++, caller);
+       __BIND_TEXT(db, stmt, idx++, rua_stat_tag);
 
        r = sqlite3_step(stmt);
        if (r != SQLITE_DONE) {
-               LOGE("step error(%d) \n", r);
-               goto out;
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return r;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
 
        return r;
 }
 
-static int __exec(sqlite3 *db, char *query)
+static int __create_table(sqlite3 *db)
 {
        int r;
        char *errmsg = NULL;
 
-       if (db == NULL)
-               return -1;
-
-       r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
+       r = sqlite3_exec(db, CREATE_RUA_STAT_TABLE, NULL, NULL, &errmsg);
        if (r != SQLITE_OK) {
-               SECURE_LOGE("query(%s) exec error(%s)", query, errmsg);
+               LOGE("create table failed: %s", errmsg);
                sqlite3_free(errmsg);
                return -1;
        }
@@ -179,71 +141,35 @@ static int __exec(sqlite3 *db, char *query)
        return 0;
 }
 
-static int __create_table(sqlite3 *db)
-{
-       int r;
-
-       r = __exec(db, CREATE_RUA_STAT_TABLE);
-       if (r == -1) {
-               LOGE("create table error");
-               return -1;
-       }
-
-       return 0;
-}
-
-int _rua_stat_init(sqlite3 **db, char *db_name, int flags, uid_t uid)
-{
-       int r;
-
-       r = _rua_util_open_db(db, flags, uid, db_name);
-       r = __create_table(*db);
-       if (r) {
-               db_util_close(*db);
-               return -1;
-       }
-
-       if (*db == NULL) {
-               LOGE("__rua_stat_init error");
-               return -1;
-       }
-       return 0;
-}
-
 API int rua_stat_usr_db_update(char *caller, char *rua_stat_tag, uid_t uid)
 {
        int r;
        int affected_rows = 0;
-       sqlite3 *db = NULL;
+       sqlite3 *db;
 
        LOGD("rua_stat_update start");
 
-       r = _rua_stat_init(&db, RUA_STAT_DB_NAME,
-                       SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
-                       uid);
-       if (r == -1) {
-               LOGE("__rua_stat_init fail");
+       if (caller == NULL || rua_stat_tag == NULL) {
+               LOGE("invalid parameter");
                return -1;
        }
 
-       if (db == NULL) {
-               LOGE("rua_stat is not initialized");
-               return -1;
-       }
-
-       if (caller == NULL) {
-               LOGE("caller is null");
+       r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
+                       uid, RUA_STAT_DB_NAME);
+       if (r == -1) {
+               LOGE("open rua stat db failed");
                return -1;
        }
 
-       if (rua_stat_tag == NULL) {
-               LOGE("rua_stat_tag is null");
+       r = __create_table(db);
+       if (r == -1) {
+               LOGE("create rua stat tabale failed");
                return -1;
        }
 
        r = __rua_stat_lose_score_update(db, caller, rua_stat_tag);
        if (r != SQLITE_DONE) {
-               LOGE("__rua_stat_lose_score_insert fail.");
+               LOGE("__rua_stat_lose_score_insert failed");
                return -1;
        }
 
@@ -253,13 +179,14 @@ API int rua_stat_usr_db_update(char *caller, char *rua_stat_tag, uid_t uid)
                r = __rua_stat_insert(db, caller, rua_stat_tag);
 
                if (r != SQLITE_DONE) {
-                       LOGE("__rua_stat_insert fail.");
+                       LOGE("__rua_stat_insert failed");
                        return -1;
                }
        }
-       if (db)
-               db_util_close(db);
+
+       sqlite3_close_v2(db);
        LOGD("rua_stat_update done");
+
        return r;
 }
 
index 09dd080..ee83521 100644 (file)
@@ -23,7 +23,7 @@
 #include <pwd.h>
 
 #include <sys/stat.h>
-#include <db-util.h>
+#include <sqlite3.h>
 #include <tzplatform_config.h>
 #include <dlog.h>
 
@@ -50,6 +50,19 @@ char *_rua_util_get_db_path(uid_t uid, char *db_name)
        return strdup(db_path);
 }
 
+#define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
+#define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
+static int __db_busy_handler(void *data, int count)
+{
+       /* sqlite3_prepare_V2 will return SQLITE_BUSY */
+       if (count >= BUSY_WAITING_MAX)
+               return 0;
+
+       usleep(BUSY_WAITING_USEC);
+
+       return 1;
+}
+
 int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name)
 {
        int r;
@@ -61,19 +74,20 @@ int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name)
                return -1;
        }
 
-       r = db_util_open_with_options(db_path, db, flags, NULL);
-       if (r) {
-               LOGE("db util open error(%d/%d/%d/%s)", r,
-                       sqlite3_errcode(*db),
-                       sqlite3_extended_errcode(*db),
-                       sqlite3_errmsg(*db));
-               free(db_path);
+       r = sqlite3_open_v2(db_path, db, flags, NULL);
+       free(db_path);
+       if (r != SQLITE_OK) {
+               LOGE("open db failed: %d", r);
                return -1;
        }
 
-       free(db_path);
+       r = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("register busy handler failed: %s", sqlite3_errmsg(*db));
+               return -1;
+       }
 
-       return r;
+       return 0;
 }
 
 int _rua_util_check_uid(uid_t target_uid)
index 13efffe..ce77c31 100644 (file)
 
 #include <sqlite3.h>
 
+#define __BIND_TEXT(db, stmt, i, text)                                         \
+do {                                                                           \
+       if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\
+               LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));       \
+               sqlite3_finalize(stmt);                                        \
+               return -1;                                                     \
+       }                                                                      \
+} while (0)
+
+#define __BIND_INT(db, stmt, i, int)                                           \
+do {                                                                           \
+       if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) {                     \
+               LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));       \
+               sqlite3_finalize(stmt);                                        \
+               return -1;                                                     \
+       }                                                                      \
+} while (0)
+
+#define __BIND_DOUBLE(db, stmt, i, double)                                     \
+do {                                                                           \
+       if (sqlite3_bind_double(stmt, i, double) != SQLITE_OK) {               \
+               LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));       \
+               sqlite3_finalize(stmt);                                        \
+               return -1;                                                     \
+       }                                                                      \
+} while (0)
+
 char *_rua_util_get_db_path(uid_t uid, char *db_name);
 int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name);
 int _rua_util_check_uid(uid_t target_uid);