Refactoring the project, new folders for view and model classes. Added
[emufront] / src / db / dbexecutable.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 <QDebug>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlError>
24 #include <QSqlRelationalTableModel>
25 #include "dbexecutable.h"
26 #include "dbsetup.h"
27 #include "executable.h"
28
29
30 DbExecutable::DbExecutable(QObject *parent)
31     : DbQueryModelManager(parent)
32 {
33     dbSetup = new DbSetup(this);
34     tableName = DbExecutable::DB_TABLE_EXECUTABLE;
35 }
36
37
38 /* Throws EmuFrontException */
39 EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
40 {
41     Executable *ex = 0;
42     if (!rec) return ex;
43     int id = rec->value(Executable_Id).toInt();
44     int supid = rec->value(Executable_SetupId).toInt();
45     EmuFrontObject *ob = dbSetup->getDataObject(supid); /* Throws EmuFrontException */
46     Setup *sup = dynamic_cast<Setup*>(ob);
47     qDebug() << "Setup id " << sup->getId() << ", platform " << sup->getPlatform()->getName();
48     QString name = rec->value(Executable_Name).toString();
49     QString exec = rec->value(Executable_Executable).toString();
50     QString opts = rec->value(Executable_Options).toString();
51     int type = rec->value(Executable_TypeId).toInt();
52     ex = new Executable(id, name, exec, opts, sup, type);
53     return ex;
54 }
55
56 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
57 {
58     const Executable *ex = dynamic_cast<const Executable*>(ob);
59     bool ret = false;
60     QSqlQuery q;
61     q.prepare("UPDATE executable SET "
62               "name=:name, "
63               "executable=:executable, "
64               "options=:options, "
65               "setupid=:setupid, "
66               "type=:type "
67               "WHERE id=:id");
68     // TODO: null check
69     q.bindValue(":setupid", ex->getSetup()->getId());
70     q.bindValue(":name", ex->getName());
71     q.bindValue(":executable", ex->getExecutable());
72     q.bindValue(":options", ex->getOptions());
73     q.bindValue(":type", ex->getType());
74     q.bindValue(":id", ex->getId());
75     ret = q.exec();
76     if (ret) resetModel();
77     if (!ret)
78         qDebug() << q.lastError().text();
79     return ret;
80 }
81
82 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
83 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
84 {
85     const Executable *ex = dynamic_cast<const Executable*>(ob);
86     QSqlQuery q;
87     q.prepare("INSERT INTO executable "
88         "(id, name, executable, options, setupid, type) "
89         "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
90
91     if (!ex->getSetup() || ex->getSetup()->getId() < 0) {
92         qDebug() << "Setup not available!";
93         return -1;
94     }
95     q.bindValue(":setupid", ex->getSetup()->getId());
96     q.bindValue(":name", ex->getName());
97     q.bindValue(":executable", ex->getExecutable());
98     q.bindValue(":options", ex->getOptions());
99     q.bindValue(":type", ex->getType());
100     int id = -1;
101     if (q.exec())
102         id = q.lastInsertId().toInt();
103     else qDebug() << q.lastError().text();
104     return id;
105 }
106
107 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
108 {
109     // TODO
110     return false;
111 }
112
113 QString DbExecutable::constructSelectById(int id) const
114 {
115     return constructSelect(
116         QString("WHERE %1").arg(constructFilterById(id)));
117 }
118
119 QString DbExecutable::constructFilterById(int id) const
120 {
121     return QString("executable.id=%1").arg(id);
122 }
123
124 QString DbExecutable::constructSelect(QString whereClause) const
125 {
126     QString sql = QString("SELECT "
127         "executable.id AS ExecutableId, "
128         "executable.name AS ExecutableName, "
129         "executable.executable AS Executable, "
130         "executable.options AS ExecutableOptions, "
131         "executable.type AS ExecutableType, "
132         "setup.id As ExecutableSetupId, "
133         "platform.name || ' ' || mediatype.name AS SetupName "
134         "FROM executable "
135         "INNER JOIN setup ON executable.setupid = setup.id "
136         "INNER JOIN platform ON setup.platformid=platform.id "
137         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
138         "%1 "
139         "ORDER BY executable.name ").arg(whereClause);
140     qDebug() << sql;
141     return sql;
142 }
143
144 /*bool DbExecutable::deleteDataObject(int id) const
145 {
146     // TODO
147     return false;
148 }*/
149
150 QSqlQueryModel* DbExecutable::getData()
151 {
152     QSqlQueryModel *model = new QSqlQueryModel(this);
153     QString select = constructSelect();
154     qDebug() << select;
155     model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
156     model->setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
157     model->setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
158     model->setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
159     model->setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
160     model->setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
161     model->setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
162     model->setQuery(select);
163     return model;
164 }
165
166 QString DbExecutable::getCountRefsSelect(int id) const
167 {
168     // These objects don't have references from other objects
169     // currently.
170     return QString("SELECT 0");
171 }
172
173 void DbExecutable::filterByPlatformMediaType(int platformId, int mediaTypeId)
174 {
175     QList<QString> filters;
176     filters.append(QString("setup.platformid=%1").arg(platformId));
177     filters.append(QString("setup.mediatypeid=%1").arg(mediaTypeId));
178     filterDataObjects(filters);
179 }