[Refactoring account-delete-account] 54/185054/2
authorAbhishek Vijay <abhishek.v@samsung.com>
Wed, 25 Jul 2018 14:06:39 +0000 (19:36 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Thu, 26 Jul 2018 09:52:12 +0000 (15:22 +0530)
Change-Id: I635d4f5a523111cba9c2e261c31cf1c7746938f2
Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
common/include/account_db_helper.h
common/src/account_db_helper.c

index 34b91c0bcaf9a7b9ebff5f6f5036c9bb64dd08ca..1a6deff9857e566275ffcb948c3fb9326eef1a87 100644 (file)
@@ -179,6 +179,8 @@ GList *_account_query_account_by_package_name(sqlite3 *account_db_handle, const
 
 int _account_check_duplicated(sqlite3 *account_db_handle, account_s *data, const char *verified_appid, uid_t uid);
 
+int _account_delete_table(sqlite3 *account_db_handle, account_stmt *phstmt, GSList *account_id_list,
+                                       const char *package_name, const char *app_id, const char* table);
 int _account_delete_account_by_package_name(sqlite3 *account_db_handle, const char *package_name, gboolean permission, int pid, uid_t uid);
 
 
index 12240306792a2609cda9dc5b36c558ee0e38da2b..5516c8e9a84622263408d6d89421befed0a5ffb9 100644 (file)
@@ -1423,18 +1423,80 @@ int _account_check_duplicated(sqlite3 *account_db_handle, account_s *data, const
        return _ACCOUNT_ERROR_NONE;
 }
 
+int _account_delete_table(sqlite3 *account_db_handle, account_stmt *phstmt,
+               GSList *account_id_list, const char *package_name, const char *app_id, const char* table)
+{
+       _INFO("_account_delete_table start - table[%s]", table);
+       int error_code = _ACCOUNT_ERROR_NONE;
+       account_stmt hstmt = *phstmt;
+       int binding_count = 1;
+       int rc = 0;
+       char query[ACCOUNT_SQL_LEN_MAX] = {0, };
+
+       /*function params like "phstmt", "account_id_list", "package_name" & "app_id" can be NULL in
+        * case of table deletion performed either by package_name or app_id; hence no need to check them for NULL*/
+
+       ACCOUNT_RETURN_VAL((account_db_handle != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
+       ACCOUNT_RETURN_VAL((table != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("table is null!"));
 
+       if ((g_strcmp0(table, CAPABILITY_TABLE) == 0) || (g_strcmp0(table, ACCOUNT_TABLE) == 0)) {
+               ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", table);
+       } else { /*ACCOUNT_CUSTOM_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE, ACCOUNT_TYPE_TABLE*/
+               ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", table);
+       }
+
+       hstmt = _account_prepare_query(account_db_handle, query);
+
+       if ((_account_db_err_code(account_db_handle) == SQLITE_PERM) && (g_strcmp0(table, ACCOUNT_CUSTOM_TABLE) == 0)) {
+               _account_end_transaction(account_db_handle, false);
+               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
+               g_slist_free_full(account_id_list, g_free);
+               return _ACCOUNT_ERROR_PERMISSION_DENIED;
+       }
+
+       if (hstmt == NULL) {
+               _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle));
+               error_code = _ACCOUNT_ERROR_DB_FAILED;
+               goto END;
+       }
+
+       if (package_name != NULL)
+               _account_query_bind_text(hstmt, binding_count++, package_name);
+       else if (app_id != NULL)
+               _account_query_bind_text(hstmt, binding_count++, app_id);
+
+       rc = _account_query_step(hstmt);
+       if (rc != SQLITE_DONE) {
+               _ERR("The record isn't found - package_name[%s]\n", package_name);
+               error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
+               goto END;
+       }
+
+       rc = _account_query_finalize(hstmt);
+       if (rc != _ACCOUNT_ERROR_NONE) {
+               if (package_name != NULL) {
+                       _account_end_transaction(account_db_handle, false);
+                       g_slist_free_full(account_id_list, g_free);
+               }
+               _ERR("finalize error");
+               error_code = rc;
+               goto END;
+       }
+
+       hstmt =  NULL;
+       _INFO("_account_delete_table end");
+END:
+       return error_code;
+}
 
 int _account_delete_account_by_package_name(sqlite3 *account_db_handle, const char *package_name, gboolean permission, int pid, uid_t uid)
 {
        _INFO("_account_delete_account_by_package_name");
        int error_code = _ACCOUNT_ERROR_NONE;
        account_stmt hstmt = NULL;
-       char query[ACCOUNT_SQL_LEN_MAX] = {0, };
        int rc = 0;
        int ret_transaction = 0;
        bool is_success = FALSE;
-       int binding_count = 1;
        GSList *account_id_list = NULL;
        int ret = -1;
 
@@ -1532,77 +1594,41 @@ int _account_delete_account_by_package_name(sqlite3 *account_db_handle, const ch
        }
 
        _INFO("start delete custom table");
-       /* delete custom table */
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
 
-       hstmt = _account_prepare_query(account_db_handle, query);
-
-       if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
-               _account_end_transaction(account_db_handle, false);
-               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
-               g_slist_free_full(account_id_list, g_free);
-               return _ACCOUNT_ERROR_PERMISSION_DENIED;
+       /* delete custom table */
+       error_code = _account_delete_table(account_db_handle, &hstmt, account_id_list, package_name, NULL, ACCOUNT_CUSTOM_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
        }
 
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       binding_count = 1;
-       _account_query_bind_text(hstmt, binding_count++, package_name);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE),
-               { _account_end_transaction(account_db_handle, false); g_slist_free_full(account_id_list, g_free); }, rc, ("finalize error"));
-       hstmt = NULL;
-
        _INFO("start delete capability table");
-       /* delete capability table */
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
 
-       hstmt = _account_prepare_query(account_db_handle, query);
-
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       binding_count = 1;
-       _account_query_bind_text(hstmt, binding_count++, package_name);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE),
-               { _account_end_transaction(account_db_handle, false); g_slist_free_full(account_id_list, g_free); }, rc, ("finalize error"));
-       hstmt = NULL;
+       /* delete capability table */
+       error_code = _account_delete_table(account_db_handle, &hstmt, account_id_list, package_name, NULL, CAPABILITY_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
+       }
 
        _INFO("start delete account table");
        /* delete account table */
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
-
-       hstmt = _account_prepare_query(account_db_handle, query);
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       binding_count = 1;
-       _account_query_bind_text(hstmt, binding_count++, package_name);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found, rc=%d\n", rc));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE),
-               { _account_end_transaction(account_db_handle, false); g_slist_free_full(account_id_list, g_free); }, rc, ("finalize error"));
+       error_code = _account_delete_table(account_db_handle, &hstmt, account_id_list, package_name, NULL, ACCOUNT_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
+       }
        is_success = TRUE;
 
