2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
24 #include <QSqlRelationalTableModel>
25 #include "dbexecutable.h"
27 #include "../dataobjects/executable.h"
29 DbExecutable::DbExecutable(QObject *parent)
30 : DbQueryModelManager(parent)
32 dbSetup = new DbSetup(this);
35 EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
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);
50 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
52 const Executable *ex = dynamic_cast<const Executable*>(ob);
55 q.prepare("UPDATE executable SET "
57 "executable=:executable, "
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());
71 qDebug() << q.lastError().text();
75 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
77 const Executable *ex = dynamic_cast<const Executable*>(ob);
79 q.prepare("INSERT INTO executable "
80 "(id, name, executable, options, setupid, type) "
81 "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
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());
91 id = q.lastInsertId().toInt();
92 else qDebug() << q.lastError().text();
96 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
102 int DbExecutable::countDataObjectRefs(int) const
108 QString DbExecutable::constructSelectById(int id) const
110 return constructSelect(
111 QString("WHERE %1").arg(constructFilterById(id)));
114 QString DbExecutable::constructFilterById(int id) const
116 return QString("executable.id=%1").arg(id);
119 QString DbExecutable::constructSelect(QString whereClause) const
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 "
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 "
134 "ORDER BY executable.name ")
138 bool DbExecutable::deleteDataObject(int id) const
144 QSqlQueryModel* DbExecutable::getData()
146 QSqlQueryModel *model = new QSqlQueryModel;
147 QString select = constructSelect();
149 model->setQuery(select);