2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
23 #include <QSqlRelationalTableModel>
27 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
29 tableName = DbFile::DB_TABLE_NAME_FILE;
33 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
35 int id = record->value(File_Id).toInt();
36 QString name = record->value(File_Name).toString();
37 QString checksum = record->value(File_CheckSum).toString();
38 int size = record->value(File_FileSize).toInt();
39 int type = record->value(File_FileType).toInt();
40 return new EmuFrontFile(id, name, checksum, size, type);
43 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
45 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
49 query.prepare(QString("UPDATE file SET"
52 "checksum=:checksum, "
54 "updatetime:=updatetime "
56 query.bindValue(":name", plf->getName());
57 query.bindValue(":type", plf->getType());
58 query.bindValue(":checksum", plf->getCheckSum());
59 query.bindValue(":size", plf->getSize());
60 query.bindValue(":updatetime", getCurrentTimeStamp());
62 if (ret) resetModel();
66 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
67 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
69 const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
71 q.prepare("INSERT INTO file "
72 "(id, name, type, checksum, size, updatetime) "
73 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
74 q.bindValue(":name", fi->getName());
75 q.bindValue(":type", fi->getType());
76 q.bindValue(":checksum", fi->getCheckSum());
77 q.bindValue(":size", fi->getSize());
78 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
81 id = q.lastInsertId().toInt();
85 // WARNING: this will delete also all the databindings to selected platform
86 // the delete must be confirmed in the UI
87 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
92 bool DbFile::deleteDataObject(int id) const
94 if (countDataObjectRefs(id) > 0)
98 q.prepare(QString("DELETE FROM file WHERE id=:id"));
99 q.bindValue(":id", id);
103 QString DbFile::constructSelect(QString where) const
105 return QString("SELECT file.id AS FileId, "
106 "file.name AS Name, "
107 "file.type AS FileType, "
108 "file.checksum AS Checksum, "
109 "file.size AS FileSize, "
110 "file.updatetime AS UpdateTime "
113 "ORDER BY Name").arg(where);
116 QString DbFile::constructFilterById(int id) const
118 return QString("file.id = %1").arg(id);
121 QString DbFile::constructSelectById(int id) const
123 return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
126 QSqlQueryModel* DbFile::getData()
128 QSqlQueryModel *model = new QSqlQueryModel(this);
129 model->setQuery(constructSelect());
130 model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
131 model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
132 model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
133 model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
134 model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
138 /* Throws EmuFrontException */
139 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
141 return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
144 QString DbFile::getCountRefsSelect(int id) const
146 /* files are referenced from platform, mediatype, mediaimagecontainer_mediaimage and mediaimagecontainer. */
147 return QString("SELECT count(*) FROM ("
148 "SELECT file.id FROM file "
149 "INNER JOIN platform ON file.id=platform.fileid "
152 "SELECT file.id FROM file "
153 "INNER JOIN mediatype ON file.id=mediatype.fileid "
156 "SELECT file.id FROM file "
157 "INNER JOIN mediaimagecontainer_mediaimage "
158 "ON (mediaimagecontainerid=file.id OR mediaimageid=file.id) "