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 cyopy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
21 #include <QSqlQueryModel>
24 #include "dbquerymodelmanager.h"
26 DbQueryModelManager::DbQueryModelManager(QObject *parent)
27 : DatabaseManager(parent)
31 void DbQueryModelManager::filterById(int id)
33 QList<QString> filters;
34 filters.append(constructFilterById(id));
35 filterDataObjects(filters);
38 /* filters is a list of SQL conditions e.g. setup.id=1 */
39 void DbQueryModelManager::filterDataObjects(QList<QString> filters)
41 //if (!sqlTableModel) sqlTableModel = getDataModel();
44 QString where = constructWhereByFilters(filters);
45 //qDebug() << "Constructing SQL with " << where;
46 QString query = constructSelect(where);
47 sqlTableModel->setQuery(query);
50 QString DbQueryModelManager::constructWhereByFilters(QList<QString>filters)
52 if (filters.count() == 0) return "";
53 QString where = " WHERE ";
55 foreach(QString filter, filters){
56 where.append(QString(" %1 ").arg(filter));
57 if (++c < filters.count())
58 where.append(" AND ");
60 //qDebug() << "constructWhereByFilters: " << where;
64 void DbQueryModelManager::clearFilters()
66 sqlTableModel->setQuery(constructSelect());
69 bool DbQueryModelManager::deleteDataObject(int id) const
71 QString sql = getDeleteObjectSql();
75 q.bindValue(":id", id);
78 qDebug() << q.lastError().text();
83 QString DbQueryModelManager::getDeleteObjectSql() const
85 return QString("DELETE FROM %1 WHERE id=:id").arg(tableName);