UI: SQLite: Refactor default flags.

This commit is contained in:
Nodir Temirkhodjaev 2021-03-26 19:09:07 +03:00
parent 994e5fe67c
commit 5f4236f672
5 changed files with 12 additions and 16 deletions

View File

@ -58,15 +58,8 @@ SqliteDb::~SqliteDb()
sqlite3_shutdown(); 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(); const auto filePathUtf8 = m_filePath.toUtf8();
return sqlite3_open_v2(filePathUtf8.data(), &m_db, m_openFlags, nullptr) == SQLITE_OK; 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"; 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; dbWarning() << "Cannot re-create the DB" << m_filePath;
renameDbFile(tempFilePath, m_filePath); renameDbFile(tempFilePath, m_filePath);
return false; return false;

View File

@ -17,8 +17,6 @@ class SqliteDb
{ {
public: public:
enum OpenFlag { enum OpenFlag {
OpenVoid = -1,
OpenDefault = 0,
OpenReadOnly = 0x00000001, // SQLITE_OPEN_READONLY OpenReadOnly = 0x00000001, // SQLITE_OPEN_READONLY
OpenReadWrite = 0x00000002, // SQLITE_OPEN_READWRITE OpenReadWrite = 0x00000002, // SQLITE_OPEN_READWRITE
OpenCreate = 0x00000004, // SQLITE_OPEN_CREATE OpenCreate = 0x00000004, // SQLITE_OPEN_CREATE
@ -29,6 +27,7 @@ public:
OpenSharedCache = 0x00020000, // SQLITE_OPEN_SHAREDCACHE OpenSharedCache = 0x00020000, // SQLITE_OPEN_SHAREDCACHE
OpenPrivateCache = 0x00040000, // SQLITE_OPEN_PRIVATECACHE OpenPrivateCache = 0x00040000, // SQLITE_OPEN_PRIVATECACHE
OpenNoFollow = 0x01000000, // SQLITE_OPEN_NOFOLLOW OpenNoFollow = 0x01000000, // SQLITE_OPEN_NOFOLLOW
OpenDefault = (OpenReadWrite | OpenCreate | OpenNoMutex)
}; };
explicit SqliteDb(const QString &filePath = QString(), quint32 openFlags = OpenDefault); explicit SqliteDb(const QString &filePath = QString(), quint32 openFlags = OpenDefault);
@ -38,9 +37,12 @@ public:
struct sqlite3 *db() const { return m_db; } struct sqlite3 *db() const { return m_db; }
quint32 openFlags() const { return m_openFlags; } 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(); void close();
bool attach(const QString &schemaName, const QString &filePath = QString()); bool attach(const QString &schemaName, const QString &filePath = QString());

View File

@ -307,7 +307,7 @@ bool saveAppGroup(SqliteDb *db, AppGroup *appGroup, int orderIndex)
ConfManager::ConfManager(const QString &filePath, FortManager *fortManager, QObject *parent) : ConfManager::ConfManager(const QString &filePath, FortManager *fortManager, QObject *parent) :
QObject(parent), QObject(parent),
m_fortManager(fortManager), m_fortManager(fortManager),
m_sqliteDb(new SqliteDb(filePath, SqliteDb::OpenNoMutex)), m_sqliteDb(new SqliteDb(filePath)),
m_conf(new FirewallConf(this)) m_conf(new FirewallConf(this))
{ {
m_appEndTimer.setInterval(5 * 60 * 1000); // 5 minutes m_appEndTimer.setInterval(5 * 60 * 1000); // 5 minutes

View File

@ -54,7 +54,7 @@ StatManager::StatManager(const QString &filePath, QuotaManager *quotaManager, QO
m_isActivePeriodSet(false), m_isActivePeriodSet(false),
m_isActivePeriod(false), m_isActivePeriod(false),
m_quotaManager(quotaManager), m_quotaManager(quotaManager),
m_sqliteDb(new SqliteDb(filePath, SqliteDb::OpenNoMutex)) m_sqliteDb(new SqliteDb(filePath))
{ {
} }

View File

@ -72,7 +72,8 @@ AppInfoManager::~AppInfoManager()
void AppInfoManager::setupDb(const QString &filePath) 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(); logCritical() << "File open error:" << filePath << m_sqliteDb->errorMessage();
return; return;
} }