First unit test implemented!
[emufront] / src / db / dbfilepath.cpp
index ae29386..b75ef52 100644 (file)
@@ -5,50 +5,92 @@
 //
 //
 // 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.
 //
-// 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 <QSqlRelationalTableModel>
-#include "dbfilepath.h"
+#include <QSqlRecord>
+#include <QSqlQuery>
 #include "../dataobjects/filepathobject.h"
+#include "dbfilepath.h"
+#include "dbsetup.h"
 
-DbFilePath::DbFilePath(QObject *parent) : DatabaseManager(parent)
-{
-    sqlTableModel = getData();
-}
 
-QSqlTableModel* DbFilePath::getDataModel()
+DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
-    return sqlTableModel;
+    tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
+    dbSetup = new DbSetup(this);
 }
 
-EmuFrontObject* DbFilePath::getDataObjectFromModel(QModelIndex *index)
+/* Throws EmuFrontException */
+EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
 {
-    return new FilePathObject;
+    int id = rec->value(FilePath_Id).toInt();
+    QString fpath = rec->value(FilePath_Name).toString();
+    int setupId = rec->value(FilePath_SetupId).toInt();
+    int fileType = rec->value(FilePath_FileTypeId).toInt();
+    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId)); /* Throws EmuFrontException */
+       // TODO
+    //int lastScanned = 0;
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
 {
-    return false;
+    const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
+    bool ret = false;
+    QSqlQuery query;
+    query.prepare(QString("UPDATE filepath SET "
+        "name=:name, "
+        "filetypeid=:filetypeid, "
+        "setupid=:setupid, "
+        "lastscanned=:lastscanned "
+        "WHERE id=:id"));
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getType());
+    query.bindValue(":lastscanned", 0); // TODO
+    query.bindValue(":id", fpo->getId());
+    ret = query.exec();
+    if (ret) resetModel();
+    return ret;
 }
 
-bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
+bool DbFilePath::setScanned(const EmuFrontObject *ob)
 {
-    return false;
+    QSqlQuery q;
+    q.prepare("UPDATE filepath SET lastscanned=:lastscanned WHERE id=:id");
+    q.bindValue(":lastscanned", getCurrentTimeStamp());
+    q.bindValue(":id", ob->getId());
+    bool ret = q.exec();
+    if (ret) resetModel();
+    return ret;
 }
 
-int DbFilePath::countDataObjectRefs(int id) const
+/* Returns id of inserted data item after succesful insert, -1 if insert failed */
+int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
 {
-    return 0;
+    const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
+    QSqlQuery query;
+    query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
+        "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getType());
+    if (fpo->getSetup())
+        query.bindValue(":setupid", fpo->getSetup()->getId());
+    query.bindValue(":lastscanned", 0); // TODO
+    int id = -1;
+    if (query.exec())
+        id = query.lastInsertId().toInt();
+    return id;
 }
 
 // WARNING: this will delete also all the databindings to selected media image path
@@ -57,17 +99,62 @@ bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
     return false;
 }
 
-QSqlTableModel* DbFilePath::getData()
+bool DbFilePath::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
+}
+
+QString DbFilePath::constructSelect(QString where) const
+{
+    return QString("SELECT "
+            "filepath.id AS FilePathId, "
+            "filepath.name AS Name, "
+            "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
+            "setup.id AS SetupId, "
+            "platform.name || ' ' || mediatype.name AS SetupName, "
+            "filepath.filetypeid "
+            "FROM filepath "
+            "INNER JOIN setup ON filepath.setupid=setup.id  "
+            "INNER JOIN platform ON setup.platformid=platform.id "
+            "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+            "%1 "
+            "ORDER BY SetupName").arg(where);
+}
+
+QString DbFilePath::constructFilterById(int id) const
+{
+    return QString("filepath.id = %1").arg(id);
+}
+
+QString DbFilePath::constructSelectById(int id) const
+{
+    QString where = QString("WHERE %1").arg(constructFilterById(id));
+    return constructSelect(where);
+}
+
+QSqlQueryModel* DbFilePath::getData()
+{
+    QSqlQueryModel *model = new QSqlQueryModel(this);
+    model->setQuery(constructSelect());
+    model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
+    model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
+    model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
+    model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
+    model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
+    return model;
+}
+
+QString DbFilePath::getCountRefsSelect(int id) const
 {
-   QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
-   model->setTable(DB_TABLE_NAME_FILEPATH);
-   model->setRelation(FilePath_PlatformId,
-       QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
-   model->setRelation(FilePath_MediaTypeId,
-       QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
-   model->setSort(FilePath_Name, Qt::AscendingOrder);
-   model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
-   model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
-   model->select();
-   return model;
+    /* filepath is referenced from mediaimagecontainer */
+    return QString("SELECT count(*) FROM filepath"
+              "INNER JOIN mediaimagecontainer "
+              "ON filepath.id=mediaimagecontainer.filepathid"
+              "WHERE filepath.id=%1").arg(id);
 }