X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Fapps%2FPswGen%2FStorage%2Fmodule.cpp;h=fc75dbb194fc57070e34baaa3a37bb3d9ec8a376;hb=13dedf19c628b9967e4c4106ab0fd967f1df0b90;hp=3c18b9a7cf64b3870eefa0b1aa9d0a16e357a623;hpb=0c61450fcffcb36a6e23ca5b083c26543d956084;p=evaf
diff --git a/src/apps/PswGen/Storage/module.cpp b/src/apps/PswGen/Storage/module.cpp
index 3c18b9a..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,6 +139,11 @@ bool StorageImpl::init()
void StorageImpl::done()
{
mData.clear();
+ if (QSqlDatabase::contains(DbConnectionName))
+ {
+ QSqlDatabase::removeDatabase(DbConnectionName);
+ EVAF_INFO("Removed database %s", DbConnectionName);
+ }
EVAF_INFO("%s finalized", qPrintable(objectName()));
}
@@ -143,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;
}
@@ -158,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;
}
@@ -199,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;
}
@@ -223,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;")) {
@@ -234,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;
}
@@ -242,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;
}