Fix issue for cion initialization 39/285439/7
authorInkyun Kil <inkyun.kil@samsung.com>
Tue, 13 Dec 2022 01:21:15 +0000 (10:21 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 13 Dec 2022 03:04:22 +0000 (03:04 +0000)
- Fix issue for checking if table is exist

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/285443/

Change-Id: I02461a8caf3986699bebf327d8b20a359e3ed345
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
src/modules/cion/cion_module.cc

index ec945f0..86fa6fa 100644 (file)
@@ -35,8 +35,6 @@ namespace {
 constexpr const char CREATE_CION_TABLE[] = R"__cion(
 PRAGMA user_version = 50;
 PRAGMA journal_mode = PERSIST;
-PRAGMA foreign_keys = ON;
-BEGIN EXCLUSIVE TRANSACTION;
 CREATE TABLE IF NOT EXISTS cion_uuid (
   appid         TEXT NOT NULL,
   uuid          TEXT NOT NULL,
@@ -50,33 +48,18 @@ CREATE TABLE IF NOT EXISTS cion_display_name (
   PRIMARY KEY(service_name, appid) ,
   FOREIGN KEY(appid) REFERENCES cion_uuid (appid) ON DELETE CASCADE
 );
-COMMIT TRANSACTION;
 )__cion";
 
 bool CreateTable(const tizen_base::Database& db) {
-  auto q = tizen_base::Database::Sql(CREATE_CION_TABLE);
-  auto r = db.Exec(q);
-  if (!static_cast<bool>(r))
-    return false;
-
-  return true;
-}
-
-bool IsTableExist(const tizen_base::Database& db) {
-  auto q = tizen_base::Database::Sql(
-      "SELECT name FROM sqlite_master WHERE type='table'"
-      " ORDER BY name ASC");
-  auto r = db.Exec(q);
-  if (!static_cast<bool>(r))
+  try {
+    auto q = tizen_base::Database::Sql(CREATE_CION_TABLE);
+    db.OneStepExec(q);
+  } catch (const tizen_base::DbException& e) {
+    _E("Exception(%s) occurs", e.msg());
     return false;
-
-  for (const auto& i : r) {
-    auto table_name = static_cast<std::string>(i.Get(0));
-    if (table_name == "cion")
-      return true;
   }
 
-  return false;
+  return true;
 }
 
 } // namespace
@@ -185,24 +168,22 @@ void CionModule::RemoveEnabledApp(const std::string& service_name,
 bool CionModule::DbInit() {
   try {
     tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE);
-    if (IsTableExist(db)) {
-      if (!CreateTable(db)) {
-        _E("Fail to create table");
-        return false;
-      }
+    if (!CreateTable(db)) {
+      _E("Fail to create table");
+      return false;
     }
-  } catch(const std::runtime_error&) {
+  } catch (const tizen_base::DbException& e) {
+    _E("runtime error(%s) occurs", e.msg());
     unlink(DBPATH);
     try {
       tizen_base::Database db(DBPATH, SQLITE_OPEN_CREATE |
           SQLITE_OPEN_READWRITE);
-      if (IsTableExist(db)) {
-        if (!CreateTable(db)) {
-          _E("Fail to create table");
-          return false;
-        }
+      if (!CreateTable(db)) {
+        _E("Fail to create table");
+        return false;
       }
-    } catch(const std::runtime_error&) {
+    } catch (const tizen_base::DbException& e) {
+      _E("runtime error(%s) occurs", e.msg());
       unlink(DBPATH);
       return false;
     }