From: InHong Han Date: Thu, 30 Jun 2022 04:51:07 +0000 (+0900) Subject: Reduce duplicate code in db_manager X-Git-Tag: submit/tizen/20220713.011249~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=95868f12996dbaee3f4f7984a0660dc91e1644ca;p=platform%2Fcore%2Fuifw%2Fcapi-ui-sticker.git Reduce duplicate code in db_manager Change-Id: I704e77358480b564ba88847e600fef06cdc05cf4 --- diff --git a/server/stickerd_db_manager.c b/server/stickerd_db_manager.c index dfa84c9..e299e6d 100644 --- a/server/stickerd_db_manager.c +++ b/server/stickerd_db_manager.c @@ -186,25 +186,10 @@ static const char *_db_get_query(sticker_info_db_type sticker_type, command_type return query; } -static int _recover_db(void) +static int _create_db_table(sqlite3 *db) { - int ret = STICKERD_SERVER_ERROR_NONE; - sqlite3 *db = NULL; char *err = NULL; - - LOGD("Start to recover sticker db"); - //Remove sticker database file - if (unlink(STICKER_DB_PATH) == -1) - LOGE("Failed to remove db file"); - - ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); - if (ret != SQLITE_OK) { - LOGE("Failed to open db : %s", sqlite3_errmsg(db)); - if (unlink(STICKER_DB_PATH) == -1) - LOGE("Failed to remove db file"); - ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; - } + int ret = STICKERD_SERVER_ERROR_NONE; ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err); if (ret != SQLITE_OK) { @@ -238,15 +223,41 @@ static int _recover_db(void) if (ret != SQLITE_OK) { LOGE("Failed to create sticker_group_info table : %s", err); ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; } - is_corrupted = FALSE; - cleanup: if (err) sqlite3_free(err); + return ret; +} + +static int _recover_db(void) +{ + int ret = STICKERD_SERVER_ERROR_NONE; + sqlite3 *db = NULL; + + LOGD("Start to recover sticker db"); + //Remove sticker database file + if (unlink(STICKER_DB_PATH) == -1) + LOGE("Failed to remove db file"); + + ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); + if (ret != SQLITE_OK) { + LOGE("Failed to open db : %s", sqlite3_errmsg(db)); + if (unlink(STICKER_DB_PATH) == -1) + LOGE("Failed to remove db file"); + ret = STICKERD_SERVER_ERROR_DB_FAILED; + goto cleanup; + } + + ret = _create_db_table(db); + if (ret != SQLITE_OK) + goto cleanup; + + is_corrupted = FALSE; + +cleanup: if (db) sqlite3_close(db); @@ -278,40 +289,9 @@ int stickerd_db_init(void) goto cleanup; } - ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err); - if (ret != SQLITE_OK) { - LOGE("Failed to create sticker_info table : %s" , err); - ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; - } - - ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err); - if (ret != SQLITE_OK) { - LOGE("Failed to create sticker_keyword_info table : %s", err); - ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; - } - - ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err); - if (ret != SQLITE_OK) { - LOGE("Failed to create sticker_whitelist_info table : %s", err); - ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; - } - - ret = sqlite3_exec(db, STICKER_RECENT_HISTORY_INFO_CREATE_TABLE, NULL, NULL, &err); - if (ret != SQLITE_OK) { - LOGE("Failed to create sticker_recent_history_info table : %s", err); - ret = STICKERD_SERVER_ERROR_DB_FAILED; - goto cleanup; - } - - ret = sqlite3_exec(db, STICKER_GROUP_INFO_CREATRE_TABLE, NULL, NULL, &err); - if (ret != SQLITE_OK) { - LOGE("Failed to create sticker_group_info table : %s", err); - ret = STICKERD_SERVER_ERROR_DB_FAILED; + ret = _create_db_table(db); + if (ret != SQLITE_OK) goto cleanup; - } ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err); if (ret != SQLITE_OK) {