#include <QtCore>
#include <QtSql/QtSql>
+#include <QtSql/QSqlDatabase>
+
VER_EXPORT_VERSION_INFO()
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
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()));
}
EVAF_TEST_X(data, "Data cannot be null");
EVAF_TEST_X(!name.isEmpty(), "Name cannot be empty");
+ if (!QSqlDatabase::contains(DbConnectionName))
+ {
+ EVAF_ERROR("Failed to get DB connection");
+ return false;
+ }
+ QSqlDatabase db = QSqlDatabase::database(DbConnectionName);
+
// Is it an update or a new data record?
if (mData.constFind(name) != mData.constEnd()) {
// This is an update
if (data->modified()) {
- 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;
}
}
else {
// Store to the database
- QSqlQuery q(mDb);
+ QSqlQuery q(db);
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();
+ QSqlError err = db.lastError();
EVAF_ERROR("Failed to insert \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
return false;
}
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;
}
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;")) {
// 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;
}
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;
}
#include <QString>
#include <QAbstractListModel>
#include <QMap>
-#include <QtSql/QSqlDatabase>
+class QSqlDatabase;
namespace eVaf {
namespace PswGen {
/// Name of the database file without path
static char const * const DbName;
- /// Database connection
- QSqlDatabase mDb;
-
/// List of name/data pairs
QMap<QString, QExplicitlySharedDataPointer<Storage::Data> > mData;
* Creates database tables if necessary
* @return True if ok; false if failed
*/
- bool createTables();
+ bool createTables(QSqlDatabase & db);
/**
* Upgrades database tables if necessary
* 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);
};