Imported needed functionality from (soon) deprecated database
[emufront] / src / models / emufrontquerymodel.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
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.
12 **
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.
17 **
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/>.
20 */
21 #include "emufrontquerymodel.h"
22 #include "emufrontobject.h"
23 #include "platform.h"
24 #include "mediatype.h"
25 #include <QtSql>
26
27 EmuFrontQueryModel::EmuFrontQueryModel(QObject *parent) :
28     QSqlQueryModel(parent)
29 {
30 }
31
32 EmuFrontObject* EmuFrontQueryModel::getObject(int row) const
33 {
34     // TODO
35     return 0;
36 }
37
38 /*Platform* EmuFrontQueryModel::getPlatform(int id) const
39 {
40 }*/
41
42 EmuFrontObject* EmuFrontQueryModel::getDataObject(int id)
43 {
44     filterById(id);
45     return getFilteredDataObject();
46 }
47
48 EmuFrontObject* EmuFrontQueryModel::getDataObject(QString filter)
49 {
50     QList<QString> filters;
51     filters.append(filter);
52     filterDataObjects(filters);
53     return getFilteredDataObject();
54 }
55
56 EmuFrontObject* EmuFrontQueryModel::getFilteredDataObject()
57 {
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!
61     if (rowCount() >= 1)
62     {
63         QSqlRecord rec = record(0);
64         if (rec.isEmpty()) {
65             return 0;
66         }
67         else plf = recordToDataObject(&rec);
68     }
69      return plf;
70 }
71
72 EmuFrontObject* EmuFrontQueryModel::getDataObject(const QModelIndex &index)
73 {
74     QSqlRecord rec = record(index.row());
75     return recordToDataObject(&rec);
76 }
77
78 int EmuFrontQueryModel::getCurrentTimeStamp() {
79     return QDateTime::currentDateTime().toTime_t();
80 }
81
82 void EmuFrontQueryModel::filterById(int id)
83 {
84     QList<QString> filters;
85     filters.append(constructFilterById(id));
86     filterDataObjects(filters);
87 }
88
89 /* filters is a list of SQL conditions e.g. setup.id=1 */
90 void EmuFrontQueryModel::filterDataObjects(QList<QString> filters)
91 {
92     QString where = constructWhereByFilters(filters);
93     QString query = constructSelect(where);
94     setQuery(query);
95 }
96
97 QString EmuFrontQueryModel::constructWhereByFilters(QList<QString>filters)
98 {
99     if (filters.count() == 0) return "";
100     QString where = " WHERE ";
101     int c = 0;
102     foreach(QString filter, filters){
103         where.append(QString(" %1 ").arg(filter));
104         if (++c < filters.count())
105             where.append(" AND ");
106     }
107     return where;
108 }
109
110 void EmuFrontQueryModel::clearFilters()
111 {
112     setQuery(constructSelect());
113 }
114