Move common db function from user table file
authorAbhay agarwal <ay.agarwal@samsung.com>
Thu, 5 Sep 2019 10:00:34 +0000 (15:30 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 16 Sep 2019 12:00:12 +0000 (21:00 +0900)
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
This patch also refactor the check_integrity function in multiple useful functions
Changes done as per db review

Change-Id: Ie9d0c08c9b2c64ac9d289cd73526ee4232f615ab

ua-daemon/include/ua-manager-common.h
ua-daemon/include/ua-manager-database.h
ua-daemon/src/ua-manager-adv-db.c
ua-daemon/src/ua-manager-common.c
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-db.c
ua-daemon/src/ua-manager-device-db.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-service-db.c
ua-daemon/src/ua-manager-user-db.c

index b9c2fd4..4bf67b4 100644 (file)
@@ -127,7 +127,7 @@ int _uam_register_app_info(char *sender, uam_app_info_s app_info);
 
 int _uam_deregister_app_info(char *sender, uam_app_info_s app_info);
 
-size_t _uam_getFilesize(const char* filename);
+size_t _uam_get_file_size(const char* filename);
 
 #ifdef __cplusplus
 }
index 0761850..4361d31 100644 (file)
@@ -68,8 +68,27 @@ extern "C" {
        } \
 } while (0)
 
+#define DB_CHECK(expr) \
+       do { \
+               if (expr) { \
+                       UAM_ERR("(%s) return", #expr); \
+                       unlink(DATABASE_FULL_PATH); \
+                       database = NULL; \
+                       /* Reopen database */ \
+                       retv_if(SQLITE_OK != __uam_db_open(), UAM_ERROR_DB_FAILED); \
+                       __uam_db_prepare_table(); \
+               } \
+       } while (0)
+
 /* Helper Functions */
-int _ua_db_initialize_once(void);
+int _uam_db_initialize_once(void);
+
+/* db init/deinit */
+int _uam_db_initialize(void);
+int _uam_db_deinitialize(void);
+
+/* delete operations */
+int _uam_db_clear(void);
 
 /* USER QUERIES */
 
@@ -80,8 +99,8 @@ typedef struct db_user_info {
 } db_user_info_t;
 
 /* db init/deinit */
-int _ua_db_initialize(void);
-int _ua_db_deinitialize(void);
+int _uam_user_db_initialize(void);
+int _uam_user_db_deinitialize(void);
 
 /* select operations */
 GSList *_ua_db_get_all_users(void);
@@ -89,7 +108,7 @@ int _ua_db_get_user(int user_id, db_user_info_t *info);
 
 /* delete operations */
 int _ua_db_delete_by_user_id(int user_id);
-int _ua_db_clear(void);
+int _uam_user_db_clear(void);
 
 /* insert */
 int _ua_db_insert_user_info(int* user_id, const char *name, const char *account);
@@ -114,7 +133,7 @@ int _uam_db_get_device_number(
        const char *device_id, int tech_type, const char *address, int *device_number);
 
 /* delete operations */
-int _ua_device_db_clear(void);
+int _uam_device_db_clear(void);
 int _ua_device_db_delete_device_info(
        const char *device_id, int tech_type, const char *address);
 
@@ -131,6 +150,7 @@ int _ua_device_db_update_device_ip_address(
        char *device_id, int tech_type, char *address, char *ip_address);
 int _ua_device_db_update_device_device(char *device_id, int tech_type,
        char *address, char *ip, char os_type, char discriminant, uam_ble_payload_s payload);
+
 /* SERVICE QUERIES */
 
 typedef struct {
index 29405ca..c57f9e6 100644 (file)
@@ -130,7 +130,7 @@ static void __uam_adv_table_info_finalize(void)
 static sqlite3 *__uam_adv_db_get_database(void)
 {
        if (database == NULL)
-               _ua_db_initialize_once();
+               _uam_db_initialize_once();
 
        return database;
 }
index 71fec4d..7989aae 100644 (file)
@@ -211,7 +211,7 @@ const char *_uam_get_sensor_str(uas_plugin_id_e ids)
        }
 }
 
