First unit test implemented!
[emufront] / src / db / dbfilepath.cpp
index 021b121..b75ef52 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 "dbfilepath.h"
 #include "dbsetup.h"
 
+
 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
+    tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
     dbSetup = new DbSetup(this);
 }
 
+/* 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, /* TODO */ 0, sup);
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
@@ -61,7 +64,19 @@ bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
     return ret;
 }
 
-bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
+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;
+}
+
+/* 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;
@@ -72,12 +87,10 @@ bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
     if (fpo->getSetup())
         query.bindValue(":setupid", fpo->getSetup()->getId());
     query.bindValue(":lastscanned", 0); // TODO
-    return query.exec();
-}
-
-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
@@ -86,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 "
@@ -104,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"));
@@ -120,3 +149,12 @@ QSqlQueryModel* DbFilePath::getData()
     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
     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);
+}