0b3ce08896437f12c31608f5925ab70bd9bd9c05
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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 "../dataobjects/executable.h"
28
29 DbExecutable::DbExecutable(QObject *parent)
30     : DbQueryModelManager(parent)
31 {
32     dbSetup = new DbSetup(this);
33 }
34
35 EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
36 {
37     Executable *ex = 0;
38     if (!rec) return ex;
39     int id = rec->value(Executable_Id).toInt();
40     int supid = rec->value(Executable_SetupId).toInt();
41     Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(supid));
42     QString name = rec->value(Executable_Name).toString();
43     QString exec = rec->value(Executable_Executable).toString();
44     QString opts = rec->value(Executable_Options).toString();
45     int type = rec->value(Executable_TypeId).toInt();
46     ex = new Executable(id, name, exec, opts, sup, type);
47     return ex;
48 }
49
50 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
51 {
52     const Executable *ex = dynamic_cast<const Executable*>(ob);
53     bool ret = false;
54     QSqlQuery q;
55     q.prepare("UPDATE executable SET "
56               "name=:name, "
57               "executable=:executable, "
58               "options=:options, "
59               "setupid=:setupid, "
60               "type=:type "
61               "WHERE id=:id");
62     q.bindValue(":setupid", ex->getSetup()
63                 ? QString(ex->getSetup()->getId()) : "NULL"); // TODO: null shouln't be allowed here
64     q.bindValue(":name", ex->getName());
65     q.bindValue(":executable", ex->getExecutable());
66     q.bindValue(":options", ex->getOptions());
67     q.bindValue(":type", ex->getType());
68     q.bindValue(":id", ex->getId());
69     ret = q.exec();
70     if (!ret)
71         qDebug() << q.lastError().text();
72     return ret;
73 }
74
75 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
76 {
77     const Executable *ex = dynamic_cast<const Executable*>(ob);
78     QSqlQuery q;
79     q.prepare("INSERT INTO executable "
80         "(id, name, executable, options, setupid, type) "
81         "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
82
83     q.bindValue(":setupid", ex->getSetup()
84                 ? QString(ex->getSetup()->getId()) : "NULL"); // TODO: null shouln't be allowed here
85     q.bindValue(":name", ex->getName());
86     q.bindValue(":executable", ex->getExecutable());
87     q.bindValue(":options", ex->getOptions());
88     q.bindValue(":type", ex->getType());
89     int id = -1;
90     if (q.exec())
91         id = q.lastInsertId().toInt();
92     else qDebug() << q.lastError().text();
93     return id;
94 }
95
96 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
97 {
98     // TODO
99     return false;
100 }
101
102 int DbExecutable::countDataObjectRefs(int) const
103 {
104     // TODO
105     return 0;
106 }
107
108 QString DbExecutable::constructSelectById(int id) const
109 {
110     return constructSelect(
111         QString("WHERE %1").arg(constructFilterById(id)));
112 }
113
114 QString DbExecutable::constructFilterById(int id) const
115 {
116     return QString("executable.id=%1").arg(id);
117 }
118
119 QString DbExecutable::constructSelect(QString whereClause) const
120 {
121     return QString("SELECT "
122         "executable.id AS ExecutableId, "
123         "executable.name AS ExecutableName, "
124         "executable.executable AS Executable, "
125         "executable.options AS ExecutableOptions, "
126         "executable.type AS ExecutableType, "
127         "setup.id As ExecutableSetupId "
128         "platform.name || ' ' || mediatype.name AS SetupName "
129         "FROM executable "
130         "INNER JOIN setup ON executable.setupid = setup.id "
131         "INNER JOIN platform ON setup.platformid=platform.id "
132         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
133         "% "
134         "ORDER BY executable.name ")
135         .arg(whereClause);
136 }
137
138 bool DbExecutable::deleteDataObject(int id) const
139 {
140     // TODO
141     return false;
142 }
143