Remove duplicate code to check table creation
authorAbhay agarwal <ay.agarwal@samsung.com>
Fri, 6 Sep 2019 11:15:03 +0000 (16:45 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 16 Sep 2019 12:01:29 +0000 (21:01 +0900)
Change-Id: I8973346679cfb0fd58cf6e8c6889cdb697fab885
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/include/ua-manager-database.h
ua-daemon/src/ua-manager-db.c

index 17d6708..caf8d3c 100644 (file)
@@ -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) { \
index 0782a3a..71e4bb5 100644 (file)
@@ -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.