Fixed init error in Executable constructor and sql error in executable
[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     EmuFrontObject *ob = dbSetup->getDataObject(supid);
42     Setup *sup = dynamic_cast<Setup*>(ob);
43     qDebug() << "Setup id " << sup->getId() << ", platform " << sup->getPlatform()->getName();
44     QString name = rec->value(Executable_Name).toString();
45     QString exec = rec->value(Executable_Executable).toString();
46     QString opts = rec->value(Executable_Options).toString();
47     int type = rec->value(Executable_TypeId).toInt();
48     ex = new Executable(id, name, exec, opts, sup, type);
49     return ex;
50 }
51
52 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
53 {
54     const Executable *ex = dynamic_cast<const Executable*>(ob);
55     bool ret = false;
56     QSqlQuery q;
57     q.prepare("UPDATE executable SET "
58               "name=:name, "
59               "executable=:executable, "
60               "options=:options, "
61               "setupid=:setupid, "
62               "type=:type "
63               "WHERE id=:id");
64     // TODO: null check
65     q.bindValue(":setupid", ex->getSetup()->getId());
66     q.bindValue(":name", ex->getName());
67     q.bindValue(":executable", ex->getExecutable());
68     q.bindValue(":options", ex->getOptions());
69     q.bindValue(":type", ex->getType());
70     q.bindValue(":id", ex->getId());
71     ret = q.exec();
72     if (!ret)
73         qDebug() << q.lastError().text();
74     return ret;
75 }
76
77 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
78 {
79     const Executable *ex = dynamic_cast<const Executable*>(ob);
80     QSqlQuery q;
81     q.prepare("INSERT INTO executable "
82         "(id, name, executable, options, setupid, type) "
83         "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
84
85     if (!ex->getSetup() || ex->getSetup()->getId() < 0) {
86         qDebug() << "Setup not available!";
87         return -1;
88     }
89     q.bindValue(":setupid", ex->getSetup()->getId());
90     q.bindValue(":name", ex->getName());
91     q.bindValue(":executable", ex->getExecutable());
92     q.bindValue(":options", ex->getOptions());
93     q.bindValue(":type", ex->getType());
94     int id = -1;
95     if (q.exec())
96         id = q.lastInsertId().toInt();
97     else qDebug() << q.lastError().text();
98     return id;
99 }
100
101 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
102 {
103     // TODO
104     return false;
105 }
106
107 int DbExecutable::countDataObjectRefs(int) const
108 {
109     // TODO
110     return 0;
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     return 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 ")
140         .arg(whereClause);
141 }
142
143 bool DbExecutable::deleteDataObject(int id) const
144 {
145     // TODO
146     return false;
147 }
148
149 QSqlQueryModel* DbExecutable::getData()
150 {
151     QSqlQueryModel *model = new QSqlQueryModel;
152     QString select = constructSelect();
153     qDebug() << select;
154     model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
155     model->setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
156     model->setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
157     model->setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
158     model->setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
159     model->setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
160     model->setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
161     model->setQuery(select);
162     return model;
163 }
164