Fixed error in sql.
[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     if (ret) resetModel();
64     else
65             qDebug() << "Failed updating " << tableName
66                 << " " <<   query.lastError().text()
67                 << " " << query.executedQuery() ;
68     return ret;
69 }
70
71 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
72 {
73     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
74     QSqlQuery query;
75     query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
76         "VALUES (NULL, :name, :fileid) ").arg(tableName));
77     query.bindValue(":name", plf->getName());
78     if (plf->getFile())
79         query.bindValue(":fileid", plf->getFile()->getId());
80     else query.bindValue(":fileid", "NULL");
81     int id = -1;
82     if (query.exec())
83         id = query.lastInsertId().toInt();
84     else
85         qDebug() << "Failed inserting to " << tableName << " "
86             << query.lastError().text() << " " << query.executedQuery() ;
87     return id;
88 }
89
90 bool DbEmuFrontFileObject::deleteDataObject(int id) const
91 {
92     if (countDataObjectRefs(id) > 0)
93         // TODO
94         return false;
95     QSqlQuery q;
96     q.prepare(QString("DELETE FROM %1 WHERE id = :id").arg(tableName));
97     q.bindValue(":id", id);
98     return q.exec();
99
100 }
101
102 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
103 {
104     return 0; // TODO
105     // return countRows("imagecontainer", "platformid", id);
106 }
107
108 // WARNING: this will delete also all the databindings to selected platform
109 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
110 {
111     return false;
112     //QSqlDatabase::database().transaction();
113     //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
114     /*QSqlRecord record = tmodel->record(index->row());
115     int id = record.value(EmuFrontFileObject_Id).toInt();
116     qDebug() << "Deleting platform " << id;
117     int count = countDataObjectRefs(id);
118     if (count > 0)
119     {
120         QSqlQuery query;
121         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
122         {
123             qDebug() << "Deleting data bindings failed!";
124             QSqlDatabase::database().rollback();
125             return false;
126         }
127     }*/
128     /*tmodel->removeRow(index->row());
129     tmodel->submitAll();
130     return QSqlDatabase::database().commit();
131     */
132 }
133
134 QString DbEmuFrontFileObject::constructSelect(QString whereClause) const
135 {
136     QString where = whereClause.isEmpty()
137         ? "" : QString("WHERE ").append(whereClause);
138
139     return QString("SELECT maintbl.id AS FileObjectId, "
140             "maintbl.name AS Name, "
141             "file.id AS FileId, "
142             "file.name AS FileName, "
143             "file.type AS FileType, "
144             "file.checksum AS FileChecksum, "
145             "file.size AS FileSize, "
146             "file.updatetime AS FileUpdateTime "
147             "FROM %1 AS maintbl "
148             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
149             "%2 "
150             "ORDER BY Name").arg(tableName).arg(where);
151 }
152
153 QString DbEmuFrontFileObject::constructSelectById(int id) const
154 {
155     return constructSelect(constructFilterById(id));
156 }
157
158 QString DbEmuFrontFileObject::constructFilterById(int id) const
159 {
160     return QString("maintbl.id = %1").arg(id);
161 }
162
163 QSqlQueryModel* DbEmuFrontFileObject::getData()
164 {
165     QSqlQueryModel *model = new QSqlQueryModel;
166     model->setQuery(constructSelect());
167     model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
168     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
169     /*model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("File id"));
170     model->setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File name"));
171     model->setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File type"));
172     model->setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File checksum"));
173     model->setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File size"));
174     model->setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File update time"));*/
175     return model;
176 }