Added counting of reference dependencies. Added also new triggers for
[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 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 QString DbExecutable::constructSelectById(int id) const
108 {
109     return constructSelect(
110         QString("WHERE %1").arg(constructFilterById(id)));
111 }
112
113 QString DbExecutable::constructFilterById(int id) const
114 {
115     return QString("executable.id=%1").arg(id);
116 }
117
118 QString DbExecutable::constructSelect(QString whereClause) const
119 {
120     return QString("SELECT "
121         "executable.id AS ExecutableId, "
122         "executable.name AS ExecutableName, "
123         "executable.executable AS Executable, "
124         "executable.options AS ExecutableOptions, "
125         "executable.type AS ExecutableType, "
126         "setup.id As ExecutableSetupId, "
127         "platform.name || ' ' || mediatype.name AS SetupName "
128         "FROM executable "
129         "INNER JOIN setup ON executable.setupid = setup.id "
130         "INNER JOIN platform ON setup.platformid=platform.id "
131         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
132         "%1 "
133         "ORDER BY executable.name ")
134         .arg(whereClause);
135 }
136
137 bool DbExecutable::deleteDataObject(int id) const
138 {
139     // TODO
140     return false;
141 }
142
143 QSqlQueryModel* DbExecutable::getData()
144 {
145     QSqlQueryModel *model = new QSqlQueryModel;
146     QString select = constructSelect();
147     qDebug() << select;
148     model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
149     model->setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
150     model->setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
151     model->setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
152     model->setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
153     model->setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
154     model->setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
155     model->setQuery(select);
156     return model;
157 }
158
159 QString DbExecutable::getCountRefsSelect(int id) const
160 {
161     // These objects don't have references from other objects
162     // currently.
163     return QString("SELECT 0");
164 }