Fix Database ctor class 48/265248/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Oct 2021 07:32:08 +0000 (16:32 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Oct 2021 07:32:08 +0000 (16:32 +0900)
The enable_foreign_keys flag is added to deactivate foreign keys in sqlite3.
The appsvc.db doesn't use foreign keys.

Change-Id: I5f810e320592181674402440d2262a9b79e15969
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
server/api/aul_service.cc
server/database.cc
server/database.hh

index 6c240dd..60134fd 100644 (file)
@@ -77,7 +77,7 @@ template <typename T, typename ...ARGS>
 int Step(T cb, std::tuple<ARGS...> args,
     bool readonly, const std::string& query, uid_t uid) {
   std::string path = Database::GetPath(APPSVC_DB, uid);
-  Database db(path);
+  Database db(path, false);
   try {
     db.Open(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
   } catch (Exception& e) {
index 63104c3..789095f 100644 (file)
@@ -31,7 +31,9 @@ const int BUSY_WAITING_MAX = 40;
 
 }  // namespace
 
-Database::Database(std::string path) : path_(std::move(path)) {
+Database::Database(std::string path, bool enable_foreign_keys)
+    : path_(std::move(path)),
+      enable_foreign_keys_(enable_foreign_keys) {
 }
 
 Database::~Database() {
@@ -73,11 +75,13 @@ void Database::Open(int flags) {
     THROW(-ret);
   }
 
-  ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);
-  if (ret != SQLITE_OK) {
-    _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db), ret);
-    sqlite3_close_v2(db);
-    THROW(-ret);
+  if (enable_foreign_keys_) {
+    ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);
+    if (ret != SQLITE_OK) {
+      _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db), ret);
+      sqlite3_close_v2(db);
+      THROW(-ret);
+    }
   }
 
   db_ = db;
index d44f2f0..06c4e99 100644 (file)
@@ -68,7 +68,7 @@ namespace aul {
 
 class Database {
  public:
-  Database(std::string path);
+  Database(std::string path, bool enable_foreign_keys = true);
   virtual ~Database();
 
   void Open(int flags = SQLITE_OPEN_READWRITE);
@@ -87,6 +87,7 @@ class Database {
 
  private:
   std::string path_;
+  bool enable_foreign_keys_;
   sqlite3* db_ = nullptr;
 };