DbEmuFrontFileObject is now a DbQueryModelManager instead of
[emufront] / src / db / dbemufrontfileobject.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QSqlRecord>
21 #include <QSqlQuery>
22 #include <QSqlError>
23 #include <QSqlRelationalTableModel>
24 #include <QDebug>
25 #include "dbemufrontfileobject.h"
26
27 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
28     : DbQueryModelManager(parent)
29 {
30     dbFile = new DbFile(this);
31 }
32
33 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
34 {
35     int id = record->value(EmuFrontFileObject_Id).toInt();
36     QString name = record->value(EmuFrontFileObject_Name).toString();
37     int fileId = record->value(EmuFrontFileObject_FileId).toInt();
38     EmuFrontFile *f = 0;
39     if (fileId > 0)
40     {
41         EmuFrontObject *o = dbFile->getDataObject(fileId);
42         f = dynamic_cast<EmuFrontFile*>(o);
43     }
44     EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
45     return efo;
46 }
47
48 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
49 {
50     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
51     bool ret = false;
52     QSqlQuery query;
53     query.prepare(QString("UPDATE %1 SET "
54         "name=:name, "
55         "fileid=:fileid "
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());
61     ret = query.exec();
62
63     /*
64     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
65     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
66     tmodel->select();
67     if (tmodel->rowCount() == 1)
68     {
69         QSqlRecord record = tmodel->record(0);
70         record.setValue("name", plf->getName());
71         if (plf->getFile())
72             record.setValue("fileid", plf->getFile()->getId());
73         else record.setNull("fileid");
74         tmodel->setRecord(0, record);
75         ret = tmodel->submitAll();
76     }*/
77     if (ret) resetModel();
78     else
79             qDebug() << "Failed updating " << tableName
80                 << " " <<   query.lastError().text()
81                 << " " << query.executedQuery() ;
82     return ret;
83 }
84
85 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
86 {
87     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
88     QSqlQuery query;
89     query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
90         "VALUES (NULL, :name, :fileid) ").arg(tableName));
91     query.bindValue(":name", plf->getName());
92     if (plf->getFile())
93         query.bindValue(":fileid", plf->getFile()->getId());
94     else query.bindValue(":fileid", "NULL");
95     int id = -1;
96     if (query.exec())
97         id = query.lastInsertId().toInt();
98     else
99         qDebug() << "Failed inserting to " << tableName << " "
100             << query.lastError().text() << " " << query.executedQuery() ;
101     return id;
102
103     /*int row = 0;
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());
111     if (plf->getFile())
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
116     */
117 }
118
119 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
120 {
121     return 0; // TODO
122     // return countRows("imagecontainer", "platformid", id);
123 }
124
125 // WARNING: this will delete also all the databindings to selected platform
126 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
127 {
128     return false;
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);
135     if (count > 0)
136     {
137         QSqlQuery query;
138         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
139         {
140             qDebug() << "Deleting data bindings failed!";
141             QSqlDatabase::database().rollback();
142             return false;
143         }
144     }*/
145     /*tmodel->removeRow(index->row());
146     tmodel->submitAll();
147     return QSqlDatabase::database().commit();
148     */
149 }
150
151 QString DbEmuFrontFileObject::constructSelect(QString whereClause) const
152 {
153     QString where = whereClause.isEmpty()
154         ? "" : QString("WHERE ").append(whereClause);
155
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 "
166             "%2 "
167             "ORDER BY Name").arg(tableName).arg(where);
168 }
169
170 QString DbEmuFrontFileObject::constructSelectById(int id) const
171 {
172     return constructSelect(constructFilterById(id));
173 }
174
175 QString DbEmuFrontFileObject::constructFilterById(int id) const
176 {
177     return QString("maintbl.id = %1").arg(id);
178 }
179
180 QSqlQueryModel* DbEmuFrontFileObject::getData()
181 {
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"));
190     model->select();*/
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"));*/
201     return model;
202 }