From 2697f2c44e7f95355f26fdd9217fc89e5100d943 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Tue, 16 Nov 2010 23:01:51 +0200 Subject: [PATCH] Added database version field to config db table. Added db version check. EmuFront will not start if the database version doesn't match the required version by application. Moved database testing and creation from main to MainWindow class. --- src/db/databasemanager.h | 2 +- src/db/dbcreator.cpp | 44 ++++++++++++++++++++++++++++++++------- src/db/dbcreator.h | 7 ++++--- src/main.cpp | 36 +------------------------------- src/mainwindow.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++- src/mainwindow.h | 3 ++- 6 files changed, 95 insertions(+), 48 deletions(-) diff --git a/src/db/databasemanager.h b/src/db/databasemanager.h index d9ac18a..e355239 100644 --- a/src/db/databasemanager.h +++ b/src/db/databasemanager.h @@ -71,7 +71,7 @@ protected: static const QString DB_TABLE_EXECUTABLE; private: - static const QString DB_FILENAME; + static const QString DB_FILENAME; static const QString DATABASE; virtual QSqlQueryModel* getData() = 0; static QString getDbPath(); diff --git a/src/db/dbcreator.cpp b/src/db/dbcreator.cpp index 4f8e139..2ffca2c 100644 --- a/src/db/dbcreator.cpp +++ b/src/db/dbcreator.cpp @@ -18,17 +18,22 @@ // along with EmuFront. If not, see . #include +#include #include #include +#include +#include #include #include #include #include "dbcreator.h" + using namespace std; -const int DbCreator::TABLES_COUNT = 3; -const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer_filepath", "mediaimage", "mediaimagecontainer_mediaimage"}; +const int DbCreator::DB_VERSION = 1; +//const int DbCreator::TABLES_COUNT = 3; +//const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer_filepath", "mediaimage", "mediaimagecontainer_mediaimage"}; DbCreator::DbCreator(QObject *parent) : QObject(parent) { @@ -55,9 +60,21 @@ bool DbCreator::createDB() qDebug() << "Creating TABLE file"; ret = query.exec("CREATE TABLE IF NOT EXISTS config" - "(tmpdirpath TEXT)" + "(tmpdirpath TEXT, " + "dbversion INTEGER)" ); + if (ret) { + query.prepare("INSERT INTO config " + "(tmpdirpath, dbversion) " + "VALUES (:tmpdir, :dbversion)"); + query.bindValue(":tmpdir", QDir::homePath()); + query.bindValue(":dbversion", DbCreator::DB_VERSION); + ret = query.exec(); + } + + if (!ret) throw QString("tbl config"); + ret = query.exec("CREATE TABLE IF NOT EXISTS file" "(id INTEGER PRIMARY KEY, " "name TEXT, " @@ -211,11 +228,24 @@ bool DbCreator::createDB() /** * Check if database already exists. - * Returns false if doesn't or we don't have a connection. + * + * Returns 0 if database doesn't exist + * or database version number 1 if database exists + * */ -bool DbCreator::dbExists() +int DbCreator::dbExists() { - for (int i = 0; i < TABLES_COUNT; ++i) + int ret = 0; + QString sql("SELECT dbversion FROM config"); + QSqlQuery q; + q.exec(sql); + if (q.next()) { + ret = q.value(0).toInt(); + qDebug() << "Database version is " << ret + << " the application requires " << DB_VERSION; + } + return ret; + /*for (int i = 0; i < TABLES_COUNT; ++i) { if (!tableExists(TABLES[i])) { @@ -224,7 +254,7 @@ bool DbCreator::dbExists() } qDebug() << "Table " << TABLES[i] << " exists."; } - return true; + return true;*/ } bool DbCreator::tableExists(QString TABLE) diff --git a/src/db/dbcreator.h b/src/db/dbcreator.h index 146a807..acac6d1 100644 --- a/src/db/dbcreator.h +++ b/src/db/dbcreator.h @@ -25,17 +25,18 @@ class DbCreator : public QObject { public: + static const int DB_VERSION; DbCreator(QObject *parent = 0); bool deleteDB(); static bool tableExists(QString); - static bool dbExists(); + static int dbExists(); bool createDB(); private: - static const int TABLES_COUNT; - static const QString TABLES[]; + //static const int TABLES_COUNT; + //static const QString TABLES[]; }; diff --git a/src/main.cpp b/src/main.cpp index 3f7ba60..a8a4295 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,48 +19,14 @@ #include #include -#include -#include #include "mainwindow.h" -#include "db/databasemanager.h" -#include "db/dbcreator.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - QTextStream cout(stdout, QIODevice::WriteOnly); QStringList arglst = app.arguments(); - bool reset = arglst.contains("reset", Qt::CaseInsensitive); - - - if (reset) qDebug() << "Database Reset requested"; - - if (DatabaseManager::openDB()) - cout << " Database opened succesfully!" << endl; - else - { - cout << " Database connection failed!" << endl; - exit(1); - } - - if (reset || !DbCreator::dbExists()) - { - try - { - cout << " Database is missing!" << endl; - DbCreator dbCreator; - dbCreator.createDB(); - cout << " Database created succesfully!" << endl; - } - catch (QString str) { - cout << str << endl; - exit(1); - } - } - else cout << " Database exists!" << endl; - MainWindow mw; mw.show(); - return app.exec(); + return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d5d5583..43ab762 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -27,6 +27,7 @@ #include "dialogs/executablemaindialog.h" #include "utils/datfileutil.h" #include "db/databasemanager.h" +#include "db/dbcreator.h" #include "db/dbconfig.h" QString MainWindow::aboutStr = trUtf8( @@ -40,8 +41,9 @@ QString MainWindow::aboutStr = trUtf8( QString MainWindow::aboutTitle = tr("About EmuFront"); -MainWindow::MainWindow() +MainWindow::MainWindow(bool reset) { + if (!testDB(reset)) close(); setWindowTitle("EmuFront"); tmpDirFilePath = DbConfig::getTmpDir(); if (tmpDirFilePath.isEmpty()) @@ -243,3 +245,50 @@ void MainWindow::about() { QMessageBox::about(this, aboutTitle, aboutStr ); } + +bool MainWindow::testDB(bool reset) +{ + try { + if (DatabaseManager::openDB()) { + qDebug() << " Database opened succesfully!"; + } + else { + throw EmuFrontException("Database connection failed!"); + } + + int dbVer = DbCreator::dbExists(); + if (dbVer == 0) reset = true; + if (!reset && dbVer != DbCreator::DB_VERSION) { + QString msg("Database is not compatible " + "with current version of EmuFront!" + "Do you want to continue to recreate the database?" + "ALL THE CURRENT DATA WILL BE LOST!!!"); + if (QMessageBox::question(this, "Database not compatible!", msg, + QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) { + reset = true; + } + else throw EmuFrontException("The current database is not compatible!" + " Cannot continue."); + } + + if (reset) + { + try + { + DbCreator dbCreator; + dbCreator.createDB(); + } + catch (QString str) { + QString msg(tr("Exception while trying to create" + " EmuFront database: %s").arg(str)); + throw EmuFrontException(msg); + } + } + return true; + } + catch (EmuFrontException e) { + qDebug() << e.what(); + QMessageBox::critical(this, "Exception", e.what()); + return false; + } +} diff --git a/src/mainwindow.h b/src/mainwindow.h index c694ba5..2a258ee 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -40,7 +40,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - MainWindow(); + MainWindow(bool reset = false); //~MainWindow() protected: @@ -68,6 +68,7 @@ private: bool okToContinue(); void connectSignals(); void activateDialog(EmuFrontDialog*) const; + bool testDB(bool reset); PlatformDialog *platformDialog; MediaTypeDialog *mediaTypeDialog; MediaImagePathMainDialog *mediaImagePathDialog; -- 1.7.9.5