3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
21 #include "emufrontquerymodel.h"
22 #include "emufrontobject.h"
24 #include "mediatype.h"
27 EmuFrontQueryModel::EmuFrontQueryModel(QObject *parent) :
28 QSqlQueryModel(parent)
32 EmuFrontObject* EmuFrontQueryModel::getObject(int row) const
38 /*Platform* EmuFrontQueryModel::getPlatform(int id) const
42 EmuFrontObject* EmuFrontQueryModel::getDataObject(int id)
45 return getFilteredDataObject();
48 EmuFrontObject* EmuFrontQueryModel::getDataObject(QString filter)
50 QList<QString> filters;
51 filters.append(filter);
52 filterDataObjects(filters);
53 return getFilteredDataObject();
56 EmuFrontObject* EmuFrontQueryModel::getFilteredDataObject()
58 EmuFrontObject *plf = 0;
59 // TODO: if record has more than one the first instance is returned
60 // ... check if this is ok in all cases!
63 QSqlRecord rec = record(0);
67 else plf = recordToDataObject(&rec);
72 EmuFrontObject* EmuFrontQueryModel::getDataObject(const QModelIndex &index)
74 QSqlRecord rec = record(index.row());
75 return recordToDataObject(&rec);
78 int EmuFrontQueryModel::getCurrentTimeStamp() {
79 return QDateTime::currentDateTime().toTime_t();
82 void EmuFrontQueryModel::filterById(int id)
84 QList<QString> filters;
85 filters.append(constructFilterById(id));
86 filterDataObjects(filters);
89 /* filters is a list of SQL conditions e.g. setup.id=1 */
90 void EmuFrontQueryModel::filterDataObjects(QList<QString> filters)
92 QString where = constructWhereByFilters(filters);
93 QString query = constructSelect(where);
97 QString EmuFrontQueryModel::constructWhereByFilters(QList<QString>filters)
99 if (filters.count() == 0) return "";
100 QString where = " WHERE ";
102 foreach(QString filter, filters){
103 where.append(QString(" %1 ").arg(filter));
104 if (++c < filters.count())
105 where.append(" AND ");
110 void EmuFrontQueryModel::clearFilters()
112 setQuery(constructSelect());