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 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);
52 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
54 const Executable *ex = dynamic_cast<const Executable*>(ob);
57 q.prepare("UPDATE executable SET "
59 "executable=:executable, "
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());
73 qDebug() << q.lastError().text();
77 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
79 const Executable *ex = dynamic_cast<const Executable*>(ob);
81 q.prepare("INSERT INTO executable "
82 "(id, name, executable, options, setupid, type) "
83 "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
85 if (!ex->getSetup() || ex->getSetup()->getId() < 0) {
86 qDebug() << "Setup not available!";
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());
96 id = q.lastInsertId().toInt();
97 else qDebug() << q.lastError().text();
101 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
107 int DbExecutable::countDataObjectRefs(int) const
113 QString DbExecutable::constructSelectById(int id) const
115 return constructSelect(
116 QString("WHERE %1").arg(constructFilterById(id)));
119 QString DbExecutable::constructFilterById(int id) const
121 return QString("executable.id=%1").arg(id);
124 QString DbExecutable::constructSelect(QString whereClause) const
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 "
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 "
139 "ORDER BY executable.name ")
143 bool DbExecutable::deleteDataObject(int id) const
149 QSqlQueryModel* DbExecutable::getData()
151 QSqlQueryModel *model = new QSqlQueryModel;
152 QString select = constructSelect();
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);