Modified the license text comment type.
[emufront] / src / db / dbemufrontfileobject.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlError>
24 #include <QSqlRelationalTableModel>
25 #include <QDebug>
26 #include "dbemufrontfileobject.h"
27
28 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
29     : DbQueryModelManager(parent)
30 {
31     dbFile = new DbFile(this);
32 }
33
34 /* Throws EmuFrontException */
35 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
36 {
37     int id = record->value(EmuFrontFileObject_Id).toInt();
38     QString name = record->value(EmuFrontFileObject_Name).toString();
39     int fileId = record->value(EmuFrontFileObject_FileId).toInt();
40     EmuFrontFile *f = 0;
41     if (fileId > 0)
42     {
43         EmuFrontObject *o = dbFile->getDataObject(fileId); /* Throws EmuFrontException */
44         f = dynamic_cast<EmuFrontFile*>(o);
45     }
46     EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
47     return efo;
48 }
49
50 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
51 {
52     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
53     bool ret = false;
54     QSqlQuery query;
55     query.prepare(QString("UPDATE %1 SET "
56         "name=:name, "
57         "fileid=:fileid "
58         "WHERE id=:id").arg(tableName));
59     query.bindValue(":name", plf->getName());
60     query.bindValue(":fileid",
61                     plf->getFile() ? QString(plf->getFile()->getId()) : "NULL");
62     query.bindValue(":id", plf->getId());
63     ret = query.exec();
64
65     if (ret) resetModel();
66     else
67             qDebug() << "Failed updating " << tableName
68                 << " " <<   query.lastError().text()
69                 << " " << query.executedQuery() ;
70     return ret;
71 }
72
73 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
74 int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
75 {
76     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
77     QSqlQuery query;
78     query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
79         "VALUES (NULL, :name, :fileid) ").arg(tableName));
80     query.bindValue(":name", plf->getName());
81     if (plf->getFile())
82         query.bindValue(":fileid", plf->getFile()->getId());
83     else query.bindValue(":fileid", "NULL");
84     int id = -1;
85     if (query.exec())
86         id = query.lastInsertId().toInt();
87     else
88         qDebug() << "Failed inserting to " << tableName << " "
89             << query.lastError().text() << " " << query.executedQuery() ;
90     return id;
91 }
92
93 // WARNING: this will delete also all the databindings to selected platform
94 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
95 {
96     // TODO
97     return false;
98 }
99
100 QString DbEmuFrontFileObject::constructSelect(QString where) const
101 {
102     return QString("SELECT maintbl.id AS FileObjectId, "
103             "maintbl.name AS Name, "
104             "file.id AS FileId, "
105             "file.name AS FileName, "
106             "file.type AS FileType, "
107             "file.checksum AS FileChecksum, "
108             "file.size AS FileSize, "
109             "file.updatetime AS FileUpdateTime "
110             "FROM %1 AS maintbl "
111             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
112             "%2 "
113             "ORDER BY Name").arg(tableName).arg(where);
114 }
115
116 QString DbEmuFrontFileObject::constructSelectById(int id) const
117 {
118     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
119 }
120
121 QString DbEmuFrontFileObject::constructFilterById(int id) const
122 {
123     return QString("maintbl.id = %1").arg(id);
124 }
125
126 QSqlQueryModel* DbEmuFrontFileObject::getData()
127 {
128     QSqlQueryModel *model = new QSqlQueryModel(this);
129     model->setQuery(constructSelect());
130     model->setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("Id"));
131     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
132     return model;
133 }
134
135 QString DbEmuFrontFileObject::getDeleteObjectSql() const
136 {
137     return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);
138 }