int g_db_cnt = 0;
int g_backup_db_cnt = 0;
+static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
+static int __vc_db_begin_transaction(sqlite3* db_handle);
+static int __vc_db_create_table(sqlite3* db_handle, const char* table);
+static int __vc_db_rollback_transaction(sqlite3* db_handle);
+static int __vc_db_commit_transaction(sqlite3* db_handle);
+
static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
{
sqlite3_stmt* pStmt = NULL;
+ int ret = -1;
- int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
+ /* prepare db */
+ ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
return VC_DB_ERROR_OPERATION_FAILED;
return VC_DB_ERROR_NONE;
}
+static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist)
+{
+ char* sql = NULL;
+ *is_exist = false;
+ if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
+ sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_result';");
+ } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
+ sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_info';");
+ } else {
+ return VC_DB_ERROR_INVALID_PARAMETER;
+ }
+
+ if (NULL == sql) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+ return VC_DB_ERROR_OUT_OF_MEMORY;
+ }
+
+ SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
+
+ char* err_msg = NULL;
+ char** results = NULL;
+ int ret = sqlite3_get_table(db_handle, sql, &results, NULL, NULL, &err_msg);
+ if (ret != SQLITE_OK) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_get_table return fail, ret(%d), err(%s)", ret, err_msg);
+ sqlite3_free(err_msg);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+
+ if (0 != atoi(results[1])) {
+ *is_exist = true;
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] table exists (%d)", atoi(results[1]));
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] table doesn't exist (%d)", atoi(results[1]));
+ }
+
+ sqlite3_free_table(results);
+ free(sql);
+ sql = NULL;
+ return VC_DB_ERROR_NONE;
+}
+
static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
{
sqlite3_stmt* stmt = NULL;
}
int ret = 0;
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+ }
+
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
sqlite3_stmt* stmt = NULL;
const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
int ret = 0;
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
return VC_DB_ERROR_OUT_OF_MEMORY;
}
+
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
+ free(temp_text);
+ temp_text = NULL;
+
vc_cmd_destroy(temp_cmd);
temp_cmd = NULL;
}
}
+ if (NULL != temp_text) {
+ free(temp_text);
+ temp_text = NULL;
+ }
+
temp = sqlite3_column_int(stmt, 0);
vc_cmd_set_id(temp_cmd, temp);
sqlite3_stmt* stmt = NULL;
const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
int ret = 0;
+
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
return VC_DB_ERROR_OUT_OF_MEMORY;
}
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
__vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
vc_cmd_set_command(temp_cmd, temp_fixed);
vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
- if (temp_unfixed)
+ if (NULL != temp_unfixed) {
free(temp_unfixed);
temp_unfixed = NULL;
+ }
} else {
vc_cmd_set_command(temp_cmd, temp_command);
}
sqlite3_stmt* stmt = NULL;
const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
sqlite3_stmt* stmt = NULL;
const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
+ /* check whether there is a table or not */
+ bool is_exist = false;
+ ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+ if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+ SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+ if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ } else {
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+ }
+
+ /* prepare db */
ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
if (ret != SQLITE_OK) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
return temp_cmd;
}
-int __vc_db_create_table(sqlite3* db_handle)
+static int __vc_db_create_table(sqlite3* db_handle, const char* table)
{
- const char* vc_info_sql = "CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
- command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
- const char* vc_result_sql = "CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
- pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
+ SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table);
+ char* sql = NULL;
- int ret = __vc_db_exec_query(db_handle, vc_info_sql);
- if (ret != VC_DB_ERROR_NONE) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
- return VC_DB_ERROR_OPERATION_FAILED;
+ if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
+ sql = strdup("CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
+ pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
+ } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
+ sql = strdup("CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
+ command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
+ } else {
+ return VC_DB_ERROR_INVALID_PARAMETER;
}
- SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
- ret = __vc_db_exec_query(db_handle, vc_result_sql);
+ int ret = __vc_db_exec_query(db_handle, sql);
if (ret != VC_DB_ERROR_NONE) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
- return ret;
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
+ return VC_DB_ERROR_OPERATION_FAILED;
}
- SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
+ SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
return VC_DB_ERROR_NONE;
}
if (!stat.st_size) {
__vc_db_begin_transaction(*db_handle);
- int ret = __vc_db_create_table(*db_handle);
+ int ret = -1;
+ ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
+ if (ret != VC_DB_ERROR_NONE) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ __vc_db_rollback_transaction(*db_handle);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
if (ret != VC_DB_ERROR_NONE) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
__vc_db_rollback_transaction(*db_handle);
return VC_DB_ERROR_OPERATION_FAILED;
}
{
__vc_db_begin_transaction(g_db_handle);
- int ret = __vc_db_create_table(g_db_handle);
+ int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
+ if (ret != VC_DB_ERROR_NONE) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+ __vc_db_rollback_transaction(g_db_handle);
+ return VC_DB_ERROR_OPERATION_FAILED;
+ }
+ ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
if (ret != VC_DB_ERROR_NONE) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
__vc_db_rollback_transaction(g_db_handle);
return VC_DB_ERROR_OPERATION_FAILED;
}