Implemented initial deleteDataObject(id) in Db-layer. Implemented
[emufront] / src / db / dbemufrontfileobject.cpp
index 992c516..0193e66 100644 (file)
@@ -9,22 +9,23 @@
 // 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,
+// 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #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,37 +49,55 @@ 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 :table (id, name, fileid) "
+        "VALUES (NULL, :name, :fileid) "));
+    query.bindValue(":table", 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;
+}
+
+bool DbEmuFrontFileObject::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM :table WHERE id=:id"));
+    q.bindValue(":table", tableName);
+    q.bindValue(":id", id);
+    return q.exec();
 }
 
 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
@@ -90,8 +109,9 @@ int DbEmuFrontFileObject::countDataObjectRefs(int id) const
 // WARNING: this will delete also all the databindings to selected platform
 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
 {
+    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 +126,52 @@ 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 whereClause) 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(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;
 }