From aa8ffebbbf4435b73c14532ab584256ce0d9350d Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Fri, 6 Sep 2019 16:45:03 +0530 Subject: [PATCH] Remove duplicate code to check table creation Change-Id: I8973346679cfb0fd58cf6e8c6889cdb697fab885 Signed-off-by: Abhay agarwal --- ua-daemon/include/ua-manager-database.h | 6 --- ua-daemon/src/ua-manager-db.c | 67 +++++++++++++-------------------- 2 files changed, 26 insertions(+), 47 deletions(-) diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 17d6708..caf8d3c 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -29,12 +29,6 @@ extern "C" { #define SQLITE_BUSY_TIMEOUT 500000 - -/* - * 15) If possible, - * change these four phrases to sqlite_exec3 and make it an atomic operation. - */ - /* Helper macros */ #define EXEC(error_code, command, handle_error) do { \ if (error_code != command) { \ diff --git a/ua-daemon/src/ua-manager-db.c b/ua-daemon/src/ua-manager-db.c index 0782a3a..71e4bb5 100644 --- a/ua-daemon/src/ua-manager-db.c +++ b/ua-daemon/src/ua-manager-db.c @@ -193,31 +193,6 @@ is_table_existing_done: return result; } -static int __uam_db_check_table_creation(void) -{ - FUNC_ENTRY; - int error_code = UAM_ERROR_DB_FAILED; - - if (!__uam_db_is_table_existing(UAM_DB_USERDATA_TABLE)) - return error_code; - - if (!__uam_db_is_table_existing(UAM_DB_DEVICES_TABLE)) - return error_code; - - if (!__uam_db_is_table_existing(UAM_DB_SERVICES_TABLE)) - return error_code; - - if (!__uam_db_is_table_existing(UAM_DB_DEVICE_SERVICES_TABLE)) - return error_code; - - if (!__uam_db_is_table_existing(UAM_DB_IBEACON_ADV_TABLE)) - return error_code; - - UAM_DBG("Successfully verified table creation"); - FUNC_EXIT; - return UAM_ERROR_NONE; -} - static int __uam_db_check_integrity_cb( void *err, int count, char **data, char **columns) { @@ -268,25 +243,32 @@ static int __uam_db_create_table(const char *table_name) return ret; } -static void __uam_db_prepare_table(void) +static int __uam_db_check_table_creation(void) { + FUNC_ENTRY; + int error_code = UAM_ERROR_DB_FAILED; + /* * 4) Each __is_table_existing() open DB file again and again.. it is redundant */ if (!__uam_db_is_table_existing(UAM_DB_USERDATA_TABLE)) - __uam_db_create_table(CREATE_USERDATA_TABLE); + retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_USERDATA_TABLE), error_code); if (!__uam_db_is_table_existing(UAM_DB_DEVICES_TABLE)) - __uam_db_create_table(CREATE_DEVICES_TABLE); + retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_DEVICES_TABLE), error_code); if (!__uam_db_is_table_existing(UAM_DB_SERVICES_TABLE)) - __uam_db_create_table(CREATE_SERVICES_TABLE); + retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_SERVICES_TABLE), error_code); if (!__uam_db_is_table_existing(UAM_DB_DEVICE_SERVICES_TABLE)) - __uam_db_create_table(CREATE_DEVICE_SERVICES_TABLE); + retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_DEVICE_SERVICES_TABLE), error_code); if (!__uam_db_is_table_existing(UAM_DB_IBEACON_ADV_TABLE)) - __uam_db_create_table(CREATE_IBEACON_ADV_TABLE); + retv_if(UAM_ERROR_NONE != __uam_db_create_table(CREATE_IBEACON_ADV_TABLE), error_code); + + UAM_DBG("Successfully verified table creation"); + FUNC_EXIT; + return UAM_ERROR_NONE; } static int __uam_db_verify() @@ -308,6 +290,7 @@ static int __uam_db_verify() /* set locking mode */ retv_if(UAM_ERROR_NONE != __uam_db_set_locking_mode(), UAM_ERROR_DB_FAILED); + UAM_DBG("Successfully verified database"); return UAM_ERROR_NONE; } @@ -315,20 +298,22 @@ int _uam_db_initialize_once(void) { FUNC_ENTRY; char *sql; - /* open database */ - retv_if(SQLITE_OK != __uam_db_open(), UAM_ERROR_DB_FAILED); - if (UAM_ERROR_NONE != __uam_db_verify()) { - UAM_ERR("Failed to verify database"); - sqlite3_close(database); - unlink(DATABASE_FULL_PATH); - database = NULL; + int max_retries = 2; - /* Reopen database */ + do{ + /* open database */ retv_if(SQLITE_OK != __uam_db_open(), UAM_ERROR_DB_FAILED); - } - __uam_db_prepare_table(); + if (UAM_ERROR_NONE != __uam_db_verify()) { + UAM_ERR("Failed to verify database"); + sqlite3_close(database); + unlink(DATABASE_FULL_PATH); + database = NULL; + } else { + break; + } + } while(max_retries--); /* * 3) Default journal mode is PERIST so it seems use-less. -- 2.7.4