[Service/CodeClean] separate method to init database
authorJaeyun Jung <jy1210.jung@samsung.com>
Fri, 17 Feb 2023 07:52:31 +0000 (16:52 +0900)
committerSangjung Woo <again4you@gmail.com>
Wed, 22 Feb 2023 02:18:28 +0000 (11:18 +0900)
Code clean, sepatate method to initialize ml-service database.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
daemon/includes/service-db.hh
daemon/service-db.cc

index c84b480..1680591 100644 (file)
@@ -32,6 +32,7 @@ public:
   virtual void set_pipeline (const std::string name, const std::string description);
   virtual void get_pipeline (const std::string name, std::string &description);
   virtual void delete_pipeline (const std::string name);
+  /** @todo update methods to handle nn model */
   virtual void set_model (const std::string name, const std::string model);
   virtual void get_model (const std::string name, std::string &model);
   virtual void delete_model (const std::string name);
@@ -42,6 +43,8 @@ private:
   MLServiceDB (std::string path);
   virtual ~MLServiceDB ();
 
+  void initDB ();
+
   std::string _path;
   bool _initialized;
   sqlite3 *_db;
index 9f69b9a..208f606 100644 (file)
@@ -69,15 +69,77 @@ MLServiceDB::~MLServiceDB ()
 }
 
 /**
- * @brief Connect to ML Service DB and initialize the private variables.
+ * @brief Create table and handle database version.
  */
 void
-MLServiceDB::connectDB ()
+MLServiceDB::initDB ()
 {
   int rc;
   char *sql;
   char *errmsg = nullptr;
 
+  if (_initialized)
+    return;
+
+  /**
+   * @todo handle database version
+   * - check old level db and insert pipeline description into sqlite.
+   * - check database info (TBL_DB_INFO), fetch data and update table schema when version is updated, ...
+   * - need transaction
+   */
+  rc = sqlite3_exec (_db, "BEGIN TRANSACTION;", nullptr, nullptr, &errmsg);
+  if (rc != SQLITE_OK) {
+    g_warning ("Failed to begin transaction: %s (%d)", errmsg, rc);
+    sqlite3_clear_errmsg (errmsg);
+    return;
+  }
+
+  /* Create tables. */
+  sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_DB_INFO]);
+  rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
+  g_free (sql);
+  if (rc != SQLITE_OK) {
+    g_warning ("Failed to create table for database info: %s (%d)", errmsg, rc);
+    sqlite3_clear_errmsg (errmsg);
+    return;
+  }
+
+  sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_PIPELINE_DESCRIPTION]);
+  rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
+  g_free (sql);
+  if (rc != SQLITE_OK) {
+    g_warning ("Failed to create table for pipeline description: %s (%d)", errmsg, rc);
+    sqlite3_clear_errmsg (errmsg);
+    return;
+  }
+
+  sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_MODEL_INFO]);
+  rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
+  g_free (sql);
+  if (rc != SQLITE_OK) {
+    g_warning ("Failed to create table for model info: %s (%d)", errmsg, rc);
+    sqlite3_clear_errmsg (errmsg);
+    return;
+  }
+
+  rc = sqlite3_exec (_db, "END TRANSACTION;", nullptr, nullptr, &errmsg);
+  if (rc != SQLITE_OK) {
+    g_warning ("Failed to end transaction: %s (%d)", errmsg, rc);
+    sqlite3_clear_errmsg (errmsg);
+    return;
+  }
+
+  _initialized = true;
+}
+
+/**
+ * @brief Connect to ML Service DB and initialize the private variables.
+ */
+void
+MLServiceDB::connectDB ()
+{
+  int rc;
+
   if (_db != nullptr)
     return;
 
@@ -87,59 +149,7 @@ MLServiceDB::connectDB ()
     goto error;
   }
 
-  /* Create table and handle database version. */
-  if (!_initialized) {
-    /**
-     * @todo handle database version
-     * - check old level db and insert pipeline description into sqlite.
-     * - check database info (TBL_DB_INFO), fetch data and update table schema when version is updated, ...
-     * - need transaction
-     */
-
-    rc = sqlite3_exec (_db, "BEGIN TRANSACTION;", nullptr, nullptr, &errmsg);
-    if (rc != SQLITE_OK) {
-      g_warning ("Failed to begin transaction: %s (%d)", errmsg, rc);
-      sqlite3_clear_errmsg (errmsg);
-      goto error;
-    }
-
-    /* Create tables. */
-    sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_DB_INFO]);
-    rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
-    g_free (sql);
-    if (rc != SQLITE_OK) {
-      g_warning ("Failed to create table for database info: %s (%d)", errmsg, rc);
-      sqlite3_clear_errmsg (errmsg);
-      goto error;
-    }
-
-    sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_PIPELINE_DESCRIPTION]);
-    rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
-    g_free (sql);
-    if (rc != SQLITE_OK) {
-      g_warning ("Failed to create table for pipeline description: %s (%d)", errmsg, rc);
-      sqlite3_clear_errmsg (errmsg);
-      goto error;
-    }
-
-    sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS %s;", g_mlsvc_table_schema[TBL_MODEL_INFO]);
-    rc = sqlite3_exec (_db, sql, nullptr, nullptr, &errmsg);
-    g_free (sql);
-    if (rc != SQLITE_OK) {
-      g_warning ("Failed to create table for model info: %s (%d)", errmsg, rc);
-      sqlite3_clear_errmsg (errmsg);
-      goto error;
-    }
-
-    rc = sqlite3_exec (_db, "END TRANSACTION;", nullptr, nullptr, &errmsg);
-    if (rc != SQLITE_OK) {
-      g_warning ("Failed to end transaction: %s (%d)", errmsg, rc);
-      sqlite3_clear_errmsg (errmsg);
-      goto error;
-    }
-
-    _initialized = true;
-  }
+  initDB ();
 
 error:
   if (!_initialized) {