Delete of Executable objects.
[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 "../dataobjects/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 EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
38 {
39     Executable *ex = 0;
40     if (!rec) return ex;
41     int id = rec->value(Executable_Id).toInt();
42     int supid = rec->value(Executable_SetupId).toInt();
43     EmuFrontObject *ob = dbSetup->getDataObject(supid);
44     Setup *sup = dynamic_cast<Setup*>(ob);
45     qDebug() << "Setup id " << sup->getId() << ", platform " << sup->getPlatform()->getName();
46     QString name = rec->value(Executable_Name).toString();
47     QString exec = rec->value(Executable_Executable).toString();
48     QString opts = rec->value(Executable_Options).toString();
49     int type = rec->value(Executable_TypeId).toInt();
50     ex = new Executable(id, name, exec, opts, sup, type);
51     return ex;
52 }
53
54 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
55 {
56     const Executable *ex = dynamic_cast<const Executable*>(ob);
57     bool ret = false;
58     QSqlQuery q;
59     q.prepare("UPDATE executable SET "
60               "name=:name, "
61               "executable=:executable, "
62               "options=:options, "
63               "setupid=:setupid, "
64               "type=:type "
65               "WHERE id=:id");
66     // TODO: null check
67     q.bindValue(":setupid", ex->getSetup()->getId());
68     q.bindValue(":name", ex->getName());
69     q.bindValue(":executable", ex->getExecutable());
70     q.bindValue(":options", ex->getOptions());
71     q.bindValue(":type", ex->getType());
72     q.bindValue(":id", ex->getId());
73     ret = q.exec();
74     if (!ret)
75         qDebug() << q.lastError().text();
76     return ret;
77 }
78
79 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
80 {
81     const Executable *ex = dynamic_cast<const Executable*>(ob);
82     QSqlQuery q;
83     q.prepare("INSERT INTO executable "
84         "(id, name, executable, options, setupid, type) "
85         "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
86
87     if (!ex->getSetup() || ex->getSetup()->getId() < 0) {
88         qDebug() << "Setup not available!";
89         return -1;
90     }
91     q.bindValue(":setupid", ex->getSetup()->getId());
92     q.bindValue(":name", ex->getName());
93     q.bindValue(":executable", ex->getExecutable());
94     q.bindValue(":options", ex->getOptions());
95     q.bindValue(":type", ex->getType());
96     int id = -1;
97     if (q.exec())
98         id = q.lastInsertId().toInt();
99     else qDebug() << q.lastError().text();
100     return id;
101 }
102
103 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
104 {
105     // TODO
106     return false;
107 }
108
109 QString DbExecutable::constructSelectById(int id) const
110 {
111     return constructSelect(
112         QString("WHERE %1").arg(constructFilterById(id)));
113 }
114
115 QString DbExecutable::constructFilterById(int id) const
116 {
117     return QString("executable.id=%1").arg(id);
118 }
119
120 QString DbExecutable::constructSelect(QString whereClause) const
121 {
122     return QString("SELECT "
123         "executable.id AS ExecutableId, "
124         "executable.name AS ExecutableName, "
125         "executable.executable AS Executable, "
126         "executable.options AS ExecutableOptions, "
127         "executable.type AS ExecutableType, "
128         "setup.id As ExecutableSetupId, "
129         "platform.name || ' ' || mediatype.name AS SetupName "
130         "FROM executable "
131         "INNER JOIN setup ON executable.setupid = setup.id "
132         "INNER JOIN platform ON setup.platformid=platform.id "
133         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
134         "%1 "
135         "ORDER BY executable.name ")
136         .arg(whereClause);
137 }
138
139 /*bool DbExecutable::deleteDataObject(int id) const
140 {
141     // TODO
142     return false;
143 }*/
144
145 QSqlQueryModel* DbExecutable::getData()
146 {
147     QSqlQueryModel *model = new QSqlQueryModel;
148     QString select = constructSelect();
149     qDebug() << select;
150     model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
151     model->setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
152     model->setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
153     model->setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
154     model->setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
155     model->setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
156     model->setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
157     model->setQuery(select);
158     return model;
159 }
160
161 QString DbExecutable::getCountRefsSelect(int id) const
162 {
163     // These objects don't have references from other objects
164     // currently.
165     return QString("SELECT 0");
166 }