-size_t _uam_getFilesize(const char* filename)
+size_t _uam_get_file_size(const char* filename)
 {
        struct stat st;
 
index 526e7b0..5b81d54 100644 (file)
@@ -1974,7 +1974,7 @@ int _uam_core_init(void)
        GSList *l;
 
        /* Init database */
-       _ua_db_initialize();
+       _uam_db_initialize();
 
        /* Reset detecton window to default */
        detection_window = UAM_DETECTION_WINDOW_DEFAULT;
@@ -2097,7 +2097,7 @@ void _uam_core_deinit(void)
        GSList *l;
 
        /* de-init database */
-       _ua_db_deinitialize();
+       _uam_db_deinitialize();
 
        /* Reset detecton window to default */
        detection_window = UAM_DETECTION_WINDOW_DEFAULT;
@@ -2662,9 +2662,9 @@ void _uam_core_reset_database(void)
        FUNC_ENTRY;
        int ret;
 
-       ret = _ua_db_clear();
+       ret = _uam_db_clear();
        if (UAM_ERROR_NONE != ret) {
-               UAM_ERR("_ua_db_clear failed with %s",
+               UAM_ERR("_uam_db_clear failed with %s",
                                _uam_manager_error_to_str(ret));
                return;
        }
index 9d8ed98..0e7f47a 100644 (file)
 
 sqlite3 *database;
 
-static int __ua_db_busy(void *user, int attempts)
+static int __uam_db_exec_sql(char *sql, void *cb)
 {
        FUNC_ENTRY;
-       UAM_ERR("DB locked by another process, attempts number %d",
-                       attempts);
-
-       /*
-        * 8) It can be usleep infinitely, so we can stop this callback (return 0)
-        *    For instance, usually, after 10 time sleeping.
-        *   The attemts variable tells you how many attempts.
-        */
-       usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
-       FUNC_EXIT;
-       return 1;
-}
-
-static int __uam_db_check_integrity_cb(
-       void *err, int count, char **data, char **columns)
-{
-       FUNC_ENTRY;
-       int i;
-
-       /*
-        * 7) count is column count so we just check which count is 'ok'
-        *    we can check integrity 2 ways:
-        *    - 1st is check column like us
-        *    - 2ne is check recodes are okey using sqlite3_step (ref: avocd_db_check_integrity())
-        *      //TIZEN/[MAIN]/[ONEPROD_Prj]/[FET]/[COSMOS]/SYSTEM/Avoc/avoc-service/avocd/src/db/avocd_db.cpp
-        *    which one is okey.
-        */
-       for (i = 0; i < count; i++) {
-               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
-               if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok"))
-                       return 0; /* 8) In order to enhance readabiity we can explicitly 0 means SUCCESS */
-       }
-       return 1; /* 9) In order to enhance readabiity we can explicitly 0 means DATA_ABORT */
-}
-
-static int __uam_db_check_table_cb(
-       void *err, int count, char **data, char **columns)
-{
-       FUNC_ENTRY;
-       int i;
-
-       for (i = 0; i < count; i++) {
-               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
-               if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1"))
-                       return 0;
-       }
-       return 1;
-}
-
-static int __check_integrity(void)
-{
-       FUNC_ENTRY;
-       int res = 0;
-       char *sql = NULL;
-
-       /* 11) Please don't open DB file multiple times */
-       res = sqlite3_open(DATABASE_FULL_PATH, &database);
-       if (res != SQLITE_OK) {
-               UAM_ERR("Error in opening database %s: %s",
-                       DATABASE_FULL_PATH, sqlite3_errmsg(database));
-               FUNC_EXIT;
-               return UAM_ERROR_DB_FAILED;
-       }
-       UAM_DBG("Successfully opened database");
-
-       /* 12) It is need to check file size is 0 at first time */
-       size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
-       if (db_size == 0) {
-               UAM_ERR("Database size is 0");
-               FUNC_EXIT;
-               return UAM_ERROR_DB_FAILED;
-       }
-       UAM_DBG("Database size: %zd", db_size);
-
-       /*
-        * 10) In oder to enhance SAM score, please replace below repeat block as a function
-        */
-       res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify database integrity %s",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
-               FUNC_EXIT;
-               return UAM_ERROR_DB_FAILED;
-       }
-       UAM_DBG("Successfully verified database integrity");
-
-       /* REDUNDANT Start */
-       sql = sqlite3_mprintf(
-               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'", /* 14) If possible please this SQL text as MACRO */
-               UAM_DB_USERDATA_TABLE);
-
-       res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
-       sqlite3_free(sql);
+       int sql_ret;
+       char *error = NULL;
 
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify table creation %s",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
-               FUNC_EXIT;
+       if (NULL == database) {
+               UAM_ERR("database is NULL");
                return UAM_ERROR_DB_FAILED;
        }
-       UAM_DBG("Successfully verified %s table creation", UAM_DB_USERDATA_TABLE);
-       /* REDUNDANT End */
-
-       /* REDUNDANT Start */
-       sql = sqlite3_mprintf(
-               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
-               UAM_DB_DEVICES_TABLE);
 
-       res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
-       sqlite3_free(sql);
-
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify table creation %s",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
-               FUNC_EXIT;
+       if (NULL == sql) {
+               UAM_ERR("sql is NULL");
                return UAM_ERROR_DB_FAILED;
        }
-       UAM_DBG("Successfully verified %s table creation", UAM_DB_DEVICES_TABLE);
-       /* REDUNDANT End */
 
-       /* REDUNDANT Start */
-       sql = sqlite3_mprintf(
-               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
-               UAM_DB_SERVICES_TABLE);
-
-       res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
-       sqlite3_free(sql);
-
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify table creation %s",
-                               sqlite3_errmsg(database));
+       sql_ret = sqlite3_exec(database, sql, cb, NULL, &error);
+       if (SQLITE_OK != sql_ret) {
+               UAM_ERR("Failed to execute sql: (%d) %s", sql_ret, error);
+               sqlite3_free(error);
                sqlite3_close(database);
                database = NULL;
-               FUNC_EXIT;
                return UAM_ERROR_DB_FAILED;
        }
-       UAM_DBG("Successfully verified %s table creation", UAM_DB_SERVICES_TABLE);
 
-       /* REDUNDANT Start */
-       sql = sqlite3_mprintf(
-               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
-               UAM_DB_IBEACON_ADV_TABLE);
+       return UAM_ERROR_NONE;
+}
 
