[0.1.21] Fix format specifiers
[platform/core/account/account-manager.git] / server / src / account-server-db.c
index 75bd125..8ba8eec 100644 (file)
@@ -41,8 +41,6 @@
 #include "account_type.h"
 #include "account-server-db.h"
 
-//typedef sqlite3_stmt* account_stmt;
-
 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
 
 #define EMAIL_APPID "email-setting-efl"
@@ -61,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);
@@ -237,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;
        }
 
@@ -340,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;
        }
 
@@ -574,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);
@@ -655,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;
        }
 
@@ -745,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);
-       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);
-       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);
@@ -1243,88 +1256,14 @@ static int _account_compare_old_record(account_s *new_account, int account_id)
        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);
+       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);
+       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)
@@ -1762,8 +1701,8 @@ GSList* _account_db_query_all(int pid, uid_t uid)
        for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
                account_s *account = NULL;
                account = (account_s*)iter->data;
-               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
-               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
+               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account, false);
+               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account, false);
        }
 
 CATCH:
@@ -1859,36 +1798,44 @@ CATCH:
        return error_code;
 }
 
-int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
+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;
-       char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
-       int                             rc = 0;
+       int error_code = _ACCOUNT_ERROR_NONE;
+       account_stmt hstmt = NULL;
+       char query[ACCOUNT_SQL_LEN_MAX] = {0, };
+       int rc = 0;
 
        ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
        ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
        ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
 
+       ACCOUNT_DEBUG("starting db operations");
+
+       /* prepare query for account-table */
        ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
+       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);
 
-       ACCOUNT_DEBUG("starting db operations");
+       _INFO("after _account_prepare_query, rc=[%d]", rc);
 
-       ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
        hstmt = _account_prepare_query(g_hAccountDB, query);
-       rc = _account_db_err_code(g_hAccountDB);
-       _INFO("after _account_prepare_query, rc=[%d]", rc);
 
+       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;
        }
 
        ACCOUNT_DEBUG("before _account_query_step");
+
        rc = _account_query_step(hstmt);
+
        ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
+
        ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
 
        while (rc == SQLITE_ROW) {
@@ -1905,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);
+       _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);
+       _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;
@@ -2026,8 +1973,8 @@ GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_
 
                account_s *testaccount = (account_s*)iter->data;
 
-               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
-               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
+               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
+               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
        }
 
        *error_code = _ACCOUNT_ERROR_NONE;
@@ -2142,8 +2089,8 @@ _account_query_account_by_capability(int pid, uid_t uid, const char* capability_
                //account = (account_h)iter->data;
                account_s* testaccount = (account_s*)iter->data;
 
-               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
-               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
+               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
+               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
 
        }
 
@@ -2255,8 +2202,8 @@ GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char*
        for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
                account_s* testaccount = (account_s*)iter->data;
 
-               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
-               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
+               _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
+               _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
 
        }
 
@@ -2325,7 +2272,7 @@ int account_server_delete_account_by_package_name(const char* package_name, bool
 
 int _account_delete(int pid, uid_t uid, int account_id)
 {
-       int                             error_code = _ACCOUNT_ERROR_NONE;
+       int error_code = _ACCOUNT_ERROR_NONE;
        account_stmt    hstmt = NULL;
        char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
        int                             rc = 0;
@@ -2398,6 +2345,7 @@ int _account_delete(int pid, uid_t uid, int account_id)
                return ret_transaction;
        }
 
+       /* capability table */
        ACCOUNT_MEMSET(query, 0x00, sizeof(query));
        ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
 
@@ -2420,8 +2368,8 @@ int _account_delete(int pid, uid_t uid, int account_id)
        ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
        hstmt = NULL;
 
+       /* account table */
        ACCOUNT_MEMSET(query, 0, sizeof(query));
-
        ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
 
        hstmt = _account_prepare_query(g_hAccountDB, query);
@@ -2437,7 +2385,6 @@ int _account_delete(int pid, uid_t uid, int account_id)
 
        /* delete custom data */
        ACCOUNT_MEMSET(query, 0, sizeof(query));
-
        ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
 
        hstmt = _account_prepare_query(g_hAccountDB, query);
@@ -2522,8 +2469,8 @@ static int _account_query_account_by_username_and_package(const char* username,
 
        rc = _account_query_finalize(hstmt);
        ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
-       _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
-       _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
+       _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, false);
+       _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, false);
 
        hstmt = NULL;
        error_code = _ACCOUNT_ERROR_NONE;
@@ -3261,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));
 
@@ -3279,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);
@@ -3347,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);
@@ -3978,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;
        }
@@ -4009,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;
@@ -4083,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;
        }
@@ -4485,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;
        }
 
@@ -4586,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;
        }