120446b17ad2ddbae48023b66b9ee12a12f49215
[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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
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 // WARNING: this will delete also all the databindings to selected platform
91 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
92 {
93     // TODO
94     return false;
95 }
96
97 QString DbEmuFrontFileObject::constructSelect(QString where) const
98 {
99     return QString("SELECT maintbl.id AS FileObjectId, "
100             "maintbl.name AS Name, "
101             "file.id AS FileId, "
102             "file.name AS FileName, "
103             "file.type AS FileType, "
104             "file.checksum AS FileChecksum, "
105             "file.size AS FileSize, "
106             "file.updatetime AS FileUpdateTime "
107             "FROM %1 AS maintbl "
108             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
109             "%2 "
110             "ORDER BY Name").arg(tableName).arg(where);
111 }
112
113 QString DbEmuFrontFileObject::constructSelectById(int id) const
114 {
115     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
116 }
117
118 QString DbEmuFrontFileObject::constructFilterById(int id) const
119 {
120     return QString("maintbl.id = %1").arg(id);
121 }
122
123 QSqlQueryModel* DbEmuFrontFileObject::getData()
124 {
125     QSqlQueryModel *model = new QSqlQueryModel(this);
126     model->setQuery(constructSelect());
127     model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
128     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
129     return model;
130 }
131
132 QString DbEmuFrontFileObject::getDeleteObjectSql() const
133 {
134     return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);
135 }