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 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.
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 "executable.h"
30 DbExecutable::DbExecutable(QObject *parent)
31 : DbQueryModelManager(parent)
33 dbSetup = new DbSetup(this);
34 tableName = DbExecutable::DB_TABLE_EXECUTABLE;
38 /* Throws EmuFrontException */
39 EmuFrontObject* DbExecutable::recordToDataObject(const QSqlRecord* rec)
43 int id = rec->value(Executable_Id).toInt();
44 int supid = rec->value(Executable_SetupId).toInt();
45 EmuFrontObject *ob = dbSetup->getDataObject(supid); /* Throws EmuFrontException */
46 Setup *sup = dynamic_cast<Setup*>(ob);
47 qDebug() << "Setup id " << sup->getId() << ", platform " << sup->getPlatform()->getName();
48 QString name = rec->value(Executable_Name).toString();
49 QString exec = rec->value(Executable_Executable).toString();
50 QString opts = rec->value(Executable_Options).toString();
51 int type = rec->value(Executable_TypeId).toInt();
52 ex = new Executable(id, name, exec, opts, sup, type);
56 bool DbExecutable::updateDataObjectToModel(const EmuFrontObject* ob)
58 const Executable *ex = dynamic_cast<const Executable*>(ob);
61 q.prepare("UPDATE executable SET "
63 "executable=:executable, "
69 q.bindValue(":setupid", ex->getSetup()->getId());
70 q.bindValue(":name", ex->getName());
71 q.bindValue(":executable", ex->getExecutable());
72 q.bindValue(":options", ex->getOptions());
73 q.bindValue(":type", ex->getType());
74 q.bindValue(":id", ex->getId());
76 if (ret) resetModel();
78 qDebug() << q.lastError().text();
82 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
83 int DbExecutable::insertDataObjectToModel(const EmuFrontObject* ob)
85 const Executable *ex = dynamic_cast<const Executable*>(ob);
87 q.prepare("INSERT INTO executable "
88 "(id, name, executable, options, setupid, type) "
89 "VALUES (NULL, :name, :executable, :options, :setupid, :type)");
91 if (!ex->getSetup() || ex->getSetup()->getId() < 0) {
92 qDebug() << "Setup not available!";
95 q.bindValue(":setupid", ex->getSetup()->getId());
96 q.bindValue(":name", ex->getName());
97 q.bindValue(":executable", ex->getExecutable());
98 q.bindValue(":options", ex->getOptions());
99 q.bindValue(":type", ex->getType());
102 id = q.lastInsertId().toInt();
103 else qDebug() << q.lastError().text();
107 bool DbExecutable::deleteDataObjectFromModel(QModelIndex*)
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 QString sql = 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 ").arg(whereClause);
144 /*bool DbExecutable::deleteDataObject(int id) const
150 QSqlQueryModel* DbExecutable::getData()
152 QSqlQueryModel *model = new QSqlQueryModel(this);
153 QString select = constructSelect();
155 model->setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
156 model->setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
157 model->setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
158 model->setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
159 model->setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
160 model->setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
161 model->setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
162 model->setQuery(select);
166 QString DbExecutable::getCountRefsSelect(int id) const
168 // These objects don't have references from other objects
170 return QString("SELECT 0");
173 void DbExecutable::filterByPlatformMediaType(int platformId, int mediaTypeId)
175 QList<QString> filters;
176 filters.append(QString("setup.platformid=%1").arg(platformId));
177 filters.append(QString("setup.mediatypeid=%1").arg(mediaTypeId));
178 filterDataObjects(filters);