-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// 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.
-//
-// 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 EmuFront. If not, see <http://www.gnu.org/licenses/>.
-
+/*
+** EmuFront
+** Copyright 2010 Mikko Keinänen
+**
+** This file is part of EmuFront.
+**
+**
+** EmuFront is free software: you can redistribute it and/or modify
+** 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
+** 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 EmuFront. If not, see <http://www.gnu.org/licenses/>.
+*/
#include <QDebug>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlRelationalTableModel>
#include "dbfile.h"
+
DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
{
+ tableName = DbFile::DB_TABLE_NAME_FILE;
type = -1;
}
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);
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();
}
bool DbFile::deleteDataObject(int id) const
return q.exec();
}
-QString DbFile::constructSelect(QString whereClause) const
+QString DbFile::constructSelect(QString where) const
{
- QString where = whereClause.isEmpty()
- ? "" : QString("WHERE ").append(whereClause);
-
return QString("SELECT file.id AS FileId, "
"file.name AS Name, "
"file.type AS FileType, "
QString DbFile::constructSelectById(int id) const
{
- return constructSelect(constructFilterById(id));
+ return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
}
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);
+}