5992d68c5350d5c466571828e6e5f9451137ee11
[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 /* Throws EmuFrontException */
34 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
35 {
36     int id = record->value(EmuFrontFileObject_Id).toInt();
37     QString name = record->value(EmuFrontFileObject_Name).toString();
38     int fileId = record->value(EmuFrontFileObject_FileId).toInt();
39     EmuFrontFile *f = 0;
40     if (fileId > 0)
41     {
42         EmuFrontObject *o = dbFile->getDataObject(fileId); /* Throws EmuFrontException */
43         f = dynamic_cast<EmuFrontFile*>(o);
44     }
45     EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
46     return efo;
47 }
48
49 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
50 {
51     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
52     bool ret = false;
53     QSqlQuery query;
54     query.prepare(QString("UPDATE %1 SET "
55         "name=:name, "
56         "fileid=:fileid "
57         "WHERE id=:id").arg(tableName));
58     query.bindValue(":name", plf->getName());
59     query.bindValue(":fileid",
60                     plf->getFile() ? QString(plf->getFile()->getId()) : "NULL");
61     query.bindValue(":id", plf->getId());
62     ret = query.exec();
63
64     if (ret) resetModel();
65     else
66             qDebug() << "Failed updating " << tableName
67                 << " " <<   query.lastError().text()
68                 << " " << query.executedQuery() ;
69     return ret;
70 }
71
72 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
73 {
74     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
75     QSqlQuery query;
76     query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
77         "VALUES (NULL, :name, :fileid) ").arg(tableName));
78     query.bindValue(":name", plf->getName());
79     if (plf->getFile())
80         query.bindValue(":fileid", plf->getFile()->getId());
81     else query.bindValue(":fileid", "NULL");
82     int id = -1;
83     if (query.exec())
84         id = query.lastInsertId().toInt();
85     else
86         qDebug() << "Failed inserting to " << tableName << " "
87             << query.lastError().text() << " " << query.executedQuery() ;
88     return id;
89 }
90
91 // WARNING: this will delete also all the databindings to selected platform
92 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
93 {
94     // TODO
95     return false;
96 }
97
98 QString DbEmuFrontFileObject::constructSelect(QString where) const
99 {
100     return QString("SELECT maintbl.id AS FileObjectId, "
101             "maintbl.name AS Name, "
102             "file.id AS FileId, "
103             "file.name AS FileName, "
104             "file.type AS FileType, "
105             "file.checksum AS FileChecksum, "
106             "file.size AS FileSize, "
107             "file.updatetime AS FileUpdateTime "
108             "FROM %1 AS maintbl "
109             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
110             "%2 "
111             "ORDER BY Name").arg(tableName).arg(where);
112 }
113
114 QString DbEmuFrontFileObject::constructSelectById(int id) const
115 {
116     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
117 }
118
119 QString DbEmuFrontFileObject::constructFilterById(int id) const
120 {
121     return QString("maintbl.id = %1").arg(id);
122 }
123
124 QSqlQueryModel* DbEmuFrontFileObject::getData()
125 {
126     QSqlQueryModel *model = new QSqlQueryModel(this);
127     model->setQuery(constructSelect());
128     model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
129     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
130     return model;
131 }
132
133 QString DbEmuFrontFileObject::getDeleteObjectSql() const
134 {
135     return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);
136 }