Reset data model after succesful update.
[emufront] / src / db / dbexecutable.cpp
index c7c0b2f..391ada8 100644 (file)
@@ -5,9 +5,9 @@
 //
 //
 // 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.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
 //
 // EmuFront is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY{} without even the implied warranty of
 #include "dbsetup.h"
 #include "../dataobjects/executable.h"
 
+
 DbExecutable::DbExecutable(QObject *parent)
     : DbQueryModelManager(parent)
 {
     dbSetup = new DbSetup(this);
+    tableName = DbExecutable::DB_TABLE_EXECUTABLE;
 }
 
+
+/* Throws EmuFrontException */
 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();
-    EmuFrontObject *ob = dbSetup->getDataObject(supid);
+    EmuFrontObject *ob = dbSetup->getDataObject(supid); /* Throws EmuFrontException */
     Setup *sup = dynamic_cast<Setup*>(ob);
     qDebug() << "Setup id " << sup->getId() << ", platform " << sup->getPlatform()->getName();
     QString name = rec->value(Executable_Name).toString();
@@ -69,6 +73,7 @@ bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
     q.bindValue(":type", ex->getType());
     q.bindValue(":id", ex->getId());
     ret = q.exec();
+    if (ret) resetModel();
     if (!ret)
         qDebug() << q.lastError().text();
     return ret;
@@ -104,12 +109,6 @@ bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
     return false;
 }
 
-int DbExecutable::countDataObjectRefs(int) const
-{
-    // TODO
-    return 0;
-}
-
 QString DbExecutable::constructSelectById(int id) const
 {
     return constructSelect(
@@ -123,7 +122,7 @@ QString DbExecutable::constructFilterById(int id) const
 
 QString DbExecutable::constructSelect(QString whereClause) const
 {
-    return QString("SELECT "
+    QString sql = QString("SELECT "
         "executable.id AS ExecutableId, "
         "executable.name AS ExecutableName, "
         "executable.executable AS Executable, "
@@ -136,19 +135,20 @@ QString DbExecutable::constructSelect(QString whereClause) const
         "INNER JOIN platform ON setup.platformid=platform.id "
         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
         "%1 "
-        "ORDER BY executable.name ")
-        .arg(whereClause);
+        "ORDER BY executable.name ").arg(whereClause);
+    qDebug() << sql;
+    return sql;
 }
 
-bool DbExecutable::deleteDataObject(int id) const
+/*bool DbExecutable::deleteDataObject(int id) const
 {
     // TODO
     return false;
-}
+}*/
 
 QSqlQueryModel* DbExecutable::getData()
 {
-    QSqlQueryModel *model = new QSqlQueryModel;
+    QSqlQueryModel *model = new QSqlQueryModel(this);
     QString select = constructSelect();
     qDebug() << select;
     model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
@@ -162,3 +162,17 @@ QSqlQueryModel* DbExecutable::getData()
     return model;
 }
 
+QString DbExecutable::getCountRefsSelect(int id) const
+{
+    // These objects don't have references from other objects
+    // currently.
+    return QString("SELECT 0");
+}
+
+void DbExecutable::filterByPlatformMediaType(int platformId, int mediaTypeId)
+{
+    QList<QString> filters;
+    filters.append(QString("setup.platformid=%1").arg(platformId));
+    filters.append(QString("setup.mediatypeid=%1").arg(mediaTypeId));
+    filterDataObjects(filters);
+}