From: Enar Vaikene Date: Thu, 9 May 2019 12:53:48 +0000 (+0300) Subject: PswGen: X-Git-Url: https://vaikene.ee/gitweb/overview.html?a=commitdiff_plain;h=af2feaab777210b8fbd9190904df3dd187acf0cd;p=evaf PswGen: * As mentioned in Qt documentation, do not keep QSqlDatabase instances. --- diff --git a/src/apps/PswGen/Storage/module.cpp b/src/apps/PswGen/Storage/module.cpp index 09efe2e..fc75dbb 100644 --- a/src/apps/PswGen/Storage/module.cpp +++ b/src/apps/PswGen/Storage/module.cpp @@ -26,6 +26,8 @@ #include #include +#include + VER_EXPORT_VERSION_INFO() @@ -101,27 +103,29 @@ StorageImpl::~StorageImpl() bool StorageImpl::init() { // Open the database + QSqlDatabase db; if (!QSqlDatabase::contains(DbConnectionName)) { // No database connection yet - mDb = QSqlDatabase::addDatabase("QSQLITE", DbConnectionName); - mDb.setDatabaseName(Common::iApp::instance()->dataRootDir() + DbName); - if (!mDb.open()) { - QSqlError err = mDb.lastError(); + db = QSqlDatabase::addDatabase("QSQLITE", DbConnectionName); + EVAF_INFO("Added database %s", DbConnectionName); + db.setDatabaseName(Common::iApp::instance()->dataRootDir() + DbName); + if (!db.open()) { + QSqlError err = db.lastError(); EVAF_ERROR("Failed to open database : %s", qPrintable(err.text())); return false; } } else { // Database connection already exists - mDb = QSqlDatabase::database(DbConnectionName); + db = QSqlDatabase::database(DbConnectionName); } // Create tables if necessary - if (!createTables()) + if (!createTables(db)) return false; // Load data - if (!loadData()) + if (!loadData(db)) return false; /// Register our interface @@ -135,7 +139,11 @@ bool StorageImpl::init() void StorageImpl::done() { mData.clear(); - mDb.close(); + if (QSqlDatabase::contains(DbConnectionName)) + { + QSqlDatabase::removeDatabase(DbConnectionName); + EVAF_INFO("Removed database %s", DbConnectionName); + } EVAF_INFO("%s finalized", qPrintable(objectName())); } @@ -144,14 +152,21 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointermodified()) { - QSqlQuery q(mDb); + QSqlQuery q(db); if (!q.exec(QString("UPDATE data SET suffix = \'%1\', length = \'%2\', flags = \'%3\' WHERE name = \'%4\';") .arg(data->suffix()).arg(data->length()).arg(data->flags()).arg(name))) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to update \'%s\' : %s", qPrintable(name), qPrintable(err.text())); return false; } @@ -159,11 +174,11 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointersuffix()).arg(data->length()) .arg(int(data->flags())))) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to insert \'%s\' : %s", qPrintable(name), qPrintable(err.text())); return false; } @@ -200,23 +215,23 @@ QVariant StorageImpl::data(QModelIndex const & index, int role) const return QVariant(); } -bool StorageImpl::createTables() +bool StorageImpl::createTables(QSqlDatabase & db) { - QSqlQuery q(mDb); + QSqlQuery q(db); if (!q.exec("SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'data\';")) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to query database : %s", qPrintable(err.text())); return false; } if (q.isActive() && q.isSelect() && q.first()) { // Check if the table needs to be upgraded - return upgradeTables(); + return upgradeTables(db); } // Create the 'data' table if (!q.exec("CREATE TABLE data (name text primary key not null, suffix text, length integer, flags integer);")) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to create table \'data\' : %s", qPrintable(err.text())); return false; } @@ -224,9 +239,9 @@ bool StorageImpl::createTables() return true; } -bool StorageImpl::upgradeTables() +bool StorageImpl::upgradeTables(QSqlDatabase & db) { - QSqlQuery q(mDb); + QSqlQuery q(db); // Check if the 'suffix' column exists if (q.exec("SELECT suffix from data;")) { @@ -235,7 +250,7 @@ bool StorageImpl::upgradeTables() // Add the 'suffix' columnt if (!q.exec("ALTER TABLE data ADD COLUMN suffix TEXT;")) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to upgrade table \'data\' : %s", qPrintable(err.text())); return false; } @@ -243,11 +258,11 @@ bool StorageImpl::upgradeTables() return true; } -bool StorageImpl::loadData() +bool StorageImpl::loadData(QSqlDatabase & db) { - QSqlQuery q(mDb); + QSqlQuery q(db); if (!q.exec("SELECT name, suffix, length, flags FROM data;")) { - QSqlError err = mDb.lastError(); + QSqlError err = db.lastError(); EVAF_ERROR("Failed to query database : %s", qPrintable(err.text())); return false; } diff --git a/src/apps/PswGen/Storage/module.h b/src/apps/PswGen/Storage/module.h index 49c7e6a..e0839b3 100644 --- a/src/apps/PswGen/Storage/module.h +++ b/src/apps/PswGen/Storage/module.h @@ -29,8 +29,8 @@ #include #include #include -#include +class QSqlDatabase; namespace eVaf { namespace PswGen { @@ -126,9 +126,6 @@ private: // Members /// Name of the database file without path static char const * const DbName; - /// Database connection - QSqlDatabase mDb; - /// List of name/data pairs QMap > mData; @@ -139,7 +136,7 @@ private: // Methods * Creates database tables if necessary * @return True if ok; false if failed */ - bool createTables(); + bool createTables(QSqlDatabase & db); /** * Upgrades database tables if necessary @@ -149,13 +146,13 @@ private: // Methods * performs the upgrade without a loss of data if possible. * Returns false if upgrade is not possible or fails. */ - bool upgradeTables(); + bool upgradeTables(QSqlDatabase & db); /** * Loads data from the database * @return True if ok; false if failed */ - bool loadData(); + bool loadData(QSqlDatabase & db); };