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)
31 EmuFrontObject* EmuFrontQueryModel::getObject(int row)
33 if (row < 0 || row > rowCount() - 1)
35 QSqlRecord rec = record(row);
36 return recordToDataObject(&rec);
39 EmuFrontObject* EmuFrontQueryModel::getDataObject(int id)
42 return getFilteredDataObject();
45 EmuFrontObject* EmuFrontQueryModel::getDataObject(QString filter)
47 QList<QString> filters;
48 filters.append(filter);
49 filterDataObjects(filters);
50 return getFilteredDataObject();
53 EmuFrontObject* EmuFrontQueryModel::getFilteredDataObject()
55 EmuFrontObject *plf = 0;
56 // TODO: if record has more than one the first instance is returned
57 // ... check if this is ok in all cases!
60 QSqlRecord rec = record(0);
64 else plf = recordToDataObject(&rec);
69 EmuFrontObject* EmuFrontQueryModel::getDataObject(const QModelIndex &index)
71 QSqlRecord rec = record(index.row());
72 return recordToDataObject(&rec);
75 int EmuFrontQueryModel::getCurrentTimeStamp() {
76 return QDateTime::currentDateTime().toTime_t();
79 void EmuFrontQueryModel::filterById(int id)
81 QList<QString> filters;
82 filters.append(constructFilterById(id));
83 filterDataObjects(filters);
86 /* filters is a list of SQL conditions e.g. setup.id=1 */
87 void EmuFrontQueryModel::filterDataObjects(QList<QString> filters)
89 QString where = constructWhereByFilters(filters);
90 QString query = constructSelect(where);
94 QString EmuFrontQueryModel::constructWhereByFilters(QList<QString>filters)
96 if (filters.count() == 0) return "";
97 QString where = " WHERE ";
99 foreach(QString filter, filters){
100 where.append(QString(" %1 ").arg(filter));
101 if (++c < filters.count())
102 where.append(" AND ");
107 void EmuFrontQueryModel::clearFilters()
109 setQuery(constructSelect());