Added skeletal implementation of ExternalExecutableModel.
[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 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
73 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
74 {
75     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
76     QSqlQuery query;
77     query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
78         "VALUES (NULL, :name, :fileid) ").arg(tableName));
79     query.bindValue(":name", plf->getName());
80     if (plf->getFile())
81         query.bindValue(":fileid", plf->getFile()->getId());
82     else query.bindValue(":fileid", "NULL");
83     int id = -1;
84     if (query.exec())
85         id = query.lastInsertId().toInt();
86     else
87         qDebug() << "Failed inserting to " << tableName << " "
88             << query.lastError().text() << " " << query.executedQuery() ;
89     return id;
90 }
91
92 // WARNING: this will delete also all the databindings to selected platform
93 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
94 {
95     // TODO
96     return false;
97 }
98
99 QString DbEmuFrontFileObject::constructSelect(QString where) const
100 {
101     return QString("SELECT maintbl.id AS FileObjectId, "
102             "maintbl.name AS Name, "
103             "file.id AS FileId, "
104             "file.name AS FileName, "
105             "file.type AS FileType, "
106             "file.checksum AS FileChecksum, "
107             "file.size AS FileSize, "
108             "file.updatetime AS FileUpdateTime "
109             "FROM %1 AS maintbl "
110             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
111             "%2 "
112             "ORDER BY Name").arg(tableName).arg(where);
113 }
114
115 QString DbEmuFrontFileObject::constructSelectById(int id) const
116 {
117     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
118 }
119
120 QString DbEmuFrontFileObject::constructFilterById(int id) const
121 {
122     return QString("maintbl.id = %1").arg(id);
123 }
124
125 QSqlQueryModel* DbEmuFrontFileObject::getData()
126 {
127     QSqlQueryModel *model = new QSqlQueryModel(this);
128     model->setQuery(constructSelect());
129     model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
130     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
131     return model;
132 }
133
134 QString DbEmuFrontFileObject::getDeleteObjectSql() const
135 {
136     return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);
137 }