Reduce duplicate code in db_manager 62/277062/1
authorInHong Han <inhong1.han@samsung.com>
Thu, 30 Jun 2022 04:51:07 +0000 (13:51 +0900)
committerInHong Han <inhong1.han@samsung.com>
Thu, 30 Jun 2022 05:37:56 +0000 (14:37 +0900)
Change-Id: I704e77358480b564ba88847e600fef06cdc05cf4

server/stickerd_db_manager.c

index dfa84c9f2f48eefd372207dcff2bbefedcfec38e..e299e6d92494937c293f493a77d13e23872cf460 100644 (file)
@@ -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) {