Data object removal implemented in the db layer. UI refreshing added
[emufront] / src / db / dbemufrontfileobject.cpp
index a36cc00..0ee19a5 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 <QSqlRecord>
 #include <QSqlQuery>
+#include <QSqlError>
 #include <QSqlRelationalTableModel>
 #include <QDebug>
 #include "dbemufrontfileobject.h"
 
 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
-    : DbTableModelManager(parent)
+    : DbQueryModelManager(parent)
 {
     dbFile = new DbFile(this);
 }
@@ -48,50 +49,63 @@ bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
 {
     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
     bool ret = false;
-    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
-    tmodel->setFilter(QString("id = %1").arg(plf->getId()));
-    tmodel->select();
-    if (tmodel->rowCount() == 1)
-    {
-        QSqlRecord record = tmodel->record(0);
-        record.setValue("name", plf->getName());
-        if (plf->getFile())
-            record.setValue("fileid", plf->getFile()->getId());
-        else record.setNull("fileid");
-        tmodel->setRecord(0, record);
-        ret = tmodel->submitAll();
-    }
-    resetModel();
+    QSqlQuery query;
+    query.prepare(QString("UPDATE %1 SET "
+        "name=:name, "
+        "fileid=:fileid "
+        "WHERE id=:id").arg(tableName));
+    query.bindValue(":name", plf->getName());
+    query.bindValue(":fileid",
+                    plf->getFile() ? QString(plf->getFile()->getId()) : "NULL");
+    query.bindValue(":id", plf->getId());
+    ret = query.exec();
+
+    if (ret) resetModel();
+    else
+            qDebug() << "Failed updating " << tableName
+                << " " <<   query.lastError().text()
+                << " " << query.executedQuery() ;
     return ret;
 }
 
-bool DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
+int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
-    int row = 0;
-    if (!sqlTableModel) sqlTableModel = getDataModel();
-    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
-    tmodel->insertRows(row, 1);
-    // the null value for index will be set implicitily
-    // when we don't assign any value to cell 0 in the sql table model
-    //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
-    tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_Name), plf->getName());
+    QSqlQuery query;
+    query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
+        "VALUES (NULL, :name, :fileid) ").arg(tableName));
+    query.bindValue(":name", plf->getName());
     if (plf->getFile())
-        tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_FileId), plf->getFile()->getId());
-    return tmodel->submitAll();
+        query.bindValue(":fileid", plf->getFile()->getId());
+    else query.bindValue(":fileid", "NULL");
+    int id = -1;
+    if (query.exec())
+        id = query.lastInsertId().toInt();
+    else
+        qDebug() << "Failed inserting to " << tableName << " "
+            << query.lastError().text() << " " << query.executedQuery() ;
+    return id;
 }
 
-int DbEmuFrontFileObject::countDataObjectRefs(int id) const
+bool DbEmuFrontFileObject::deleteDataObject(int id) const
 {
-    return 0; // TODO
-    // return countRows("imagecontainer", "platformid", id);
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM %1 WHERE id = :id").arg(tableName));
+    q.bindValue(":id", id);
+    return q.exec();
+
 }
 
 // WARNING: this will delete also all the databindings to selected platform
 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
 {
+    // TODO
+    return false;
     //QSqlDatabase::database().transaction();
-    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
     /*QSqlRecord record = tmodel->record(index->row());
     int id = record.value(EmuFrontFileObject_Id).toInt();
     qDebug() << "Deleting platform " << id;
@@ -106,21 +120,57 @@ bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
             return false;
         }
     }*/
-    tmodel->removeRow(index->row());
+    /*tmodel->removeRow(index->row());
     tmodel->submitAll();
     return QSqlDatabase::database().commit();
+    */
+}
+
+QString DbEmuFrontFileObject::constructSelect(QString where) const
+{
+    /*QString where = whereClause.isEmpty()
+        ? "" : QString("WHERE ").append(whereClause);*/
+
+    return QString("SELECT maintbl.id AS FileObjectId, "
+            "maintbl.name AS Name, "
+            "file.id AS FileId, "
+            "file.name AS FileName, "
+            "file.type AS FileType, "
+            "file.checksum AS FileChecksum, "
+            "file.size AS FileSize, "
+            "file.updatetime AS FileUpdateTime "
+            "FROM %1 AS maintbl "
+            "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
+            "%2 "
+            "ORDER BY Name").arg(tableName).arg(where);
+}
+
+QString DbEmuFrontFileObject::constructSelectById(int id) const
+{
+    return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
+}
+
+QString DbEmuFrontFileObject::constructFilterById(int id) const
+{
+    return QString("maintbl.id = %1").arg(id);
 }
 
 QSqlQueryModel* DbEmuFrontFileObject::getData()
 {
-    QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
-    model->setTable(tableName);
-    // TODO: table realtion model seems not to be suitable for this
-    // since not always does data object have a file relation:
-    //model->setRelation(EmuFrontFileObject_FileId, QSqlRelation("file", "id", "name"));
-    model->setSort(EmuFrontFileObject_Name, Qt::AscendingOrder);
+    QSqlQueryModel *model = new QSqlQueryModel;
+    model->setQuery(constructSelect());
+    model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
-    model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("Icon"));
-    model->select();
+    /*model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("File id"));
+    model->setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File name"));
+    model->setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File type"));
+    model->setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File checksum"));
+    model->setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File size"));
+    model->setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File update time"));*/
     return model;
 }
+
+QString DbEmuFrontFileObject::getDeleteObjectSql() const
+{
+    return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);
+}