From e4104d14be79ea0eb65851d5ab2570216c7d49b6 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 13 Oct 2021 16:32:08 +0900 Subject: Fix Database ctor class 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 --- server/api/aul_service.cc | 2 +- server/database.cc | 16 ++++++++++------ server/database.hh | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/server/api/aul_service.cc b/server/api/aul_service.cc index 6c240dd8..60134fd5 100644 --- a/server/api/aul_service.cc +++ b/server/api/aul_service.cc @@ -77,7 +77,7 @@ template int Step(T cb, std::tuple 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) { diff --git a/server/database.cc b/server/database.cc index 63104c3b..789095f2 100644 --- a/server/database.cc +++ b/server/database.cc @@ -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; diff --git a/server/database.hh b/server/database.hh index d44f2f0f..06c4e992 100644 --- a/server/database.hh +++ b/server/database.hh @@ -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; }; -- cgit v1.2.3