-// 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;
}
query.bindValue(":updatetime", getCurrentTimeStamp());
ret = query.exec();
if (ret) resetModel();
-
- /*QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
- tmodel->setFilter(QString("id = %1").arg(plf->getId()));
- tmodel->select();
- if (tmodel->rowCount() == 1)
- {
- QSqlRecord record = tmodel->record(0);
- record.setValue("name", plf->getName());
- record.setValue("type", plf->getType());
- record.setValue("checksum", plf->getCheckSum());
- record.setValue("size", plf->getSize());
- record.setValue("updatetime", getCurrentTimeStamp());
- tmodel->setRecord(0, record);
- ret = tmodel->submitAll();
- }
- resetModel();*/
return ret;
}
-bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
+/* 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);
QSqlQuery q;
if (q.exec())
id = q.lastInsertId().toInt();
return id;
-
- /*int row = 0;
- QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
- tmodel->insertRows(row, 1);
- // the null value for index will be set implicitily
- // when we don't assign any value to cell 0 in the sql table model
- //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
- tmodel->setData(sqlTableModel->index(row, File_Name), plf->getName());
- tmodel->setData(sqlTableModel->index(row, File_FileType), plf->getType());
- tmodel->setData(sqlTableModel->index(row, File_CheckSum), plf->getCheckSum());
- tmodel->setData(sqlTableModel->index(row, File_FileSize), plf->getSize());
- tmodel->setData(sqlTableModel->index(row, File_UpdateTime), getCurrentTimeStamp());
- return tmodel->submitAll();*/
-}
-
-/*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
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, "
"ORDER BY Name").arg(where);
}
+QString DbFile::constructFilterById(int id) const
+{
+ return QString("file.id = %1").arg(id);
+}
+
QString DbFile::constructSelectById(int id) const
{
- return constructSelect(QString("file.id = %1").arg(id));
+ return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
}
QSqlQueryModel* DbFile::getData()
return model;
}
+/* Throws EmuFrontException */
EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
{
- filterDataObjects(QString("checksum LIKE '%1'").arg(checksum));
- return getDataObject();
+ 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);
}