First unit test implemented!
[emufront] / src / db / dbfilepath.cpp
index 3420601..b75ef52 100644 (file)
@@ -5,17 +5,17 @@
 //
 //
 // 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 <QSqlRecord>
 #include "dbfilepath.h"
 #include "dbsetup.h"
 
+
 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
+    tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
     dbSetup = new DbSetup(this);
-    sqlTableModel = 0; //getData();
 }
 
-EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
+/* Throws EmuFrontException */
+EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
 {
     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));
+    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, fpath, 0, sup);
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
@@ -54,64 +56,41 @@ bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
         "lastscanned=:lastscanned "
         "WHERE id=:id"));
     query.bindValue(":name", fpo->getName());
-    query.bindValue(":filetypeid", fpo->getFiletype());
+    query.bindValue(":filetypeid", fpo->getType());
     query.bindValue(":lastscanned", 0); // TODO
     query.bindValue(":id", fpo->getId());
     ret = query.exec();
     if (ret) resetModel();
-    /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
-    sqlTableModel->select();
-    if (sqlTableModel->rowCount() == 1)
-    {
-        QSqlRecord rec = sqlTableModel->record(0);
-        rec.setValue("name", fpo->getName());
-        rec.setValue("filetypeid", fpo->getFilet.bype());
-
-        Setup *sup = fpo->getSetup();
-        if (sup) rec.setValue("setupid", sup->getId());
+    return ret;
+}
 
-        // TODO
-        //rec.setValue("lastscanned", 0);
-    }*/
+bool DbFilePath::setScanned(const EmuFrontObject *ob)
+{
+    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;
 }
 
-bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
+/* Returns id of inserted data item after succesful insert, -1 if insert failed */
+int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     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->getFiletype());
+    query.bindValue(":filetypeid", fpo->getType());
     if (fpo->getSetup())
         query.bindValue(":setupid", fpo->getSetup()->getId());
     query.bindValue(":lastscanned", 0); // TODO
-    return query.exec();
-    /*int row = 0;
-
-    sqlTableModel->insertRows(row, 1);
-
-
-    Setup *sup = fpo->getSetup();
-    //Platform *pl = fpo->getPlatform();
-    //MediaType *mt = fpo->getMediaType();
-
-    //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
-    // not all the file path types have platform and/or media type
-    //if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
-    //if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
-    if (sup) sqlTableModel->setData(sqlTableModel->index(row, FilePath_SetupId), sup->getId());
-    // TODO:
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
-    return sqlTableModel->submitAll();*/
-}
-
-int DbFilePath::countDataObjectRefs(int id) const
-{
-    return 0;
+    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
@@ -120,16 +99,26 @@ bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
     return false;
 }
 
-QString DbFilePath::constructSelect(QString whereClause) const
+bool DbFilePath::deleteDataObject(int id) const
 {
-    QString where = whereClause.isEmpty()
-        ? "" : QString("WHERE ").append(whereClause);
+    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();
+}
 
-    return QString("SELECT filepath.id AS FilePathId, "
+QString DbFilePath::constructSelect(QString where) const
+{
+    return QString("SELECT "
+            "filepath.id AS FilePathId, "
             "filepath.name AS Name, "
-            "filepath.lastscanned AS LastScanned, "
+            "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
             "setup.id AS SetupId, "
-            "platform.name || ' ' || mediatype.name AS SetupName "
+            "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 "
@@ -138,14 +127,20 @@ QString DbFilePath::constructSelect(QString whereClause) const
             "ORDER BY SetupName").arg(where);
 }
 
+QString DbFilePath::constructFilterById(int id) const
+{
+    return QString("filepath.id = %1").arg(id);
+}
+
 QString DbFilePath::constructSelectById(int id) const
 {
-    return constructSelect(QString("filepath.id = %1").arg(id));
+    QString where = QString("WHERE %1").arg(constructFilterById(id));
+    return constructSelect(where);
 }
 
 QSqlQueryModel* DbFilePath::getData()
 {
-    QSqlQueryModel *model = new QSqlQueryModel;
+    QSqlQueryModel *model = new QSqlQueryModel(this);
     model->setQuery(constructSelect());
     model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
     model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
@@ -153,20 +148,13 @@ QSqlQueryModel* DbFilePath::getData()
     model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
     return model;
+}
 
-            //"platform.name, mediatype.name
-   /*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"e));*/
-    /*model->setRelation(FilePath_SetupId,
-        QSqlRelation(DB_TABLE_NAME_SETUP, "id", ""));
-           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;*/
+QString DbFilePath::getCountRefsSelect(int id) const
+{
+    /* 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);
 }