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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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>
26 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
31 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
33 int id = record->value(File_Id).toInt();
34 QString name = record->value(File_Name).toString();
35 QString checksum = record->value(File_CheckSum).toString();
36 int size = record->value(File_FileSize).toInt();
37 int type = record->value(File_FileType).toInt();
38 return new EmuFrontFile(id, name, checksum, size, type);
41 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
43 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
47 query.prepare(QString("UPDATE file SET"
50 "checksum=:checksum, "
52 "updatetime:=updatetime "
54 query.bindValue(":name", plf->getName());
55 query.bindValue(":type", plf->getType());
56 query.bindValue(":checksum", plf->getCheckSum());
57 query.bindValue(":size", plf->getSize());
58 query.bindValue(":updatetime", getCurrentTimeStamp());
60 if (ret) resetModel();
64 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
66 const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
68 q.prepare("INSERT INTO file "
69 "(id, name, type, checksum, size, updatetime) "
70 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
71 q.bindValue(":name", fi->getName());
72 q.bindValue(":type", fi->getType());
73 q.bindValue(":checksum", fi->getCheckSum());
74 q.bindValue(":size", fi->getSize());
75 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
78 id = q.lastInsertId().toInt();
82 /*int DbFile::insertFile(const EmuFrontFile *mi)
84 qDebug() << "Inserting file " << mi->getName() << " to db.";
86 q.prepare("INSERT INTO file "
87 "(id, name, type, checksum, size, updatetime) "
88 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
89 q.bindValue(":name", mi->getName());
90 q.bindValue(":type", mi->getType());
91 q.bindValue(":checksum", mi->getCheckSum());
92 q.bindValue(":size", mi->getSize());
93 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
96 id = q.lastInsertId().toInt();
101 int DbFile::countDataObjectRefs(int id) const
104 // return countRows("imagecontainer", "platformid", id);
107 // WARNING: this will delete also all the databindings to selected platform
108 // the delete must be confirmed in the UI
109 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
113 /*QSqlDatabase::database().transaction();*/
114 //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
115 /*QSqlRecord record = tmodel->record(index->row());
116 int id = record.value(File_Id).toInt();
117 int count = countDataObjectRefs(id);
121 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
123 qDebug() << "Deleting data bindings failed!";
124 QSqlDatabase::database().rollback();
128 //tmodel->removeRow(index->row());
129 //tmodel->submitAll();
130 //return QSqlDatabase::database().commit();
133 bool DbFile::deleteDataObject(int id) const
135 if (countDataObjectRefs(id) > 0)
139 q.prepare(QString("DELETE FROM file WHERE id=:id"));
140 q.bindValue(":id", id);
144 QString DbFile::constructSelect(QString where) const
146 /*QString where = whereClause.isEmpty()
147 ? "" : QString("WHERE ").append(whereClause);*/
149 return QString("SELECT file.id AS FileId, "
150 "file.name AS Name, "
151 "file.type AS FileType, "
152 "file.checksum AS Checksum, "
153 "file.size AS FileSize, "
154 "file.updatetime AS UpdateTime "
157 "ORDER BY Name").arg(where);
160 QString DbFile::constructFilterById(int id) const
162 return QString("file.id = %1").arg(id);
165 QString DbFile::constructSelectById(int id) const
167 return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
170 QSqlQueryModel* DbFile::getData()
172 QSqlQueryModel *model = new QSqlQueryModel(this);
173 model->setQuery(constructSelect());
174 model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
175 model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
176 model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
177 model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
178 model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
182 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
184 return getDataObject(QString("checksum LIKE '%1'").arg(checksum));