Initial implementation DbExecutable: data base functionality for
authorMikko Keinänen <mikko.keinanen@gmail.com>
Mon, 4 Oct 2010 21:03:15 +0000 (00:03 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Mon, 4 Oct 2010 21:03:15 +0000 (00:03 +0300)
Executable objects.

doc/uml-db_layer.dia
src/dataobjects/executable.h
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbexecutable.cpp [new file with mode: 0644]
src/db/dbexecutable.h [new file with mode: 0644]
src/db/dbsetup.cpp
src/emufront.pro

index e2b38ab..2605b89 100644 (file)
Binary files a/doc/uml-db_layer.dia and b/doc/uml-db_layer.dia differ
index 3d0a0ee..949c50d 100644 (file)
@@ -29,7 +29,8 @@ class Executable : public EmuFrontObject
 public:
     Executable();
     Executable(int id, QString name);
-    Executable(int id, QString name, QString executable, QString options, Setup*, int type);
+    Executable(int id, QString name, QString executable,
+        QString options, Setup*, int type);
     Executable(const Executable &);
     ~Executable();
     Executable& operator =(const Executable &);
index 6a639d7..69b9682 100644 (file)
@@ -39,6 +39,7 @@ const QString DatabaseManager::DB_TABLE_NAME_FILEPATH = QString("filepath");
 const QString DatabaseManager::DB_TABLE_NAME_SETUP = QString("setup");
 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER = QString("mediaimagecontainer");
 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER_MEDIAIMAGE = QString("mediaimagecontainer_mediaimage");
+const QString DatabaseManager::DB_TABLE_EXECUTABLE = QString("executable");
 
 DatabaseManager::DatabaseManager(QObject *parent)
        : QObject(parent)
index 77b9e1c..4b9f113 100644 (file)
@@ -67,6 +67,7 @@ protected:
     static const QString DB_TABLE_NAME_SETUP;
     static const QString DB_TABLE_MEDIAIMAGECONTAINER;
     static const QString DB_TABLE_MEDIAIMAGECONTAINER_MEDIAIMAGE;
+    static const QString DB_TABLE_EXECUTABLE;
 
 
 private:
diff --git a/src/db/dbexecutable.cpp b/src/db/dbexecutable.cpp
new file mode 100644 (file)
index 0000000..0b3ce08
--- /dev/null
@@ -0,0 +1,143 @@
+// 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.
+//
+// EmuFront 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QDebug>
+#include <QSqlRecord>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QSqlRelationalTableModel>
+#include "dbexecutable.h"
+#include "dbsetup.h"
+#include "../dataobjects/executable.h"
+
+DbExecutable::DbExecutable(QObject *parent)
+    : DbQueryModelManager(parent)
+{
+    dbSetup = new DbSetup(this);
+}
+
+EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
+{
+    Executable *ex = 0;
+    if (!rec) return ex;
+    int id = rec->value(Executable_Id).toInt();
+    int supid = rec->value(Executable_SetupId).toInt();
+    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(supid));
+    QString name = rec->value(Executable_Name).toString();
+    QString exec = rec->value(Executable_Executable).toString();
+    QString opts = rec->value(Executable_Options).toString();
+    int type = rec->value(Executable_TypeId).toInt();
+    ex = new Executable(id, name, exec, opts, sup, type);
+    return ex;
+}
+
+bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
+{
+    const Executable *ex = dynamic_cast<const Executable*>(ob);
+    bool ret = false;
+    QSqlQuery q;
+    q.prepare("UPDATE executable SET "
+              "name=:name, "
+              "executable=:executable, "
+              "options=:options, "
+              "setupid=:setupid, "
+              "type=:type "
+              "WHERE id=:id");
+    q.bindValue(":setupid", ex->getSetup()
+                ? QString(ex->getSetup()->getId()) : "NULL"); // TODO: null shouln't be allowed here
+    q.bindValue(":name", ex->getName());
+    q.bindValue(":executable", ex->getExecutable());
+    q.bindValue(":options", ex->getOptions());
+    q.bindValue(":type", ex->getType());
+    q.bindValue(":id", ex->getId());
+    ret = q.exec();
+    if (!ret)
+        qDebug() << q.lastError().text();
+    return ret;
+}
+
+int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
+{
+    const Executable *ex = dynamic_cast<const Executable*>(ob);
+    QSqlQuery q;
+    q.prepare("INSERT INTO executable "
+        "(id, name, executable, options, setupid, type) "
+        "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
+
+    q.bindValue(":setupid", ex->getSetup()
+                ? QString(ex->getSetup()->getId()) : "NULL"); // TODO: null shouln't be allowed here
+    q.bindValue(":name", ex->getName());
+    q.bindValue(":executable", ex->getExecutable());
+    q.bindValue(":options", ex->getOptions());
+    q.bindValue(":type", ex->getType());
+    int id = -1;
+    if (q.exec())
+        id = q.lastInsertId().toInt();
+    else qDebug() << q.lastError().text();
+    return id;
+}
+
+bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
+{
+    // TODO
+    return false;
+}
+
+int DbExecutable::countDataObjectRefs(int) const
+{
+    // TODO
+    return 0;
+}
+
+QString DbExecutable::constructSelectById(int id) const
+{
+    return constructSelect(
+        QString("WHERE %1").arg(constructFilterById(id)));
+}
+
+QString DbExecutable::constructFilterById(int id) const
+{
+    return QString("executable.id=%1").arg(id);
+}
+
+QString DbExecutable::constructSelect(QString whereClause) const
+{
+    return QString("SELECT "
+        "executable.id AS ExecutableId, "
+        "executable.name AS ExecutableName, "
+        "executable.executable AS Executable, "
+        "executable.options AS ExecutableOptions, "
+        "executable.type AS ExecutableType, "
+        "setup.id As ExecutableSetupId "
+        "platform.name || ' ' || mediatype.name AS SetupName "
+        "FROM executable "
+        "INNER JOIN setup ON executable.setupid = setup.id "
+        "INNER JOIN platform ON setup.platformid=platform.id "
+        "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+        "% "
+        "ORDER BY executable.name ")
+        .arg(whereClause);
+}
+
+bool DbExecutable::deleteDataObject(int id) const
+{
+    // TODO
+    return false;
+}
+
diff --git a/src/db/dbexecutable.h b/src/db/dbexecutable.h
new file mode 100644 (file)
index 0000000..dac954c
--- /dev/null
@@ -0,0 +1,53 @@
+// 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.
+//
+// EmuFront 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef DBEXECUTABLE_H
+#define DBEXECUTABLE_H
+
+#include "dbquerymodelmanager.h"
+
+class DbSetup;
+
+class DbExecutable : public DbQueryModelManager
+{
+public:
+    DbExecutable(QObject*);
+     virtual bool updateDataObjectToModel(const EmuFrontObject*);
+    int insertDataObjectToModel(const EmuFrontObject*);
+    bool deleteDataObjectFromModel(QModelIndex*);
+    int countDataObjectRefs(int) const;
+    enum {
+        Executable_Id = 0,
+        Executable_Name,
+        Executable_Executable,
+        Executable_Options,
+        Executable_TypeId,
+        Executable_SetupId
+    };
+protected:
+    virtual EmuFrontObject* recordToDataObject(const QSqlRecord*);
+    virtual QString constructSelectById(int id) const;
+    virtual QString constructFilterById(int id) const;
+    virtual QString constructSelect(QString whereClause = "") const;
+    virtual bool deleteDataObject(int id) const;
+private:
+    DbSetup *dbSetup;
+};
+
+#endif // DBEXECUTABLE_H
index fc4bc08..6884cad 100644 (file)
@@ -93,7 +93,8 @@ int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
 
 int DbSetup::countDataObjectRefs(int ) const
 {
-    return 0;
+    // TODO
+    return -1;
 }
 
 QString DbSetup::constructSelect(QString where) const
@@ -121,7 +122,8 @@ QString DbSetup::constructFilterById(int id) const
 
 QString DbSetup::constructSelectById(int id) const
 {
-    return constructSelect(QString("WHERE %1").append(constructFilterById(id)));
+    return constructSelect(
+        QString("WHERE %1").arg(constructFilterById(id)));
 }
 
 // WARNING: this will delete also all the databindings to selected media image path
@@ -133,7 +135,8 @@ bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
 
 bool DbSetup::deleteDataObject(int id) const
 {
-    if (countDataObjectRefs(id) > 0)
+    int c = countDataObjectRefs(id);
+    if (c != 0)
         // TODO
         return false;
     QSqlQuery q;
index 71a07ab..9c48fca 100644 (file)
@@ -57,7 +57,8 @@ HEADERS += mainwindow.h \
     widgets/efcombobox.h \
     widgets/effileobjectcombobox.h \
     widgets/setupcombobox.h \
-    dataobjects/executable.h
+    dataobjects/executable.h \
+    db/dbexecutable.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -102,5 +103,6 @@ SOURCES += main.cpp \
     widgets/efcombobox.cpp \
     widgets/effileobjectcombobox.cpp \
     widgets/setupcombobox.cpp \
-    dataobjects/executable.cpp
+    dataobjects/executable.cpp \
+    db/dbexecutable.cpp
 OTHER_FILES +=