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();
62 /*QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
63 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
65 if (tmodel->rowCount() == 1)
67 QSqlRecord record = tmodel->record(0);
68 record.setValue("name", plf->getName());
69 record.setValue("type", plf->getType());
70 record.setValue("checksum", plf->getCheckSum());
71 record.setValue("size", plf->getSize());
72 record.setValue("updatetime", getCurrentTimeStamp());
73 tmodel->setRecord(0, record);
74 ret = tmodel->submitAll();
80 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
82 const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
84 q.prepare("INSERT INTO file "
85 "(id, name, type, checksum, size, updatetime) "
86 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
87 q.bindValue(":name", fi->getName());
88 q.bindValue(":type", fi->getType());
89 q.bindValue(":checksum", fi->getCheckSum());
90 q.bindValue(":size", fi->getSize());
91 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
94 id = q.lastInsertId().toInt();
98 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
99 tmodel->insertRows(row, 1);
100 // the null value for index will be set implicitily
101 // when we don't assign any value to cell 0 in the sql table model
102 //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
103 tmodel->setData(sqlTableModel->index(row, File_Name), plf->getName());
104 tmodel->setData(sqlTableModel->index(row, File_FileType), plf->getType());
105 tmodel->setData(sqlTableModel->index(row, File_CheckSum), plf->getCheckSum());
106 tmodel->setData(sqlTableModel->index(row, File_FileSize), plf->getSize());
107 tmodel->setData(sqlTableModel->index(row, File_UpdateTime), getCurrentTimeStamp());
108 return tmodel->submitAll();*/
111 /*int DbFile::insertFile(const EmuFrontFile *mi)
113 qDebug() << "Inserting file " << mi->getName() << " to db.";
115 q.prepare("INSERT INTO file "
116 "(id, name, type, checksum, size, updatetime) "
117 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
118 q.bindValue(":name", mi->getName());
119 q.bindValue(":type", mi->getType());
120 q.bindValue(":checksum", mi->getCheckSum());
121 q.bindValue(":size", mi->getSize());
122 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
125 id = q.lastInsertId().toInt();
130 int DbFile::countDataObjectRefs(int id) const
133 // return countRows("imagecontainer", "platformid", id);
136 // WARNING: this will delete also all the databindings to selected platform
137 // the delete must be confirmed in the UI
138 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
142 /*QSqlDatabase::database().transaction();*/
143 //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
144 /*QSqlRecord record = tmodel->record(index->row());
145 int id = record.value(File_Id).toInt();
146 int count = countDataObjectRefs(id);
150 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
152 qDebug() << "Deleting data bindings failed!";
153 QSqlDatabase::database().rollback();
157 //tmodel->removeRow(index->row());
158 //tmodel->submitAll();
159 //return QSqlDatabase::database().commit();
162 QString DbFile::constructSelect(QString whereClause) const
164 QString where = whereClause.isEmpty()
165 ? "" : QString("WHERE ").append(whereClause);
167 return QString("SELECT file.id AS FileId, "
168 "file.name AS Name, "
169 "file.type AS FileType, "
170 "file.checksum AS Checksum, "
171 "file.size AS FileSize, "
172 "file.updatetime AS UpdateTime "
175 "ORDER BY Name").arg(where);
178 QString DbFile::constructSelectById(int id) const
180 return constructSelect(QString("file.id = %1").arg(id));
183 QSqlQueryModel* DbFile::getData()
185 QSqlQueryModel *model = new QSqlQueryModel(this);
186 model->setQuery(constructSelect());
187 model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
188 model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
189 model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
190 model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
191 model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
195 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
197 return getDataObject(QString("checksum LIKE '%1'").arg(checksum));