[0.1.21] Fix format specifiers
[platform/core/account/account-manager.git] / server / src / account-server-db.c
index 94def44..8ba8eec 100644 (file)
@@ -59,8 +59,7 @@ static sqlite3* g_hAccountDB = NULL;
 static sqlite3* g_hAccountDB2 = NULL;
 static sqlite3* g_hAccountGlobalDB = NULL;
 static sqlite3* g_hAccountGlobalDB2 = NULL;
-pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
-pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 //static char *_account_dup_text(const char *text_data);
 static int _account_insert_custom(account_s *account, int account_id);
@@ -235,7 +234,7 @@ int _account_global_db_open(void)
        ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
 
        if (g_hAccountGlobalDB) {
-               _ERR("Account database is using in another app. %x", g_hAccountDB);
+               _ERR("Account database is using in another app");
                return _ACCOUNT_ERROR_DATABASE_BUSY;
        }
 
@@ -338,7 +337,7 @@ int _account_db_open(int mode, int pid, uid_t uid)
        ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
 
        if (g_hAccountDB) {
-               _ERR("Account database is using in another app. %x", g_hAccountDB);
+               _ERR("Account database is using in another app");
                return _ACCOUNT_ERROR_DATABASE_BUSY;
        }
 
@@ -572,6 +571,8 @@ static int _account_update_capability(account_s *account, int account_id)
 
        if (rc != SQLITE_DONE) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DB_FAILED;
        }
        rc = _account_query_finalize(hstmt);
@@ -653,6 +654,8 @@ static int _account_update_capability_by_user_name(account_s *account, const cha
        rc = _account_query_step(hstmt);
        if (rc != SQLITE_DONE) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DB_FAILED;
        }
 
@@ -743,126 +746,138 @@ bool _account_add_custom_to_account_cb(const char* key, const char* value, accou
        return TRUE;
 }
 
