From 5f4236f6721c60372fcdb3bea68414982d3714da Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 26 Mar 2021 19:09:07 +0300 Subject: [PATCH] UI: SQLite: Refactor default flags. --- src/ui/3rdparty/sqlite/sqlitedb.cpp | 11 ++--------- src/ui/3rdparty/sqlite/sqlitedb.h | 10 ++++++---- src/ui/conf/confmanager.cpp | 2 +- src/ui/stat/statmanager.cpp | 2 +- src/ui/util/app/appinfomanager.cpp | 3 ++- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index f4877205..5d687ac8 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -58,15 +58,8 @@ SqliteDb::~SqliteDb() sqlite3_shutdown(); } -bool SqliteDb::open(const QString &filePath, quint32 openFlags) +bool SqliteDb::open() { - if (!filePath.isEmpty()) { - m_filePath = filePath; - } - if (openFlags != OpenVoid) { - m_openFlags = openFlags; - } - const auto filePathUtf8 = m_filePath.toUtf8(); return sqlite3_open_v2(filePathUtf8.data(), &m_db, m_openFlags, nullptr) == SQLITE_OK; @@ -314,7 +307,7 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio tempFilePath = m_filePath + ".temp"; - if (!(renameDbFile(m_filePath, tempFilePath) && open(m_filePath))) { + if (!(renameDbFile(m_filePath, tempFilePath) && open())) { dbWarning() << "Cannot re-create the DB" << m_filePath; renameDbFile(tempFilePath, m_filePath); return false; diff --git a/src/ui/3rdparty/sqlite/sqlitedb.h b/src/ui/3rdparty/sqlite/sqlitedb.h index fec4b9dd..8cdc0bc6 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.h +++ b/src/ui/3rdparty/sqlite/sqlitedb.h @@ -17,8 +17,6 @@ class SqliteDb { public: enum OpenFlag { - OpenVoid = -1, - OpenDefault = 0, OpenReadOnly = 0x00000001, // SQLITE_OPEN_READONLY OpenReadWrite = 0x00000002, // SQLITE_OPEN_READWRITE OpenCreate = 0x00000004, // SQLITE_OPEN_CREATE @@ -29,6 +27,7 @@ public: OpenSharedCache = 0x00020000, // SQLITE_OPEN_SHAREDCACHE OpenPrivateCache = 0x00040000, // SQLITE_OPEN_PRIVATECACHE OpenNoFollow = 0x01000000, // SQLITE_OPEN_NOFOLLOW + OpenDefault = (OpenReadWrite | OpenCreate | OpenNoMutex) }; explicit SqliteDb(const QString &filePath = QString(), quint32 openFlags = OpenDefault); @@ -38,9 +37,12 @@ public: struct sqlite3 *db() const { return m_db; } quint32 openFlags() const { return m_openFlags; } - QString filePath() const { return m_filePath; } + void setOpenFlags(quint32 v) { m_openFlags = v; } - bool open(const QString &filePath = QString(), quint32 openFlags = OpenVoid); + QString filePath() const { return m_filePath; } + void setFilePath(const QString &v) { m_filePath = v; } + + bool open(); void close(); bool attach(const QString &schemaName, const QString &filePath = QString()); diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index d48543b7..40b74753 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -307,7 +307,7 @@ bool saveAppGroup(SqliteDb *db, AppGroup *appGroup, int orderIndex) ConfManager::ConfManager(const QString &filePath, FortManager *fortManager, QObject *parent) : QObject(parent), m_fortManager(fortManager), - m_sqliteDb(new SqliteDb(filePath, SqliteDb::OpenNoMutex)), + m_sqliteDb(new SqliteDb(filePath)), m_conf(new FirewallConf(this)) { m_appEndTimer.setInterval(5 * 60 * 1000); // 5 minutes diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 42d801e8..e05ca8b6 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -54,7 +54,7 @@ StatManager::StatManager(const QString &filePath, QuotaManager *quotaManager, QO m_isActivePeriodSet(false), m_isActivePeriod(false), m_quotaManager(quotaManager), - m_sqliteDb(new SqliteDb(filePath, SqliteDb::OpenNoMutex)) + m_sqliteDb(new SqliteDb(filePath)) { } diff --git a/src/ui/util/app/appinfomanager.cpp b/src/ui/util/app/appinfomanager.cpp index bc270a2d..e50f78cd 100644 --- a/src/ui/util/app/appinfomanager.cpp +++ b/src/ui/util/app/appinfomanager.cpp @@ -72,7 +72,8 @@ AppInfoManager::~AppInfoManager() void AppInfoManager::setupDb(const QString &filePath) { - if (!m_sqliteDb->open(filePath, SqliteDb::OpenNoMutex)) { + m_sqliteDb->setFilePath(filePath); + if (!m_sqliteDb->open()) { logCritical() << "File open error:" << filePath << m_sqliteDb->errorMessage(); return; }