Enable foreign_keys 51/285451/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 13 Dec 2022 05:33:03 +0000 (05:33 +0000)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 13 Dec 2022 05:35:45 +0000 (05:35 +0000)
This patch is for foreign_keys setting for cion db.

Change-Id: Id3e101f78ed12330eb5eb0fa01f756e6376a3530
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/modules/cion/cion_module.cc

index 86fa6fa..758ea7f 100644 (file)
@@ -35,6 +35,7 @@ namespace {
 constexpr const char CREATE_CION_TABLE[] = R"__cion(
 PRAGMA user_version = 50;
 PRAGMA journal_mode = PERSIST;
+PRAGMA foreign_keys = ON;
 CREATE TABLE IF NOT EXISTS cion_uuid (
   appid         TEXT NOT NULL,
   uuid          TEXT NOT NULL,
@@ -50,6 +51,8 @@ CREATE TABLE IF NOT EXISTS cion_display_name (
 );
 )__cion";
 
+constexpr const char PRAGMA_FOREIGN_KEYS[] = "PRAGMA foreign_keys;";
+
 bool CreateTable(const tizen_base::Database& db) {
   try {
     auto q = tizen_base::Database::Sql(CREATE_CION_TABLE);
@@ -62,6 +65,19 @@ bool CreateTable(const tizen_base::Database& db) {
   return true;
 }
 
+bool IsEnabledForeignKeys(const tizen_base::Database& db) {
+  try {
+    auto sql = tizen_base::Database::Sql(PRAGMA_FOREIGN_KEYS);
+    auto result = db.Exec(sql);
+    for (const auto& i : result)
+      return static_cast<int>(i.Get(0)) ? true : false;
+  } catch (const tizen_base::DbException& e) {
+    _E("Exception(%s) occurs", e.msg());
+  }
+
+  return false;
+}
+
 } // namespace
 
 bool CionModule::Startup(api::ToolBox* tools) {
@@ -172,6 +188,8 @@ bool CionModule::DbInit() {
       _E("Fail to create table");
       return false;
     }
+
+    _W("foreign_keys: %s", IsEnabledForeignKeys(db) ? "ON" : "OFF");
   } catch (const tizen_base::DbException& e) {
     _E("runtime error(%s) occurs", e.msg());
     unlink(DBPATH);
@@ -182,6 +200,8 @@ bool CionModule::DbInit() {
         _E("Fail to create table");
         return false;
       }
+
+      _W("foreign_keys: %s", IsEnabledForeignKeys(db) ? "ON" : "OFF");
     } catch (const tizen_base::DbException& e) {
       _E("runtime error(%s) occurs", e.msg());
       unlink(DBPATH);