-       hstmt = NULL;
-
 CATCH:
        if (hstmt != NULL) {
                rc = _account_query_finalize(hstmt);
@@ -1962,7 +1988,6 @@ int _account_type_delete_by_app_id(sqlite3 *account_db_handle, const char* app_i
        int rc = 0;
        int count = -1;
        int ret_transaction = 0;
-       int binding_count = 1;
        bool is_success = FALSE;
 
        ACCOUNT_RETURN_VAL((account_db_handle != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
@@ -1994,67 +2019,38 @@ int _account_type_delete_by_app_id(sqlite3 *account_db_handle, const char* app_i
                return ret_transaction;
        }
 
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
-
-       hstmt = _account_prepare_query(account_db_handle, query);
-
-       if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
-               ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
-               return _ACCOUNT_ERROR_PERMISSION_DENIED;
+       /*delete label table*/
+       error_code = _account_delete_table(account_db_handle, &hstmt, NULL, NULL, app_id, LABEL_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
        }
 
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       _account_query_bind_text(hstmt, binding_count++, app_id);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
-       hstmt = NULL;
-
-       binding_count = 1;
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
-
-       hstmt = _account_prepare_query(account_db_handle, query);
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       _account_query_bind_text(hstmt, binding_count++, app_id);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
+       /*delete provider feature table*/
+       error_code = _account_delete_table(account_db_handle, &hstmt, NULL, NULL, app_id, PROVIDER_FEATURE_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
+       }
        is_success = TRUE;
 
-       hstmt = NULL;
-
-       binding_count = 1;
-       ACCOUNT_MEMSET(query, 0, sizeof(query));
-
-       ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
-
-       hstmt = _account_prepare_query(account_db_handle, query);
-       ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
-                       ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(account_db_handle)));
-
-       _account_query_bind_text(hstmt, binding_count++, app_id);
-
-       rc = _account_query_step(hstmt);
-       ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
-
-       rc = _account_query_finalize(hstmt);
-       ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
+       /*delete account type table*/
+       error_code = _account_delete_table(account_db_handle, &hstmt, NULL, NULL, app_id, ACCOUNT_TYPE_TABLE);
+       if ((error_code == _ACCOUNT_ERROR_DB_FAILED) || (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
+               _ERR("account-custom-table deletion failed - db error[%s]", error_code);
+               goto CATCH;
+       } else if (error_code != _ACCOUNT_ERROR_NONE) {
+               _ERR("account-custom-table deletion failed - error[%s]", error_code);
+               return error_code;
+       }
        is_success = TRUE;
 
-       hstmt = NULL;
-
 CATCH:
        if (hstmt != NULL) {
                rc = _account_query_finalize(hstmt);