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>
28 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
30 tableName = DbFile::DB_TABLE_NAME_FILE;
34 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
36 int id = record->value(File_Id).toInt();
37 QString name = record->value(File_Name).toString();
38 QString checksum = record->value(File_CheckSum).toString();
39 int size = record->value(File_FileSize).toInt();
40 int type = record->value(File_FileType).toInt();
41 return new EmuFrontFile(id, name, checksum, size, type);
44 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
46 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
50 query.prepare(QString("UPDATE file SET"
53 "checksum=:checksum, "
55 "updatetime:=updatetime "
57 query.bindValue(":name", plf->getName());
58 query.bindValue(":type", plf->getType());
59 query.bindValue(":checksum", plf->getCheckSum());
60 query.bindValue(":size", plf->getSize());
61 query.bindValue(":updatetime", getCurrentTimeStamp());
63 if (ret) resetModel();
67 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
68 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
70 const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
72 q.prepare("INSERT INTO file "
73 "(id, name, type, checksum, size, updatetime) "
74 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
75 q.bindValue(":name", fi->getName());
76 q.bindValue(":type", fi->getType());
77 q.bindValue(":checksum", fi->getCheckSum());
78 q.bindValue(":size", fi->getSize());
79 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
82 id = q.lastInsertId().toInt();
86 // WARNING: this will delete also all the databindings to selected platform
87 // the delete must be confirmed in the UI
88 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
93 bool DbFile::deleteDataObject(int id) const
95 if (countDataObjectRefs(id) > 0)
99 q.prepare(QString("DELETE FROM file WHERE id=:id"));
100 q.bindValue(":id", id);
104 QString DbFile::constructSelect(QString where) const
106 return QString("SELECT file.id AS FileId, "
107 "file.name AS Name, "
108 "file.type AS FileType, "
109 "file.checksum AS Checksum, "
110 "file.size AS FileSize, "
111 "file.updatetime AS UpdateTime "
114 "ORDER BY Name").arg(where);
117 QString DbFile::constructFilterById(int id) const
119 return QString("file.id = %1").arg(id);
122 QString DbFile::constructSelectById(int id) const
124 return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
127 QSqlQueryModel* DbFile::getData()
129 QSqlQueryModel *model = new QSqlQueryModel(this);
130 model->setQuery(constructSelect());
131 model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
132 model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
133 model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
134 model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
135 model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
139 /* Throws EmuFrontException */
140 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
142 return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
145 QString DbFile::getCountRefsSelect(int id) const
147 /* files are referenced from platform, mediatype, mediaimagecontainer_mediaimage and mediaimagecontainer. */
148 return QString("SELECT count(*) FROM ("
149 "SELECT file.id FROM file "
150 "INNER JOIN platform ON file.id=platform.fileid "
153 "SELECT file.id FROM file "
154 "INNER JOIN mediatype ON file.id=mediatype.fileid "
157 "SELECT file.id FROM file "
158 "INNER JOIN mediaimagecontainer_mediaimage "
159 "ON (mediaimagecontainerid=file.id OR mediaimageid=file.id) "