Refactor the logic related with db 05/163905/2
authorSeungha Son <seungha.son@samsung.com>
Thu, 14 Dec 2017 04:03:43 +0000 (13:03 +0900)
committerSeungha Son <seungha.son@samsung.com>
Thu, 14 Dec 2017 08:33:29 +0000 (17:33 +0900)
Signed-off-by: Seungha Son <seungha.son@samsung.com>
Change-Id: I8ac948b360ea07457b6fe13b285ab927fc556351

src/badge_setting_service.c

index 4c04884..3a5e7f4 100644 (file)
@@ -362,63 +362,68 @@ out:
 
 static bool _is_package_in_setting_table(sqlite3 *db, const char *pkgname, const char* appid, uid_t uid)
 {
-       sqlite3_stmt *db_statement = NULL;
-       int sqlite3_ret = SQLITE_OK;
+       int sql_ret;
        bool err = true;
-       int field_index = 1;
+       char *query = NULL;
+       sqlite3_stmt *stmt = NULL;
 
        if (appid != NULL)
-               sqlite3_ret = sqlite3_prepare_v2(db, "SELECT appid FROM badge_setting WHERE uid = ? AND pkgname = ? AND appid = ?", -1, &db_statement, NULL);
+               query = sqlite3_mprintf("SELECT appid FROM badge_setting "
+                                                               "WHERE uid = %d AND pkgname = %Q AND appid = %Q",
+                                                               uid, pkgname, appid);
        else
-               sqlite3_ret = sqlite3_prepare_v2(db, "SELECT pkgname FROM badge_setting WHERE uid = ? AND pkgname = ?", -1, &db_statement, NULL);
-
-       if (sqlite3_ret != SQLITE_OK) {
+               query = sqlite3_mprintf("SELECT pkgname FROM badge_setting "
+                                                               "WHERE uid = %d AND pkgname = %Q",
+                                                               uid, pkgname);
+       if (query == NULL) {
                /* LCOV_EXCL_START */
-               ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
+               ERR("Out of Memory");
                err = false;
                goto out;
                /* LCOV_EXCL_STOP */
        }
 
-       sqlite3_bind_int(db_statement, field_index++, uid);
-       sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
-       if (appid != NULL)
-               sqlite3_bind_text(db_statement, field_index++, appid, -1, SQLITE_TRANSIENT);
+       sql_ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
+       if (sql_ret != SQLITE_OK) {
+               ERR("sqlite3_prepare_v2 failed [%d][%s]", sql_ret, sqlite3_errmsg(db));
+               err = false;
+               goto out;
+       }
 
-       sqlite3_ret = sqlite3_step(db_statement);
-       if (sqlite3_ret == SQLITE_DONE) {
-               INFO("no matched appid from pkgname found[%s][%s][%d]", pkgname, appid, sqlite3_ret);
+       sql_ret = sqlite3_step(stmt);
+       if (sql_ret == SQLITE_DONE) {
+               INFO("No matched appid[%s] from pkgname[%s]", pkgname, appid);
                err = false;
                goto out;
        }
 
-       if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
+       if (sql_ret != SQLITE_OK && sql_ret != SQLITE_ROW) {
                /* LCOV_EXCL_START */
-               ERR("sqlite3_step failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
+               ERR("sqlite3_step failed [%d][%s]", sql_ret,
+                               sqlite3_errmsg(db));
                err = false;
                goto out;
                /* LCOV_EXCL_STOP */
        }
 
 out:
-       if (db_statement)
-               sqlite3_finalize(db_statement);
+       if (stmt)
+               sqlite3_finalize(stmt);
+       if (query)
+               sqlite3_free(query);
 
        return err;
 }
 
 static int app_info_callback(const pkgmgrinfo_appinfo_h handle, void *user_data)
 {
-       badge_setting_info *info = (badge_setting_info *)user_data;
-       sqlite3 *db = info->db;
-       sqlite3_stmt *db_statement = NULL;
-       int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
-       int field_index = 1;
-       int sqlite3_ret = SQLITE_OK;
+       int ret;
+       int pkgmgr_ret;
        char *appid = NULL;
        char *pkgname = NULL;
+       char *query = NULL;
+       badge_setting_info *info = (badge_setting_info *)user_data;
+       sqlite3 *db = info->db;
 
        pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
        if (pkgmgr_ret != PACKAGE_MANAGER_ERROR_NONE) {
@@ -433,35 +438,30 @@ static int app_info_callback(const pkgmgrinfo_appinfo_h handle, void *user_data)
        }
 
        if (_is_package_in_setting_table(db, pkgname, appid, info->uid) == true) {
-               INFO("uid %d [%s] is exist", info->uid, appid);
+               INFO("uid[%d] appid[%s] is exist", info->uid, appid);
                goto out;
        }
 
-       INFO("uid %d pkgname %s [%s] will be inserted", info->uid, pkgname, appid);
-       sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO badge_setting (uid, pkgname, appid) "
-                                        "VALUES (?, ?, ?) ", -1, &db_statement, NULL);
-
-       if (sqlite3_ret != SQLITE_OK) {
-               ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
+       query = sqlite3_mprintf("INSERT INTO badge_setting "
+                                                       "(uid, pkgname, appid) "
+                                                       "VALUES (%d, %Q, %Q) ",
+                                                       info->uid, pkgname, appid);
+       if (query == NULL) {
+               ERR("Out of memory");
                goto out;
        }
 
-       sqlite3_bind_int(db_statement, field_index++, info->uid);
-       sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
-       sqlite3_bind_text(db_statement, field_index++, appid, -1, SQLITE_TRANSIENT);
-
-       sqlite3_ret = sqlite3_step(db_statement);
+       ret = badge_db_exec(db, query, NULL);
+       if (ret != BADGE_ERROR_NONE) {
+               ERR("DB Error");
+               goto out;
+       }
 
-       INFO("sqlite3_step returns[%d]", sqlite3_ret);
-
-       if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE)
-               ERR("sqlite3_step failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
+       INFO("Insert uid[%d] pkgname[%s] appid[%s]", info->uid, pkgname, appid);
 
 out:
-       if (db_statement)
-               sqlite3_finalize(db_statement);
+       if (query)
+               sqlite3_free(query);
 
        return 0;
 }
@@ -588,16 +588,15 @@ out:
 
 EXPORT_API int badge_setting_delete_package_for_uid(const char *pkgname, uid_t uid)
 {
-       sqlite3 *db = NULL;
-       sqlite3_stmt *db_statement = NULL;
        int ret = BADGE_ERROR_NONE;
-       int sqlite3_ret = SQLITE_OK;
-       int field_index = 1;
+       int sql_ret;
+       sqlite3 *db = NULL;
        bool is_package_in_setting_table = false;
+       char *query = NULL;
 
-       sqlite3_ret = sqlite3_open_v2(BADGE_DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
-       if (sqlite3_ret != SQLITE_OK || db == NULL) {
-               ERR("db_util_open failed [%s][%d]", BADGE_DB_PATH, sqlite3_ret);
+       sql_ret = sqlite3_open_v2(BADGE_DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
+       if (ret != SQLITE_OK || db == NULL) {
+               ERR("db_util_open failed [%s][%d]", BADGE_DB_PATH, sql_ret);
                ret = BADGE_ERROR_FROM_DB;
                goto out;
        }
@@ -610,39 +609,32 @@ EXPORT_API int badge_setting_delete_package_for_uid(const char *pkgname, uid_t u
 
        sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
 
-       sqlite3_ret = sqlite3_prepare_v2(db, "DELETE FROM badge_setting WHERE uid = ? AND pkgname = ? ", -1, &db_statement, NULL);
-       if (sqlite3_ret != SQLITE_OK) {
-               /* LCOV_EXCL_START */
-               ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
-               ret = BADGE_ERROR_FROM_DB;
+       query = sqlite3_mprintf("DELETE FROM badge_setting "
+                                                       "WHERE uid = %d AND pkgname = %Q ",
+                                                       uid, pkgname);
+       if (query == NULL) {
+               ERR("Out of memory");
+               ret = BADGE_ERROR_OUT_OF_MEMORY;
                goto out;
-               /* LCOV_EXCL_STOP */
        }
 
-       sqlite3_bind_int(db_statement, field_index++, uid);
-       sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
-
-       sqlite3_ret = sqlite3_step(db_statement);
-       if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
-               /* LCOV_EXCL_START */
-               ERR("sqlite3_step failed [%d][%s]", sqlite3_ret,
-                                       sqlite3_errmsg(db));
-               ret = BADGE_ERROR_FROM_DB;
-               /* LCOV_EXCL_STOP */
+       ret = badge_db_exec(db, query, NULL);
+       if (ret != BADGE_ERROR_NONE) {
+               ERR("DB Error");
+               goto out;
        }
 
 out:
-       if (db_statement)
-               sqlite3_finalize(db_statement);
+       if (query)
+               sqlite3_free(query);
        if (db) {
                if (ret == BADGE_ERROR_NONE)
                        sqlite3_exec(db, "END;", NULL, NULL, NULL);
                else
                        sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
 
-               if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
-                       WARN("db_util_close failed [%d]", sqlite3_ret);
+               if ((sql_ret = db_util_close(db)) != SQLITE_OK)
+                       WARN("db_util_close failed [%d]", sql_ret);
        }
 
        return ret;