-       res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
-       sqlite3_free(sql);
+static int __uam_db_open(void)
+{
+       FUNC_ENTRY;
+       int res = 0;
 
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify table creation %s",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
+       res = sqlite3_open_v2(DATABASE_FULL_PATH, &(database),
+                               SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+       if (SQLITE_OK != res) {
+               UAM_ERR("sqlite3_open_v2 failed, (%d)", res);
                FUNC_EXIT;
                return UAM_ERROR_DB_FAILED;
        }
-       UAM_DBG("Successfully verified %s table creation", UAM_DB_IBEACON_ADV_TABLE);
-       /* REDUNDANT End */
+       UAM_DBG("Successfully opened database");
 
-       /* REDUNDANT Start */
-       sql = sqlite3_mprintf(
-               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
-               UAM_DB_DEVICE_SERVICES_TABLE);
+       FUNC_EXIT;
+       return res;
+}
 
-       res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
-       sqlite3_free(sql);
+static int __uam_db_busy(void *data, int attempt)
+{
+       FUNC_ENTRY;
+       int* max_attempts;
 
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't verify table creation %s",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
-               FUNC_EXIT;
-               return UAM_ERROR_DB_FAILED;
-       }
-       UAM_DBG("Successfully verified %s table creation", UAM_DB_DEVICE_SERVICES_TABLE);
-       /* REDUNDANT End */
+       max_attempts = (int*)data;
 
-       /*
-        * 11) why this is required? If it is not needed, please remove it
-        */
-       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
-       if (res != SQLITE_OK) {
-               UAM_ERR("Can't set locking mode %s, skip set busy handler.",
-                               sqlite3_errmsg(database));
-               sqlite3_close(database);
-               database = NULL;
-               FUNC_EXIT;
-               return UAM_ERROR_DB_FAILED;
+       if (attempt < *max_attempts) {
+               UAM_WARN("DB locked, attempt number %d, Attempt Remaining %d",
+                       attempt, *max_attempts - attempt);
+               usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
+               return 1;
        }
 
+       UAM_ERR("DB locked by another process, attempts tried %d, Exit",
+                       attempt);
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return 0;
 }
 
-static bool __is_table_existing(const char *table)
+static bool __uam_db_is_table_existing(const char *table)
 {
        int ret;
        char *sql;
@@ -329,36 +193,82 @@ is_table_existing_done:
        return result;
 }
 
-static int __ua_create_table(const char *table_name)
+static int __uam_db_check_table_creation(void)
 {
-       int sql_ret;
+       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)
+{
+       FUNC_ENTRY;
+
+       UAM_DBG("%s = %s\n", columns[0], data[0] ? data[0] : "NULL");
+       if (!g_strcmp0(columns[0], "integrity_check") && !g_strcmp0(data[0], "ok"))
+               return SQLITE_OK;
+
+       return SQLITE_ERROR;
+}
+
+static int __uam_db_check_integrity(void)
+{
+       FUNC_ENTRY;
        char *sql = NULL;
-       char *error = NULL;
-       int ret = UAM_ERROR_NONE;
 
-       if (database == NULL) {
-               UAM_ERR("database is NULL");
-               return UAM_ERROR_DB_FAILED;
-       }
+       sql = sqlite3_mprintf("PRAGMA integrity_check", __uam_db_check_integrity_cb);
+       retv_if(UAM_ERROR_NONE != __uam_db_exec_sql(sql, NULL), UAM_ERROR_DB_FAILED);
+       sqlite3_free(sql);
 
-       sql = sqlite3_mprintf(table_name);
-       if (NULL == sql) {
-               UAM_ERR("sql is NULL");
-               ret = UAM_ERROR_DB_FAILED;
-       }
+       UAM_DBG("Successfully verified database integrity");
 
-       sql_ret = sqlite3_exec(database, sql, NULL, NULL, &error);
-       if (sql_ret != SQLITE_OK) {
-               UAM_ERR("sqlite3_exec failed, %d, %s", sql_ret, error);
-               ret = UAM_ERROR_DB_FAILED;
-               sqlite3_free(error);
-       }
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
+static int __uam_db_set_locking_mode(void)
+{
+       char *sql = NULL;
+
+       sql = sqlite3_mprintf("PRAGMA locking_mode = NORMAL");
+       retv_if(UAM_ERROR_NONE != __uam_db_exec_sql(sql, NULL), UAM_ERROR_DB_FAILED);
+       sqlite3_free(sql);
+
+       return UAM_ERROR_NONE;
+}
+
+static int __uam_db_create_table(const char *table_name)
+{
+       char *sql = NULL;
+       int ret = UAM_ERROR_NONE;
+
+       sql = sqlite3_mprintf(table_name);
+       retv_if(UAM_ERROR_NONE != __uam_db_exec_sql(sql, NULL), UAM_ERROR_DB_FAILED);
        sqlite3_free(sql);
 
        return ret;
 }
 
-static void __ua_prepare_table(void)
+static void __uam_db_prepare_table(void)
 {
        /*
         * 4) Each __is_table_existing() open DB file again and again.. it is redundant
@@ -366,75 +276,130 @@ static void __ua_prepare_table(void)
        if (!__is_table_existing(UAM_DB_USERDATA_TABLE))
                __ua_create_table(CREATE_USERDATA_TABLE);
 
-       if (!__is_table_existing(UAM_DB_DEVICES_TABLE))
-               __ua_create_table(CREATE_DEVICES_TABLE);
+       if (!__uam_db_is_table_existing(UAM_DB_DEVICES_TABLE))
+               __uam_db_create_table(CREATE_DEVICES_TABLE);
 
-       if (!__is_table_existing(UAM_DB_SERVICES_TABLE))
-               __ua_create_table(CREATE_SERVICES_TABLE);
+       if (!__uam_db_is_table_existing(UAM_DB_SERVICES_TABLE))
+               __uam_db_create_table(CREATE_SERVICES_TABLE);
 
-       if (!__is_table_existing(UAM_DB_DEVICE_SERVICES_TABLE))
-               __ua_create_table(CREATE_DEVICE_SERVICES_TABLE);
+       if (!__uam_db_is_table_existing(UAM_DB_DEVICE_SERVICES_TABLE))
+               __uam_db_create_table(CREATE_DEVICE_SERVICES_TABLE);
 
-       if (!__is_table_existing(UAM_DB_IBEACON_ADV_TABLE))
-               __ua_create_table(CREATE_IBEACON_ADV_TABLE);
+       if (!__uam_db_is_table_existing(UAM_DB_IBEACON_ADV_TABLE))
+               __uam_db_create_table(CREATE_IBEACON_ADV_TABLE);
 }
 
-int _ua_db_initialize_once(void)
+static int __uam_db_verify()
 {
        FUNC_ENTRY;
-       int sql_ret = SQLITE_OK;
-       char *error = NULL;
 
-       /*
-        * 1) Once open DB file then we don't need to re-open DB
-        *    but in our code, we open the same DB repeately.
-        */
-       sql_ret = sqlite3_open_v2(DATABASE_FULL_PATH, &(database),
-                               SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
-       if (SQLITE_OK != sql_ret) {
-               UAM_ERR("sqlite3_open_v2 failed, (%d)", sql_ret);
-               return UAM_ERROR_DB_FAILED;
-       }
+       /* check db size */
+       retv_if(0 == _uam_get_file_size(DATABASE_FULL_PATH), UAM_ERROR_DB_FAILED);
+
+       /* check table existance*/
+       retv_if(UAM_ERROR_NONE != __uam_db_check_table_creation(), UAM_ERROR_DB_FAILED);
 
        /*
         * 2) __check_integrity() should be executed before DB open
         *   Move __check_integrity() ahead of _ua_db_initialize_once()
         */
-       if (UAM_ERROR_NONE != __check_integrity()) {
-               UAM_ERR("Failed to check integrity");
-               unlink(DATABASE_FULL_PATH);
-               database = NULL;
-               sql_ret = sqlite3_open_v2(DATABASE_FULL_PATH, &database,
-                       SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
-               if (SQLITE_OK != sql_ret) {
-                       UAM_ERR("sqlite3_open_v2 failed, (%d)", sql_ret);
-                       database = NULL;
-                       return UAM_ERROR_DB_FAILED;
-               }
-       }
-
-       __ua_prepare_table();
+       /* check db integrity */
+       retv_if(UAM_ERROR_NONE != __uam_db_check_integrity(), UAM_ERROR_DB_FAILED);
 
        /*
-        * 3) Default journal mode is PERIST so it seems use-less.
+        * 11) why this is required? If it is not needed, please remove it
         */
+       /* set locking mode */
+       retv_if(UAM_ERROR_NONE != __uam_db_set_locking_mode(), UAM_ERROR_DB_FAILED);
+       return UAM_ERROR_NONE;
+}
 
-       /* Enable persist journal mode */
-       sql_ret = sqlite3_exec(database, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
-       if (SQLITE_OK != sql_ret) {
-               UAM_ERR("Fail to change journal mode: (%d) %s", sql_ret, error);
-               sqlite3_free(error);
+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;
-               return UAM_ERROR_DB_FAILED;
+
+               /* Reopen database */
+               retv_if(SQLITE_OK != __uam_db_open(), UAM_ERROR_DB_FAILED);
        }
 
+       __uam_db_prepare_table();
+
        /*
-        * 12) it seems that we don't need busy handler because we are single thread.
+        * 3) Default journal mode is PERIST so it seems use-less.
         */
+       /* Enable persist journal mode */
+       sql = sqlite3_mprintf("PRAGMA journal_mode = PERSIST");
+       retv_if(UAM_ERROR_NONE != __uam_db_exec_sql(sql, NULL), UAM_ERROR_DB_FAILED);
+       sqlite3_free(sql);
+
        /* Set how many times we'll repeat our attempts for sqlite_step */
-       if (SQLITE_OK != sqlite3_busy_handler(database, __ua_db_busy, NULL))
+       int max_attempts  = 10;
+       if (SQLITE_OK != sqlite3_busy_handler(database, __uam_db_busy, &max_attempts))
                UAM_ERR("Couldn't set busy handler!");
 
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+}
+
+int _uam_db_initialize(void)
+{
+       FUNC_ENTRY;
+       database = NULL;
+
+       EXEC(UAM_ERROR_NONE, _uam_db_initialize_once(), handle_error);
+
+       EXEC(UAM_ERROR_NONE, _uam_user_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _ua_device_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _ua_service_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _ua_device_service_db_initialize(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_adv_db_initialize(), handle_error);
+
+       FUNC_EXIT;
+       return UAM_ERROR_NONE;
+
+handle_error:
+       _uam_db_deinitialize();
+       FUNC_EXIT;
+       return UAM_ERROR_DB_FAILED;
+}
+
+int _uam_db_deinitialize(void)
+{
+       FUNC_ENTRY;
+
+       retv_if(NULL == database, UAM_ERROR_NONE);
+
+       _uam_user_db_deinitialize();
+       _ua_device_db_deinitialize();
+       _ua_service_db_deinitialize();
+       _ua_device_service_db_deinitialize();
+       _uam_adv_db_deinitialize();
+
+       FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
+
+int _uam_db_clear(void)
+{
+       int error_code = UAM_ERROR_DB_FAILED;
+
+       EXEC(UAM_ERROR_NONE, _uam_user_db_clear(), handle_error);
+       EXEC(UAM_ERROR_NONE, _uam_device_db_clear(), handle_error);
+       EXEC(UAM_ERROR_NONE, _ua_service_db_clear(), handle_error);
+       EXEC(UAM_ERROR_NONE, _ua_device_service_db_clear(), handle_error);
+       UAM_DBG("Table data deleted ");
+
+       error_code = UAM_ERROR_NONE;
+
+handle_error:
+       return error_code;
+}
index 1202e5f..47d3d8a 100644 (file)
@@ -258,7 +258,7 @@ static void __ua_device_table_devicesinfo_finalize(void)
 static sqlite3 *__ua_device_db_get_database(void)
 {
        if (database == NULL)
-               _ua_db_initialize_once();
+               _uam_db_initialize_once();
 
        return database;
 }
@@ -755,7 +755,7 @@ GSList *_ua_device_db_get_all_devices(void)
        return device_list;
 }
 
-int _ua_device_db_clear(void)
+int _uam_device_db_clear(void)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = delete_all_devices;
@@ -770,11 +770,9 @@ int _ua_device_db_clear(void)
                error_code = UAM_ERROR_DB_FAILED;
                goto handle_error;
        }
-
-       UAM_DBG("User data deleted ");
+       UAM_DBG("Device data deleted ");
 
 handle_error:
-
        sqlite3_reset(stmt);
        return error_code;
 }
index 0780ebe..64bbdc7 100644 (file)
@@ -233,7 +233,7 @@ handle_error:
 static sqlite3 *__ua_device_service_db_get_database(void)
 {
        if (database == NULL)
-               _ua_db_initialize_once();
+               _uam_db_initialize_once();
 
        return database;
 }
index a3b66d2..722744e 100644 (file)
@@ -214,7 +214,7 @@ static void __ua_service_table_servicesinfo_finalize(void)
 static sqlite3 *__ua_service_db_get_database(void)
 {
        if (database == NULL)
-               _ua_db_initialize_once();
+               _uam_db_initialize_once();
 
        return database;
 }
index 7771c9c..2d1f547 100644 (file)
@@ -195,7 +195,7 @@ static void __ua_table_usersinfo_finalize(void)
 static sqlite3 *__ua_db_get_database(void)
 {
        if (database == NULL)
-               _ua_db_initialize_once();
+               _uam_db_initialize_once();
 
        return database;
 }
@@ -229,28 +229,13 @@ int _uam_db_get_max_user_id(void)
        return error_code;
 }
 
-int _ua_db_initialize(void)
+int _uam_user_db_initialize(void)
 {
        FUNC_ENTRY;
        database = NULL;
 
-       if (UAM_ERROR_NONE != _ua_db_initialize_once())
-               goto handle_error;
-
        EXEC(UAM_ERROR_NONE, __ua_table_usersinfo_prepare(database), handle_error);
 
-       if (_ua_device_db_initialize() != UAM_ERROR_NONE)
-               goto handle_error;
-
-       if (_ua_service_db_initialize() != UAM_ERROR_NONE)
-               goto handle_error;
-
-       if (_ua_device_service_db_initialize() != UAM_ERROR_NONE)
-               goto handle_error;
-
-       if (_uam_adv_db_initialize() != UAM_ERROR_NONE)
-               goto handle_error;
-
        if (_uam_db_get_max_user_id() != UAM_ERROR_NONE)
                goto handle_error;
 
@@ -258,16 +243,12 @@ int _ua_db_initialize(void)
        return UAM_ERROR_NONE;
 
 handle_error:
-       _ua_db_deinitialize();
-       _ua_device_db_deinitialize();
-       _ua_service_db_deinitialize();
-       _ua_device_service_db_deinitialize();
-       _uam_adv_db_deinitialize();
+       _uam_user_db_deinitialize();
        FUNC_EXIT;
        return UAM_ERROR_DB_FAILED;
 }
 
-int _ua_db_deinitialize(void)
+int _uam_user_db_deinitialize(void)
 {
        FUNC_ENTRY;
 
@@ -276,11 +257,6 @@ int _ua_db_deinitialize(void)
        __ua_table_usersinfo_finalize();
        sqlite3_close(database);
 
-       _ua_device_db_deinitialize();
-       _ua_service_db_deinitialize();
-       _ua_device_service_db_deinitialize();
-       _uam_adv_db_deinitialize();
-
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 }
@@ -431,7 +407,7 @@ GSList *_ua_db_get_all_users(void)
        return user_list;
 }
 
-int _ua_db_clear(void)
+int _uam_user_db_clear(void)
 {
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = delete_all_users_data;
@@ -446,35 +422,9 @@ int _ua_db_clear(void)
                error_code = UAM_ERROR_DB_FAILED;
                goto handle_error;
        }
-
        UAM_DBG("User data deleted ");
 
-       if (_ua_device_db_clear() != UAM_ERROR_NONE) {
-               UAM_ERR("Failed to delete device data");
-               error_code = UAM_ERROR_DB_FAILED;
-               goto handle_error;
-       }
-
-       UAM_DBG("Device data deleted ");
-
-       if (_ua_service_db_clear() != UAM_ERROR_NONE) {
-               UAM_ERR("Failed to delete servicee data");
-               error_code = UAM_ERROR_DB_FAILED;
-               goto handle_error;
-       }
-
-       UAM_DBG("Service data deleted ");
-
-       if (_ua_device_service_db_clear() != UAM_ERROR_NONE) {
-               UAM_ERR("Failed to delete device-service data");
-               error_code = UAM_ERROR_DB_FAILED;
-               goto handle_error;
-       }
-
-       UAM_DBG("Device data deleted ");
-
 handle_error:
-
        sqlite3_reset(stmt);
        return error_code;
 }