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>
25 #include "dbemufrontfileobject.h"
27 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
28 : DbQueryModelManager(parent)
30 dbFile = new DbFile(this);
33 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
35 int id = record->value(EmuFrontFileObject_Id).toInt();
36 QString name = record->value(EmuFrontFileObject_Name).toString();
37 int fileId = record->value(EmuFrontFileObject_FileId).toInt();
41 EmuFrontObject *o = dbFile->getDataObject(fileId);
42 f = dynamic_cast<EmuFrontFile*>(o);
44 EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
48 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
50 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
53 query.prepare(QString("UPDATE %1 SET "
56 "WHERE id=:id").arg(tableName));
57 query.bindValue(":name", plf->getName());
58 query.bindValue(":fileid",
59 plf->getFile() ? QString(plf->getFile()->getId()) : "NULL");
60 query.bindValue(":id", plf->getId());
64 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
65 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
67 if (tmodel->rowCount() == 1)
69 QSqlRecord record = tmodel->record(0);
70 record.setValue("name", plf->getName());
72 record.setValue("fileid", plf->getFile()->getId());
73 else record.setNull("fileid");
74 tmodel->setRecord(0, record);
75 ret = tmodel->submitAll();
77 if (ret) resetModel();
79 qDebug() << "Failed updating " << tableName
80 << " " << query.lastError().text()
81 << " " << query.executedQuery() ;
85 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
87 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
89 query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
90 "VALUES (NULL, :name, :fileid) ").arg(tableName));
91 query.bindValue(":name", plf->getName());
93 query.bindValue(":fileid", plf->getFile()->getId());
94 else query.bindValue(":fileid", "NULL");
97 id = query.lastInsertId().toInt();
99 qDebug() << "Failed inserting to " << tableName << " "
100 << query.lastError().text() << " " << query.executedQuery() ;
104 if (!sqlTableModel) sqlTableModel = getDataModel();
105 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
106 tmodel->insertRows(row, 1);
107 // the null value for index will be set implicitily
108 // when we don't assign any value to cell 0 in the sql table model
109 //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
110 tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_Name), plf->getName());
112 tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_FileId), plf->getFile()->getId());
113 if (tmodel->submitAll())
114 return ... // TODO: update to use dbquerymodelmanager instead of tablemodelmanager
115 // TODO: and return the last insert id
119 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
122 // return countRows("imagecontainer", "platformid", id);
125 // WARNING: this will delete also all the databindings to selected platform
126 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
129 //QSqlDatabase::database().transaction();
130 //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
131 /*QSqlRecord record = tmodel->record(index->row());
132 int id = record.value(EmuFrontFileObject_Id).toInt();
133 qDebug() << "Deleting platform " << id;
134 int count = countDataObjectRefs(id);
138 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
140 qDebug() << "Deleting data bindings failed!";
141 QSqlDatabase::database().rollback();
145 /*tmodel->removeRow(index->row());
147 return QSqlDatabase::database().commit();
151 QString DbEmuFrontFileObject::constructSelect(QString whereClause) const
153 QString where = whereClause.isEmpty()
154 ? "" : QString("WHERE ").append(whereClause);
156 return QString("SELECT maintbl.id AS FileObjectId, "
157 "maintbl.name AS Name, "
158 "file.id AS FileId, "
159 "file.name AS FileName, "
160 "file.type AS FileType, "
161 "file.checksum AS FileChecksum, "
162 "file.size AS FileSize, "
163 "file.updatetime AS FileUpdateTime "
164 "FROM %1 AS maintbl "
165 "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
167 "ORDER BY Name").arg(tableName).arg(where);
170 QString DbEmuFrontFileObject::constructSelectById(int id) const
172 return constructSelect(constructFilterById(id));
175 QString DbEmuFrontFileObject::constructFilterById(int id) const
177 return QString("maintbl.id = %1").arg(id);
180 QSqlQueryModel* DbEmuFrontFileObject::getData()
182 /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
183 model->setTable(tableName);
184 // TODO: table realtion model seems not to be suitable for this
185 // since not always does data object have a file relation:
186 //model->setRelation(EmuFrontFileObject_FileId, QSqlRelation("file", "id", "name"));
187 model->setSort(EmuFrontFileObject_Name, Qt::AscendingOrder);
188 model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
189 model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("Icon"));
191 QSqlQueryModel *model = new QSqlQueryModel;
192 model->setQuery(constructSelect());
193 model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
194 model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
195 /*model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("File id"));
196 model->setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File name"));
197 model->setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File type"));
198 model->setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File checksum"));
199 model->setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File size"));
200 model->setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File update time"));*/