//
//
// 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
// 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 <QSqlTableModel>
-#include <QDateTime>
+#include <QSqlRelationalTableModel>
#include "dbfile.h"
-DbFile::DbFile(QObject *parent) : DbTableModelManager(parent)
-{ }
+
+DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
+{
+ tableName = DbFile::DB_TABLE_NAME_FILE;
+ type = -1;
+}
EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
{
{
const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
bool ret = false;
- 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", QDateTime::currentDateTime().toTime_t());
- tmodel->setRecord(0, record);
- ret = tmodel->submitAll();
- }
- resetModel();
- return ret;
-}
-bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
-{
- const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
- 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),
- QDateTime::currentDateTime().toTime_t());
- return tmodel->submitAll();
+ QSqlQuery query;
+ query.prepare(QString("UPDATE file SET"
+ "name=:name, "
+ "type=:type, "
+ "checksum=:checksum, "
+ "size=:size, "
+ "updatetime:=updatetime "
+ "WHERE id=:id"));
+ query.bindValue(":name", plf->getName());
+ query.bindValue(":type", plf->getType());
+ query.bindValue(":checksum", plf->getCheckSum());
+ query.bindValue(":size", plf->getSize());
+ query.bindValue(":updatetime", getCurrentTimeStamp());
+ ret = query.exec();
+ if (ret) resetModel();
+ return ret;
}
-int DbFile::countDataObjectRefs(int id) const
+/* Returns id of inserted data item after succesful insert, -1 if insert failed */
+int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
{
- return 0; // TODO
- // return countRows("imagecontainer", "platformid", id);
+ const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
+ QSqlQuery q;
+ q.prepare("INSERT INTO file "
+ "(id, name, type, checksum, size, updatetime) "
+ "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
+ q.bindValue(":name", fi->getName());
+ q.bindValue(":type", fi->getType());
+ q.bindValue(":checksum", fi->getCheckSum());
+ q.bindValue(":size", fi->getSize());
+ q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
+ int id = -1;
+ if (q.exec())
+ id = q.lastInsertId().toInt();
+ return 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)
{
- /*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();
+ return false;
+}
+
+bool DbFile::deleteDataObject(int id) const
+{
+ 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, "
+ "file.checksum AS Checksum, "
+ "file.size AS FileSize, "
+ "file.updatetime AS UpdateTime "
+ "FROM file "
+ "%1 "
+ "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("WHERE %1").arg(constructFilterById(id)));
}
QSqlQueryModel* DbFile::getData()
{
- QSqlTableModel *model = new QSqlTableModel(this);
- model->setTable(DB_TABLE_NAME_FILE);
- model->setSort(File_Name, Qt::AscendingOrder);
+ QSqlQueryModel *model = new QSqlQueryModel(this);
+ model->setQuery(constructSelect());
model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
- model->select();
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);
+}