#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) { \
#define UAM_DB_DEVICE_SERVICES_TABLE "device_services" /**< Device services DB table name */
#define UAM_DB_IBEACON_ADV_TABLE "ibeacon_adv" /**< iBeacon adv DB table name */
+/*
+ * 13) Need to version table for changing DB schema.
+ * For instance, In case of frequent OS upgrade, VD should make a dicision
+ * Whether it is needed to DB migration.
+ */
+
#define CREATE_USERDATA_TABLE "CREATE TABLE IF NOT EXISTS userdata ( " \
"name TEXT, " \
"user_id INTEGER, " \
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;
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;
+ return 0; /* 8) In order to enhance readabiity we can explicitly 0 means SUCCESS */
}
- return 1;
+ return 1; /* 9) In order to enhance readabiity we can explicitly 0 means DATA_ABORT */
}
static int __uam_db_check_table_cb(
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",
}
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");
}
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",
}
UAM_DBG("Successfully verified database integrity");
+ /* REDUNDANT Start */
sql = sqlite3_mprintf(
- "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
+ "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);
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);
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);
}
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_DB_FAILED;
}
UAM_DBG("Successfully verified %s table creation", UAM_DB_IBEACON_ADV_TABLE);
+ /* REDUNDANT End */
+ /* REDUNDANT Start */
sql = sqlite3_mprintf(
"SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
UAM_DB_DEVICE_SERVICES_TABLE);
return UAM_ERROR_DB_FAILED;
}
UAM_DBG("Successfully verified %s table creation", UAM_DB_DEVICE_SERVICES_TABLE);
+ /* REDUNDANT End */
+ /*
+ * 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.",
static void __ua_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);
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) {
return 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);
__ua_prepare_table();
+ /*
+ * 3) Default journal mode is PERIST so it seems use-less.
+ */
+
/* Enable persist journal mode */
sql_ret = sqlite3_exec(database, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
if (SQLITE_OK != sql_ret) {
return UAM_ERROR_DB_FAILED;
}
+ /*
+ * 12) it seems that we don't need busy handler because we are single thread.
+ */
/* Set how many times we'll repeat our attempts for sqlite_step */
if (SQLITE_OK != sqlite3_busy_handler(database, __ua_db_busy, NULL))
UAM_ERR("Couldn't set busy handler!");
FUNC_EXIT;
}
+/*
+ * 5) it just check only dtabase is null but all error-case
+ * we just call the function for recovery.
+ */
static sqlite3 *__ua_device_db_get_database(void)
{
if (database == NULL)
case SQLITE_ERROR:
default:
UAM_ERR("Failed to enumerate device info: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
}
} while (rc == SQLITE_ROW);
DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to update ip address: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
goto handle_error;
DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to update presence: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
goto handle_error;
DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to update device timestamp: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
goto handle_error;
DB_ACTION(sqlite3_bind_text(stmt, 10, address, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to update device discriminant: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
goto handle_error;
DB_ACTION(sqlite3_bind_int(stmt, 14, max_device_number + 1),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to insert device info: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
goto handle_error;
}
DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete device info %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
} else
UAM_DBG("User info deleted");
DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to select device info %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
error_code = UAM_ERROR_DB_FAILED;
} else {
UAM_DBG("Device info found");
case SQLITE_ERROR:
default:
UAM_ERR("Failed to enumerate device info: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
}
} while (rc == SQLITE_ROW);
case SQLITE_ERROR:
default:
UAM_ERR("Failed to enumerate device info: %s",
- sqlite3_errmsg(__ua_device_db_get_database()));
+ sqlite3_errmsg(__ua_device_db_get_database())); /* 5) it does not recover the DB because database is not null */
}
} while (rc == SQLITE_ROW);
int error_code = UAM_ERROR_NONE;
sqlite3_stmt *stmt = delete_all_devices;
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete device data %s",
sqlite3_errmsg(__ua_device_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 1, device_number), error_code, handle_error);
DB_ACTION(sqlite3_bind_int(stmt, 2, service_number), error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to insert device-service info: %s",
sqlite3_errmsg(__ua_device_service_db_get_database()));
int error_code = UAM_ERROR_NONE;
sqlite3_stmt *stmt = delete_all_device_services;
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete device data %s",
sqlite3_errmsg(__ua_device_service_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 1, service_number), error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete device-service info %s",
sqlite3_errmsg(__ua_device_service_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 1, device_number), error_code, handle_error);
DB_ACTION(sqlite3_bind_int(stmt, 2, service_number), error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete device-service info %s",
sqlite3_errmsg(__ua_device_service_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 3, cycle),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to insert service info: %s",
sqlite3_errmsg(__ua_service_db_get_database()));
DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete service info %s",
sqlite3_errmsg(__ua_service_db_get_database()));
int error_code = UAM_ERROR_NONE;
sqlite3_stmt *stmt = delete_all_services;
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete service data %s",
sqlite3_errmsg(__ua_service_db_get_database()));
DB_ACTION(sqlite3_bind_text(stmt, 2, service_name, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to update service timestamp: %s",
sqlite3_errmsg(__ua_service_db_get_database()));
DB_ACTION(sqlite3_bind_text(stmt, 3, account, -1, SQLITE_TRANSIENT),
error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to insert user info: %s",
sqlite3_errmsg(__ua_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 1, user_id), error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete user info %s",
sqlite3_errmsg(__ua_db_get_database()));
DB_ACTION(sqlite3_bind_int(stmt, 1, user_id), error_code, handle_error);
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to select user info %s",
sqlite3_errmsg(__ua_db_get_database()));
int error_code = UAM_ERROR_NONE;
sqlite3_stmt *stmt = delete_all_users_data;
+ /* 6) SQLITE_DONE or SQITE_ROW can be returned but is not error
+ * SQLITE_ROW can be returned multiple times, so we consider SQLITE_ROW case as well
+ * Lastly, we just regard SQLITE_ERROR is actual error.
+ */
if (sqlite3_step(stmt) != SQLITE_DONE) {
UAM_ERR("Failed to delete user data %s",
sqlite3_errmsg(__ua_db_get_database()));