added TODO
[emufront] / src / db / dbfile.cpp
index 7bf64e3..aa0ddb5 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 <QSqlRelationalTableModel>
 #include "dbfile.h"
 
+
 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
 {
+    tableName = DbFile::DB_TABLE_NAME_FILE;
     type = -1;
 }
 
@@ -61,6 +63,7 @@ bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
     return ret;
 }
 
+/* Returns id of inserted data item after succesful insert, -1 if insert failed */
 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
@@ -79,62 +82,26 @@ int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
     return id;
 }
 
-/*int DbFile::insertFile(const EmuFrontFile *mi)
-{
-    qDebug() << "Inserting file " << mi->getName() << " to db.";
-    QSqlQuery q;
-    q.prepare("INSERT INTO file "
-        "(id, name, type, checksum, size, updatetime) "
-        "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
-    q.bindValue(":name", mi->getName());
-    q.bindValue(":type", mi->getType());
-    q.bindValue(":checksum", mi->getCheckSum());
-    q.bindValue(":size", mi->getSize());
-    q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
-    int id = -1;
-    if (q.exec())
-        id = q.lastInsertId().toInt();
-   return id;
-
-}*/
-
-int DbFile::countDataObjectRefs(int id) const
-{
-    return 0; // TODO
-    // return countRows("imagecontainer", "platformid", id);
-}
-
 // WARNING: this will delete also all the databindings to selected platform
 // the delete must be confirmed in the UI
 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
 {
     return false;
-
-    /*QSqlDatabase::database().transaction();*/
-    //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
-    /*QSqlRecord record = tmodel->record(index->row());
-    int id = record.value(File_Id).toInt();
-    int count = countDataObjectRefs(id);
-    if (count > 0)
-    {
-        QSqlQuery query;
-        if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
-        {
-            qDebug() << "Deleting data bindings failed!";
-            QSqlDatabase::database().rollback();
-            return false;
-        }
-    }*/
-    //tmodel->removeRow(index->row());
-    //tmodel->submitAll();
-    //return QSqlDatabase::database().commit();
 }
 
-QString DbFile::constructSelect(QString whereClause) const
+bool DbFile::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 file WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
+}
 
+QString DbFile::constructSelect(QString where) const
+{
     return QString("SELECT file.id AS FileId, "
                    "file.name AS Name, "
                    "file.type AS FileType, "
@@ -153,7 +120,7 @@ QString DbFile::constructFilterById(int id) const
 
 QString DbFile::constructSelectById(int id) const
 {
-    return constructSelect(constructFilterById(id));
+    return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
 }
 
 QSqlQueryModel* DbFile::getData()
@@ -168,7 +135,27 @@ QSqlQueryModel* DbFile::getData()
     return model;
 }
 
+/* Throws EmuFrontException */
 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
 {
     return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
 }
+
+QString DbFile::getCountRefsSelect(int id) const
+{
+    /* files are referenced from platform, mediatype, mediaimagecontainer_mediaimage and mediaimagecontainer. */
+    return QString("SELECT count(*) FROM ("
+              "SELECT file.id FROM file "
+              "INNER JOIN platform ON file.id=platform.fileid "
+              "WHERE file.id=%1 "
+              "UNION ALL "
+              "SELECT file.id FROM file "
+              "INNER JOIN mediatype ON file.id=mediatype.fileid "
+              "WHERE file.id=%1 "
+              "UNION ALL "
+              "SELECT file.id FROM file "
+              "INNER JOIN mediaimagecontainer_mediaimage "
+              "ON (mediaimagecontainerid=file.id OR mediaimageid=file.id) "
+              "WHERE file.id=%1 "
+              ")").arg(id);
+}