Reorganizing database creation to a new class: DbCreator
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 22 May 2010 22:22:16 +0000 (01:22 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 22 May 2010 22:22:16 +0000 (01:22 +0300)
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbcreator.cpp [new file with mode: 0644]
src/db/dbcreator.h [new file with mode: 0644]
src/emufront.pro
src/main.cpp

index 28102e7..af9071e 100644 (file)
@@ -26,9 +26,9 @@
 #include <QFile>
 #include <QDir>
 #include <QVariant>
-#include <iostream>
 
 const QString DatabaseManager::DB_FILENAME = QString("my.db.sqlite");
+const QString DatabaseManager::DATABASE = QString("QSQLITE");
 
 DatabaseManager::DatabaseManager(QObject *parent)
        : QObject(parent)
@@ -48,12 +48,6 @@ bool DatabaseManager::openDB()
     return db.open();
 }
 
-bool DatabaseManager::deleteDB()
-{
-    // return QFile::remove(getDbPath());
-    return false;
-}
-
 QString DatabaseManager::getDbPath()
 {
        QString path;
@@ -66,33 +60,6 @@ QString DatabaseManager::getDbPath()
        return path;
 }
 
-/**
- * Check if database already exists.
- * Returns false if doesn't or we don't have a connection.
-*/ 
-bool DatabaseManager::dbExists()
-{
-       QSqlQuery query;
-    query.exec("SELECT name FROM sqlite_master WHERE name='platform'");
-    return query.next();
-}
-
-bool DatabaseManager::createDB()
-{
-       bool ret = false;
-    QSqlQuery query;
-
-    ret = query.exec("create table platform "
-                     "(id integer primary key, "
-                     "name varchar(30), "
-                     "filename varchar(125))");
-    /*ret = query.exec("create table media "
-                                               "(id integer primary key, "
-                                               "name varchar(30), "
-                                               "filename varchar(125))");*/
-       return ret;
-}
-
 void DatabaseManager::resetModel() const
 {
     if (!sqlTableModel) return;
index aeb2b65..500132e 100644 (file)
@@ -35,16 +35,25 @@ public:
 
     virtual QSqlTableModel* getDataModel() = 0;
     static bool openDB();
-    static bool deleteDB();
-    static bool dbExists();
-    static bool createDB();
+    //static bool deleteDB();
+    //static bool dbExists();
+    //static bool tableExists(QString);
+    //static bool createDB();
     void resetModel() const;
+    enum
+    {
+        Filetype_MediaImageContainer = 0,
+        Filetype_Screenshot = 1,
+        Filetype_PlatformIcon = 2,
+        Filetype_MediaTypeIcon = 3
+                             };
 
 protected:
     QSqlTableModel *sqlTableModel;
 
 private:
        static const QString DB_FILENAME;
+    static const QString DATABASE;
     virtual QSqlTableModel* getData() = 0;
     static QString getDbPath();
 };
diff --git a/src/db/dbcreator.cpp b/src/db/dbcreator.cpp
new file mode 100644 (file)
index 0000000..2ae2b32
--- /dev/null
@@ -0,0 +1,89 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QObject>
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include "dbcreator.h"
+
+DbCreator::DbCreator(QObject *parent) : QObject(parent)
+{
+}
+
+
+bool DbCreator::createDB()
+{
+    bool ret = false;
+    QSqlQuery query;
+
+    if (!tableExists("platform"))
+    {
+        query.exec("create table platform "
+                         "(id integer primary key, "
+                         "name varchar(30), "
+                         "filename varchar(125))");
+    }
+    if (!tableExists("mediatype"))
+    {
+        query.exec("create table mediatype "
+                         "(id integer primary key, "
+                         "name varchar(30), "
+                         "filename varchar(125))");
+    }
+    if (!tableExists("filetype"))
+    {
+        query.exec("create table filetype "
+                            "(id integer primary key, "
+                            "name varchar(30))");
+        query.exec("insert into filetype (id, name) values (1, 'media image container')");
+        query.exec("insert into filetype (id, name) values (2, 'screenshot')");
+        query.exec("insert into filetype (id, name) values (3, 'platform icon')");
+        query.exec("insert into filetype (id, name) values (4, 'media type icon')");
+    }
+    /*if (!tableExists("filepath"))
+    {
+        query.exec("create table filepath "
+                    "(id integer primary key, "
+                    "name varchar(255))");
+
+    }*/
+    return ret;
+}
+
+/**
+ * Check if database already exists.
+ * Returns false if doesn't or we don't have a connection.
+*/
+bool DbCreator::dbExists()
+{
+    return tableExists("platform");
+}
+
+bool DbCreator::tableExists(QString table)
+{
+    QSqlQuery query;
+    query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
+    return query.next();
+}
+
+bool DbCreator::deleteDB()
+{
+    // return QFile::remove(getDbPath());
+    return false;
+}
diff --git a/src/db/dbcreator.h b/src/db/dbcreator.h
new file mode 100644 (file)
index 0000000..a1cf73a
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef DBCREATOR_H
+#define DBCREATOR_H
+
+#include <QObject>
+
+class DbCreator : public QObject
+{
+public:
+
+    DbCreator(QObject *parent = 0);
+    bool deleteDB();
+    static bool tableExists(QString);
+    static bool dbExists();
+    bool createDB();
+};
+
+#endif // DBCREATOR_H
index fbcb614..970ca59 100644 (file)
@@ -21,7 +21,8 @@ HEADERS += mainwindow.h \
     dialogs/emufrontdialog.h \
     dataobjects/emufrontobject.h \
     dataobjects/platform.h \
-    db/dbplatform.h
+    db/dbplatform.h \
+    db/dbcreator.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -32,4 +33,5 @@ SOURCES += main.cpp \
     dialogs/emufrontdialog.cpp \
     dataobjects/emufrontobject.cpp \
     dataobjects/platform.cpp \
-    db/dbplatform.cpp
+    db/dbplatform.cpp \
+    db/dbcreator.cpp
index f585380..443151d 100644 (file)
@@ -22,6 +22,8 @@
 #include <iostream>
 #include "mainwindow.h"
 #include "db/databasemanager.h"
+#include "db/dbcreator.h"
+
 //#include "dialogs/platformnamedialog.h"
 
 int main(int argc, char *argv[])
@@ -33,12 +35,13 @@ int main(int argc, char *argv[])
         cout << " Database opened succesfully!" << endl;
        else cout << " Database connection failed!" << endl;
 
-    if (DatabaseManager::dbExists())
+    if (DbCreator::dbExists())
                cout << " Database exists!" << endl;
        else 
        {
                cout << " Database is missing!" << endl;
-        if (DatabaseManager::createDB())
+        DbCreator dbCreator;
+        if (dbCreator.createDB())
                        cout << " Database created succesfully!" << endl;
                else {
                        cout << "Failed creating database!" << endl;