From: sooyeon.kim Date: Fri, 26 Jul 2019 07:02:55 +0000 (+0900) Subject: Add a checker for vc_db_table X-Git-Tag: submit/tizen/20190726.072558~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F45%2F210945%2F1;p=platform%2Fcore%2Fuifw%2Fvoice-control.git Add a checker for vc_db_table Change-Id: I6d33f0e823f4147f39e7a7a7901d64d7c6ea0d11 Signed-off-by: sooyeon.kim --- diff --git a/common/vc_cmd_db.c b/common/vc_cmd_db.c index ade3f93..29e21c1 100644 --- a/common/vc_cmd_db.c +++ b/common/vc_cmd_db.c @@ -65,11 +65,19 @@ int g_ref_cnt = 0; 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; @@ -116,6 +124,47 @@ static int __vc_db_exec_query(sqlite3* db_handle, const char* sql) 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; @@ -134,6 +183,19 @@ static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e ty } 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)); @@ -202,6 +264,20 @@ static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e ty 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)); @@ -314,6 +390,21 @@ static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, 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)); @@ -393,6 +484,9 @@ static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, 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; @@ -404,6 +498,11 @@ static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, } } + if (NULL != temp_text) { + free(temp_text); + temp_text = NULL; + } + temp = sqlite3_column_int(stmt, 0); vc_cmd_set_id(temp_cmd, temp); @@ -503,6 +602,21 @@ static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, in 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)); @@ -724,6 +838,20 @@ static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event 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)); @@ -864,9 +992,10 @@ static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event __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); } @@ -936,6 +1065,20 @@ static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** ap 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)); @@ -1009,6 +1152,20 @@ int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** 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)); @@ -1077,6 +1234,20 @@ static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd 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)); @@ -1228,26 +1399,27 @@ static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd) 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; } @@ -1294,9 +1466,16 @@ int __vc_db_open_db(char** path, sqlite3** db_handle) 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; } @@ -1472,9 +1651,15 @@ int vc_db_create_table() { __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; }