-
-static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
+static void _account_compare_new_and_old_account_properties(account_s** new_account, account_s* old_account)
 {
-       int                             error_code = _ACCOUNT_ERROR_NONE;
-       account_stmt    hstmt = NULL;
-       char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
-       int                             rc = 0;
-       account_s *old_account = NULL;
-
-       ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
-       ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
-       ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
-       ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
-
-       old_account = (account_s*)calloc(1, sizeof(account_s));
-       if (!old_account) {
-               ACCOUNT_FATAL("Memory alloc fail\n");
-               return _ACCOUNT_ERROR_OUT_OF_MEMORY;
-       }
-
-       ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
-
-       ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
-       hstmt = _account_prepare_query(g_hAccountDB, query);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
-
-       while (rc == SQLITE_ROW) {
-               _account_convert_column_to_account(hstmt, old_account);
-               rc = _account_query_step(hstmt);
+       if ((*new_account) == NULL || old_account == NULL) {
+               _ERR("invalid input params");
+               return;
        }
 
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
-       hstmt = NULL;
-
-       // get capability
-       error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
-       ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
-
-       // get custom text
-       error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
-       ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
-
        // compare
-       new_account->id = old_account->id;
+       (*new_account)->id = old_account->id;
 
        //user name
-       if (!new_account->user_name) {
+       if (!(*new_account)->user_name) {
                if (old_account->user_name)
-                       new_account->user_name = _account_dup_text(old_account->user_name);
+                       (*new_account)->user_name = _account_dup_text(old_account->user_name);
        }
 
        // display name
-       if (!new_account->display_name) {
+       if (!(*new_account)->display_name) {
                if (old_account->display_name)
-                       new_account->display_name = _account_dup_text(old_account->display_name);
+                       (*new_account)->display_name = _account_dup_text(old_account->display_name);
        }
 
        // email address
-       if (!new_account->email_address) {
+       if (!(*new_account)->email_address) {
                if (old_account->email_address)
-                       new_account->email_address = _account_dup_text(old_account->email_address);
+                       (*new_account)->email_address = _account_dup_text(old_account->email_address);
        }
 
        // domain name
-       if (!new_account->domain_name) {
+       if (!(*new_account)->domain_name) {
                if (old_account->domain_name)
-                       new_account->domain_name = _account_dup_text(old_account->domain_name);
+                       (*new_account)->domain_name = _account_dup_text(old_account->domain_name);
        }
 
        // icon path
-       if (!new_account->icon_path) {
+       if (!(*new_account)->icon_path) {
                if (old_account->icon_path)
-                       new_account->icon_path = _account_dup_text(old_account->icon_path);
+                       (*new_account)->icon_path = _account_dup_text(old_account->icon_path);
        }
 
        // source
-       if (!new_account->source) {
+       if (!(*new_account)->source) {
                if (old_account->source)
-                       new_account->source = _account_dup_text(old_account->source);
+                       (*new_account)->source = _account_dup_text(old_account->source);
        }
 
-       _ACCOUNT_FREE(new_account->package_name);
-       new_account->package_name = _account_dup_text(old_account->package_name);
+       _ACCOUNT_FREE((*new_account)->package_name);
+       (*new_account)->package_name = _account_dup_text(old_account->package_name);
 
        // access token
-       if (!new_account->access_token) {
+       if (!(*new_account)->access_token) {
                if (old_account->access_token)
-                       new_account->access_token = _account_dup_text(old_account->access_token);
+                       (*new_account)->access_token = _account_dup_text(old_account->access_token);
        }
 
        // auth type
-       if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
-               new_account->auth_type = old_account->auth_type;
+       if ((*new_account)->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
+               (*new_account)->auth_type = old_account->auth_type;
 
        //secret
-       if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
-               new_account->secret = old_account->secret;
+       if ((*new_account)->secret == _ACCOUNT_SECRECY_INVALID)
+               (*new_account)->secret = old_account->secret;
 
        // sync support
-       if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
-               new_account->sync_support = old_account->sync_support;
+       if ((*new_account)->sync_support == _ACCOUNT_SYNC_INVALID)
+               (*new_account)->sync_support = old_account->sync_support;
 
        // TODO user text
        int i;
        for (i = 0; i < USER_TXT_CNT; i++) {
-               if (!new_account->user_data_txt[i]) {
+               if (!(*new_account)->user_data_txt[i]) {
                        if (old_account->user_data_txt[i])
-                               new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
+                               (*new_account)->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
                }
        }
 
        // TODO user int
        for (i = 0; i < USER_INT_CNT; i++) {
-               if (new_account->user_data_int[i] == 0)
-                               new_account->user_data_int[i] = old_account->user_data_int[i];
+               if ((*new_account)->user_data_int[i] == 0)
+                               (*new_account)->user_data_int[i] = old_account->user_data_int[i];
        }
 
+       return;
+}
+
+
+static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
+{
+       int                             error_code = _ACCOUNT_ERROR_NONE;
+       account_stmt    hstmt = NULL;
+       char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
+       int                             rc = 0;
+       account_s *old_account = NULL;
+
+       ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
+       ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
+       ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
+       ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
+
+       old_account = (account_s*)calloc(1, sizeof(account_s));
+       if (!old_account) {
+               ACCOUNT_FATAL("Memory alloc fail\n");
+               return _ACCOUNT_ERROR_OUT_OF_MEMORY;
+       }
+
+       ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
+
+       ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
+       hstmt = _account_prepare_query(g_hAccountDB, query);
+
+       rc = _account_query_step(hstmt);
+       ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
+
+       while (rc == SQLITE_ROW) {
+               _account_convert_column_to_account(hstmt, old_account);
+               rc = _account_query_step(hstmt);
+       }
+
+       rc = _account_query_finalize(hstmt);
+       ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
+       hstmt = NULL;
+
+       // get capability
+       error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
+       ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
+
+       // get custom text
+       error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
+       ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
+
+       _account_compare_new_and_old_account_properties(&new_account, old_account);
+
 CATCH:
        if (old_account)
                _account_free_account_with_items(old_account);
@@ -1248,81 +1263,7 @@ static int _account_compare_old_record(account_s *new_account, int account_id)
        error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
        ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
 
-       // compare
-
-       new_account->id = old_account->id;
-
-       //user name
-       if (!new_account->user_name) {
-               if (old_account->user_name)
-                       new_account->user_name = _account_dup_text(old_account->user_name);
-       }
-
-       // display name
-       if (!new_account->display_name) {
-               if (old_account->display_name)
-                       new_account->display_name = _account_dup_text(old_account->display_name);
-       }
-
-       // email address
-       if (!new_account->email_address) {
-               if (old_account->email_address)
-                       new_account->email_address = _account_dup_text(old_account->email_address);
-       }
-
-       // domain name
-       if (!new_account->domain_name) {
-               if (old_account->domain_name)
-                       new_account->domain_name = _account_dup_text(old_account->domain_name);
-       }
-
-       // icon path
-       if (!new_account->icon_path) {
-               if (old_account->icon_path)
-                       new_account->icon_path = _account_dup_text(old_account->icon_path);
-       }
-
-       // source
-       if (!new_account->source) {
-               if (old_account->source)
-                       new_account->source = _account_dup_text(old_account->source);
-       }
-
-       _ACCOUNT_FREE(new_account->package_name);
-       new_account->package_name = _account_dup_text(old_account->package_name);
-
-       // access token
-       if (!new_account->access_token) {
-               if (old_account->access_token)
-                       new_account->access_token = _account_dup_text(old_account->access_token);
-       }
-
-       // user text
-       int i;
-       for (i = 0; i < USER_TXT_CNT; i++) {
-               if (!new_account->user_data_txt[i]) {
-                       if (old_account->user_data_txt[i])
-                               new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
-               }
-       }
-
-       // auth type
-       if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
-               new_account->auth_type = old_account->auth_type;
-
-       // secret
-       if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
-               new_account->secret = old_account->secret;
-
-       // sync support
-       if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
-               new_account->sync_support = old_account->sync_support;
-
-       // user int
-       for (i = 0; i < USER_INT_CNT; i++) {
-               if (new_account->user_data_int[i] == 0)
-                               new_account->user_data_int[i] = old_account->user_data_int[i];
-       }
+       _account_compare_new_and_old_account_properties(&new_account, old_account);
 
 CATCH:
        if (old_account)
@@ -1857,134 +1798,9 @@ CATCH:
        return error_code;
 }
 
-int account_server_empty_deleted_account_info_tables(const char* del_account_table, const char* del_capability_table,
-                                       const char* del_account_custom_table)
-{
-       _INFO("account_server_empty_deleted_account_info_tables start");
-
-       account_stmt hstmt = NULL;
-       int error_code = _ACCOUNT_ERROR_NONE;
-       int rc = 0;
-       char query[ACCOUNT_SQL_LEN_MAX] = {0, };
-
-       ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
-
-       rc = _account_begin_transaction(g_hAccountDB);
-       _INFO("after _account_begin_trasaction");
-
-       if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
-               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
-               return _ACCOUNT_ERROR_PERMISSION_DENIED;
-       }
-
-       if (rc == _ACCOUNT_ERROR_DATABASE_BUSY) {
-               ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
-               return _ACCOUNT_ERROR_DATABASE_BUSY;
-       } else if (rc != _ACCOUNT_ERROR_NONE) {
-               ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", rc);
-               return rc;
-       }
-
-       /*clear deleted account table*/
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_account_table);
-
-       _INFO("clearing deleted-account-table - query[%s]", query);
-
-       hstmt = _account_prepare_query(g_hAccountDB, query);
-       if ((_account_db_err_code(g_hAccountDB) == SQLITE_PERM)) {
-               _account_end_transaction(g_hAccountDB, false);
-               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
-               return _ACCOUNT_ERROR_PERMISSION_DENIED;
-       }
-
-       if (hstmt == NULL) {
-               _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
-               error_code = _ACCOUNT_ERROR_DB_FAILED;
-               goto END;
-       }
-
-       rc = _account_query_step(hstmt);
-       if (rc != SQLITE_DONE) {
-               _ERR("The record isn't found");
-               error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
-               goto END;
-       }
-
-       rc = _account_query_finalize(hstmt);
-       if (rc != _ACCOUNT_ERROR_NONE) {
-               _ERR("finalize error");
-               error_code = rc;
-               goto END;
-       }
-
-       /*clear deleted capability table*/
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_capability_table);
-
-       _INFO("clearing deleted-capability-table - query[%s]", query);
-
-       hstmt = _account_prepare_query(g_hAccountDB, query);
-       if (hstmt == NULL) {
-               _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
-               error_code = _ACCOUNT_ERROR_DB_FAILED;
-               goto END;
-       }
-
-       rc = _account_query_step(hstmt);
-       if (rc != SQLITE_DONE) {
-               _ERR("The record isn't found");
-               error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
-               goto END;
-       }
-
-       rc = _account_query_finalize(hstmt);
-       if (rc != _ACCOUNT_ERROR_NONE) {
-               _ERR("finalize error");
-               error_code = rc;
-               goto END;
-       }
-
-       /*clear deleted account_custom table*/
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_account_custom_table);
-
-       _INFO("clearing deleted-account-custom-table - query[%s]", query);
-
-       hstmt = _account_prepare_query(g_hAccountDB, query);
-       if (hstmt == NULL) {
-               _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
-               error_code = _ACCOUNT_ERROR_DB_FAILED;
-               goto END;
-       }
-
-       rc = _account_query_step(hstmt);
-       if (rc != SQLITE_DONE) {
-               _ERR("The record isn't found");
-               error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
-               goto END;
-       }
-
-       rc = _account_query_finalize(hstmt);
-       if (rc != _ACCOUNT_ERROR_NONE) {
-               _ERR("finalize error");
-               error_code = rc;
-               goto END;
-       }
-
-       rc = _account_end_transaction(g_hAccountDB, true);
-       if (rc != _ACCOUNT_ERROR_NONE)
-               _ERR("clear deleted account tables - fail [%d]", rc);
-
-       hstmt =  NULL;
-       _INFO("account_server_empty_deleted_account_info_tables end");
-END:
-       return error_code;
-}
-
-int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record, bool *IsDeletedAccount)
+int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record, bool query_del_acc)
 {
-       _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
+       _INFO("_account_query_account_by_account_id() start, account_db_id=[%d], query_del_acc=[%d]", account_db_id, query_del_acc);
 
        int error_code = _ACCOUNT_ERROR_NONE;
        account_stmt hstmt = NULL;
@@ -1999,7 +1815,10 @@ int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id,
 
        /* prepare query for account-table */
        ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
-       ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
+       if (!query_del_acc)
+               ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
+       else
+               ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", DELETED_ACCOUNT_TABLE, account_db_id);
 
        _INFO("after _account_prepare_query, rc=[%d]", rc);
 
@@ -2017,36 +1836,7 @@ int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id,
 
        ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
 
-       /*querying information from deleted_account_table*/
-       if (rc != SQLITE_ROW) {
-
-               /*free previous hstmt*/
-               if (hstmt != NULL) {
-                       rc = _account_query_finalize(hstmt);
-                       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
-                       hstmt = NULL;
-               }
-
-               ACCOUNT_DEBUG("checking account info in deleted_account_table");
-
-               /* query from deleted-account-info table */
-               ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
-               ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", DELETED_ACCOUNT_TABLE, account_db_id);
-
-               hstmt = _account_prepare_query(g_hAccountDB, query);
-
-               rc = _account_db_err_code(g_hAccountDB);
-               if (rc == SQLITE_PERM) {
-                       ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
-                       return _ACCOUNT_ERROR_PERMISSION_DENIED;
-               }
-
-               rc = _account_query_step(hstmt);
-               ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
-
-               ACCOUNT_DEBUG("getting account info from deleted_account_table");
-               *IsDeletedAccount = true;
-       }
+       ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
 
        while (rc == SQLITE_ROW) {
                ACCOUNT_DEBUG("before _account_convert_column_to_account");
@@ -2062,11 +1852,11 @@ int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id,
        ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
 
        ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
-       _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, *IsDeletedAccount);
+       _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
        ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
 
        ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
-       _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, *IsDeletedAccount);
+       _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
        ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
 
        hstmt = NULL;
@@ -3418,7 +3208,7 @@ static int _account_type_update_provider_feature(sqlite3 *account_db_handle, acc
                return _ACCOUNT_ERROR_NONE;
        }
 
-       ACCOUNT_DEBUG("app id", app_id);
+       ACCOUNT_DEBUG("app id[%s]", app_id);
 
        ACCOUNT_MEMSET(query, 0x00, sizeof(query));
 
@@ -3436,6 +3226,8 @@ static int _account_type_update_provider_feature(sqlite3 *account_db_handle, acc
 
        if (rc != SQLITE_DONE) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DB_FAILED;
        }
        rc = _account_query_finalize(hstmt);
@@ -3504,6 +3296,8 @@ static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s
 
        if (rc != SQLITE_DONE) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DB_FAILED;
        }
        rc = _account_query_finalize(hstmt);
@@ -4135,7 +3929,7 @@ int _account_type_query_by_provider_feature_from_global_db(const char* key, GSLi
        rc = _account_query_finalize(hstmt);
        if (rc != _ACCOUNT_ERROR_NONE) {
                _account_type_gslist_account_type_free(account_type_list);
-               ACCOUNT_ERROR("finalize error(%s)", rc);
+               ACCOUNT_ERROR("finalize error(%d)", rc);
                error_code = rc;
                goto CATCH;
        }
@@ -4166,7 +3960,7 @@ CATCH:
        if (hstmt != NULL) {
                rc = _account_query_finalize(hstmt);
                if (rc != _ACCOUNT_ERROR_NONE) {
-                       ACCOUNT_ERROR("finalize error(%s)", rc);
+                       ACCOUNT_ERROR("finalize error(%d)", rc);
                        return rc;
                }
                hstmt = NULL;
@@ -4240,7 +4034,7 @@ GSList* _account_type_query_by_provider_feature(const char* key, int *error_code
        rc = _account_query_finalize(hstmt);
        if (rc != _ACCOUNT_ERROR_NONE) {
                _account_type_gslist_account_type_free(account_type_list);
-               ACCOUNT_ERROR("finalize error(%s)", rc);
+               ACCOUNT_ERROR("finalize error(%d)", rc);
                *error_code = rc;
                goto CATCH;
        }
@@ -4642,7 +4436,7 @@ static int _account_insert_custom(account_s *account, int account_id)
        rc = _account_get_record_count(g_hAccountDB, query);
 
        if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
-               ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
+               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
                return _ACCOUNT_ERROR_PERMISSION_DENIED;
        }
 
@@ -4743,9 +4537,13 @@ static int _account_update_custom(account_s *account, int account_id)
 
        if (rc == SQLITE_BUSY) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DATABASE_BUSY;
        } else if (rc != SQLITE_DONE) {
                ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
+               if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
+                       _ERR("finalize error - account_query_step() failed");
                return _ACCOUNT_ERROR_DB_FAILED;
        }