X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;ds=inline;f=src%2Fapps%2FPswGen%2FStorage%2Fmodule.cpp;h=09efe2e6b23730ee5ee76960890b2e53a8dcd64e;hb=4a845c9a4d543528ab0284835c84c3f4e0bf7b25;hp=b7676abf282f0d1323855a54919a676048619a43;hpb=1fe7d2eb7cb9c7e362078791d8e3b9ba4d500e44;p=evaf
diff --git a/src/apps/PswGen/Storage/module.cpp b/src/apps/PswGen/Storage/module.cpp
index b7676ab..09efe2e 100644
--- a/src/apps/PswGen/Storage/module.cpp
+++ b/src/apps/PswGen/Storage/module.cpp
@@ -3,7 +3,7 @@
* @brief Implementation of the iStorage interface
* @author Enar Vaikene
*
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2012 Enar Vaikene
*
* This file is part of the eVaf C++ cross-platform application development framework.
*
@@ -18,7 +18,6 @@
*/
#include "module.h"
-#include "version.h"
#include
#include
@@ -29,7 +28,6 @@
#include
VER_EXPORT_VERSION_INFO()
-Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, eVaf::PswGen::Storage::Module)
using namespace eVaf;
using namespace eVaf::PswGen;
@@ -137,6 +135,7 @@ bool StorageImpl::init()
void StorageImpl::done()
{
mData.clear();
+ mDb.close();
EVAF_INFO("%s finalized", qPrintable(objectName()));
}
@@ -150,8 +149,8 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointermodified()) {
QSqlQuery q(mDb);
- if (!q.exec(QString("UPDATE data SET length = \'%1\', flags = \'%2\' WHERE name = \'%3\';")
- .arg(data->length()).arg(data->flags()).arg(name))) {
+ 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();
EVAF_ERROR("Failed to update \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
return false;
@@ -161,8 +160,8 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointerlength())
+ if (!q.exec(QString("INSERT INTO data (name, suffix, length, flags) VALUES (\'%1\', \'%2\', %3, %4);")
+ .arg(name).arg(data->suffix()).arg(data->length())
.arg(int(data->flags())))) {
QSqlError err = mDb.lastError();
EVAF_ERROR("Failed to insert \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
@@ -170,10 +169,10 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointerreset();
@@ -210,11 +209,13 @@ bool StorageImpl::createTables()
return false;
}
- if (q.isActive() && q.isSelect() && q.first())
- return true; // We already have a table called 'data'
+ if (q.isActive() && q.isSelect() && q.first()) {
+ // Check if the table needs to be upgraded
+ return upgradeTables();
+ }
// Create the 'data' table
- if (!q.exec("CREATE TABLE data (name text primary key not null, length integer, flags integer);")) {
+ if (!q.exec("CREATE TABLE data (name text primary key not null, suffix text, length integer, flags integer);")) {
QSqlError err = mDb.lastError();
EVAF_ERROR("Failed to create table \'data\' : %s", qPrintable(err.text()));
return false;
@@ -223,22 +224,42 @@ bool StorageImpl::createTables()
return true;
}
+bool StorageImpl::upgradeTables()
+{
+ QSqlQuery q(mDb);
+
+ // Check if the 'suffix' column exists
+ if (q.exec("SELECT suffix from data;")) {
+ return true;
+ }
+
+ // Add the 'suffix' columnt
+ if (!q.exec("ALTER TABLE data ADD COLUMN suffix TEXT;")) {
+ QSqlError err = mDb.lastError();
+ EVAF_ERROR("Failed to upgrade table \'data\' : %s", qPrintable(err.text()));
+ return false;
+ }
+
+ return true;
+}
+
bool StorageImpl::loadData()
{
QSqlQuery q(mDb);
- if (!q.exec("SELECT name, length, flags FROM data;")) {
+ if (!q.exec("SELECT name, suffix, length, flags FROM data;")) {
QSqlError err = mDb.lastError();
EVAF_ERROR("Failed to query database : %s", qPrintable(err.text()));
return false;
}
+ beginResetModel();
while (q.next()) {
QString name = q.value(0).toString();
- QExplicitlySharedDataPointer data(new Storage::Data(name, q.value(1).toInt(), uint(q.value(2).toInt())));
+ QExplicitlySharedDataPointer data(
+ new Storage::Data(name, q.value(1).toString(), q.value(2).toInt(), uint(q.value(3).toInt())));
mData.insert(name, data);
}
-
- reset();
+ endResetModel();
return true;
}