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;
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
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;
+}