3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
24 #include <QSqlRelationalTableModel>
26 #include "dbemufrontfileobject.h"
28 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
29 : DbQueryModelManager(parent)
31 dbFile = new DbFile(this);
34 /* Throws EmuFrontException */
35 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
37 int id = record->value(EmuFrontFileObject_Id).toInt();
38 QString name = record->value(EmuFrontFileObject_Name).toString();
39 int fileId = record->value(EmuFrontFileObject_FileId).toInt();
43 EmuFrontObject *o = dbFile->getDataObject(fileId); /* Throws EmuFrontException */
44 f = dynamic_cast<EmuFrontFile*>(o);
46 EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
50 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
52 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
55 query.prepare(QString("UPDATE %1 SET "
58 "WHERE id=:id").arg(tableName));
59 query.bindValue(":name", plf->getName());
60 query.bindValue(":fileid",
61 plf->getFile() ? QString(plf->getFile()->getId()) : "NULL");
62 query.bindValue(":id", plf->getId());
65 if (ret) resetModel();
67 qDebug() << "Failed updating " << tableName
68 << " " << query.lastError().text()
69 << " " << query.executedQuery() ;
73 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
74 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
76 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
78 query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
79 "VALUES (NULL, :name, :fileid) ").arg(tableName));
80 query.bindValue(":name", plf->getName());
82 query.bindValue(":fileid", plf->getFile()->getId());
83 else query.bindValue(":fileid", "NULL");
86 id = query.lastInsertId().toInt();
88 qDebug() << "Failed inserting to " << tableName << " "
89 << query.lastError().text() << " " << query.executedQuery() ;
93 // WARNING: this will delete also all the databindings to selected platform
94 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
100 QString DbEmuFrontFileObject::constructSelect(QString where) const
102 return QString("SELECT maintbl.id AS FileObjectId, "
103 "maintbl.name AS Name, "
104 "file.id AS FileId, "
105 "file.name AS FileName, "
106 "file.type AS FileType, "
107 "file.checksum AS FileChecksum, "
108 "file.size AS FileSize, "
109 "file.updatetime AS FileUpdateTime "
110 "FROM %1 AS maintbl "
111 "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
113 "ORDER BY Name").arg(tableName).arg(where);
116 QString DbEmuFrontFileObject::constructSelectById(int id) const
118 return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
121 QString DbEmuFrontFileObject::constructFilterById(int id) const
123 return QString("maintbl.id = %1").arg(id);
126 QSqlQueryModel* DbEmuFrontFileObject::getData()
128 QSqlQueryModel *model = new QSqlQueryModel(this);
129 model->setQuery(constructSelect());
130 model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
131 model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
135 QString DbEmuFrontFileObject::getDeleteObjectSql() const
137 return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);