Reset data model after succesful update.
[emufront] / src / db / dbsetup.cpp
index cd920f9..85fcfcc 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
@@ -29,21 +29,25 @@ const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
 
 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
 {
+    tableName = DbSetup::DB_TABLE_NAME_SETUP;
     dbPlatform = new DbPlatform(this);
     dbMediaType = new DbMediaType(this);
 }
 
+/* Throws EmuFrontException */
 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
 {
     Setup *s = 0;
     if (!rec) return s;
     int id = rec->value(Setup_Id).toInt();
-    QString extensions = rec->value(Setup_FileTypeExtensions).toString();
-    QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
+    QString extensions = rec->value(Setup_FileTypeExtensions).toString().trimmed();
+    QStringList list;
+    if (!extensions.isEmpty())
+        list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
     int plfId = rec->value(Setup_PlatformId).toInt();
     int mtId = rec->value(Setup_MediaTypeId).toInt();
-    Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
-    MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
+    Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId)); /* Throws EmuFrontException */
+    MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId)); /* Throws EmuFrontException */
     s = new Setup(id, plf, mt, list);
     return s;
 }
@@ -56,19 +60,26 @@ bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
     query.prepare(QString("UPDATE setup SET "
         "platformid=:platformid, "
         "mediatypeid=:mediatypeid, "
-        "filetypeextensions:=filetypeextensions "
+        "filetypeextensions=:filetypeextensions "
         "WHERE id = :id"));
     if (fpo->getPlatform())
         query.bindValue(":platformid", fpo->getPlatform()->getId());
     if (fpo->getMediaType())
         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
-    query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
+    query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
     query.bindValue(":id", fpo->getId());
-    query.exec();
+    ret = query.exec();
+    if (ret) resetModel();
+    if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
     return ret;
 }
 
-bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
+QString DbSetup::supportedExtensionsToDb(QStringList list)
+{
+    return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
+}
+
+int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     qDebug() << "Inserting setup to database...";
     const Setup *fpo = dynamic_cast<const Setup*>(ob);
@@ -77,52 +88,54 @@ bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
-    QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
-    qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
     query.bindValue(":platformid", plfId);
     query.bindValue(":mediatypeid", mtId);
-    query.bindValue(":filetypeextensions", exts);
-    bool ret = query.exec();
-    if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
-    return ret;
-}
-
-int DbSetup::countDataObjectRefs(int ) const
-{
-    return 0;
+    query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
+    int id = -1;
+    if (query.exec())
+        id = query.lastInsertId().toInt();
+    else
+        qDebug() << query.lastError().text() << query.executedQuery();
+    return id;
 }
 
-QString DbSetup::constructSelect(QString whereClause) const
+QString DbSetup::constructSelect(QString where) const
 {
-    QString where = whereClause.isEmpty()
-        ? "" : QString("WHERE ").append(whereClause);
     return QString(
         "SELECT setup.id AS SetupId, "
         "setup.platformid AS PlatformId, "
         "setup.mediatypeid AS MediaTypeId, "
         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
         "platform.name || ' ' || mediatype.name AS SetupName "
-        "FROM setup %1"
+        "FROM setup "
         "INNER JOIN platform ON setup.platformid=platform.id "
-        "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+        "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
         "ORDER BY SetupName"
         ).arg(where);
 }
 
+QString DbSetup::constructFilterById(int id) const
+{
+     return QString("setup.id = %1").arg(id);
+}
+
+
 QString DbSetup::constructSelectById(int id) const
 {
-     return constructSelect(QString("setup.id = %1").arg(id));
+    return constructSelect(
+        QString("WHERE %1").arg(constructFilterById(id)));
 }
 
 // WARNING: this will delete also all the databindings to selected media image path
 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
 {
+    qDebug() << "This is not currently supported";
     return false;
 }
 
 QSqlQueryModel* DbSetup::getData()
 {
-    QSqlQueryModel *model = new QSqlQueryModel;
+    QSqlQueryModel *model = new QSqlQueryModel(this);
     QString select = constructSelect();
     qDebug() << select;
     model->setQuery(select);
@@ -133,3 +146,16 @@ QSqlQueryModel* DbSetup::getData()
     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
     return model;
 }
+
+QString DbSetup::getCountRefsSelect(int id) const
+{
+    /* setups are referenced by executable and filepath */
+    return QString("SELECT count(*) FROM "
+            "(SELECT setup.id FROM setup "
+              "INNER JOIN executable ON setup.id=executable.setupid "
+              "WHERE setup.id=%1 "
+              "UNION ALL "
+              "SELECT setup.id FROM setup "
+              "INNER JOIN filepath ON setup.id=filepath.setupid "
+              "WHERE setup.id=%1)").arg(id);
+}