Implemented initial deleteDataObject(id) in Db-layer. Implemented
[emufront] / src / db / dbsetup.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
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.
16 //
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/>.
19
20 #include <QDebug>
21 #include <QStringList>
22 #include <QSqlRecord>
23 #include <QSqlQuery>
24 #include <QSqlError>
25 #include <QSqlRelationalTableModel>
26 #include "dbsetup.h"
27
28 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
29
30 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
31 {
32     dbPlatform = new DbPlatform(this);
33     dbMediaType = new DbMediaType(this);
34 }
35
36 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
37 {
38     Setup *s = 0;
39     if (!rec) return s;
40     int id = rec->value(Setup_Id).toInt();
41     QString extensions = rec->value(Setup_FileTypeExtensions).toString();
42     QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
43     int plfId = rec->value(Setup_PlatformId).toInt();
44     int mtId = rec->value(Setup_MediaTypeId).toInt();
45     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
46     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
47     s = new Setup(id, plf, mt, list);
48     return s;
49 }
50
51 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
52 {
53     const Setup *fpo = dynamic_cast<const Setup*>(ob);
54     bool ret = false;
55     QSqlQuery query;
56     query.prepare(QString("UPDATE setup SET "
57         "platformid=:platformid, "
58         "mediatypeid=:mediatypeid, "
59         "filetypeextensions=:filetypeextensions "
60         "WHERE id = :id"));
61     if (fpo->getPlatform())
62         query.bindValue(":platformid", fpo->getPlatform()->getId());
63     if (fpo->getMediaType())
64         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
65     query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
66     query.bindValue(":id", fpo->getId());
67     ret = query.exec();
68     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
69     return ret;
70 }
71
72 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
73 {
74     qDebug() << "Inserting setup to database...";
75     const Setup *fpo = dynamic_cast<const Setup*>(ob);
76     QSqlQuery query;
77     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
78         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
79     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
80     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
81     QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
82     qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
83     query.bindValue(":platformid", plfId);
84     query.bindValue(":mediatypeid", mtId);
85     query.bindValue(":filetypeextensions", exts);
86     int id = -1;
87     if (query.exec())
88         id = query.lastInsertId().toInt();
89     else
90         qDebug() << query.lastError().text() << query.executedQuery();
91     return id;
92 }
93
94 int DbSetup::countDataObjectRefs(int ) const
95 {
96     return 0;
97 }
98
99 QString DbSetup::constructSelect(QString whereClause) const
100 {
101     QString where = whereClause.isEmpty()
102         ? "" : QString("WHERE ").append(whereClause);
103     return QString(
104         "SELECT setup.id AS SetupId, "
105         "setup.platformid AS PlatformId, "
106         "setup.mediatypeid AS MediaTypeId, "
107         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
108         "platform.name || ' ' || mediatype.name AS SetupName "
109         "FROM setup "
110         "INNER JOIN platform ON setup.platformid=platform.id "
111         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
112         "ORDER BY SetupName"
113         ).arg(where);
114 }
115
116 QString DbSetup::constructFilterById(int id) const
117 {
118      return QString("setup.id = %1").arg(id);
119 }
120
121
122 QString DbSetup::constructSelectById(int id) const
123 {
124      return constructSelect(constructFilterById(id));
125 }
126
127 // WARNING: this will delete also all the databindings to selected media image path
128 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
129 {
130     qDebug() << "This is not currently supported";
131     return false;
132 }
133
134 bool DbSetup::deleteDataObject(int id) const
135 {
136     if (countDataObjectRefs(id) > 0)
137         // TODO
138         return false;
139     QSqlQuery q;
140     q.prepare(QString("DELETE FROM setup WHERE id=:id"));
141     q.bindValue(":id", id);
142     return q.exec();
143 }
144
145 QSqlQueryModel* DbSetup::getData()
146 {
147     QSqlQueryModel *model = new QSqlQueryModel;
148     QString select = constructSelect();
149     qDebug() << select;
150     model->setQuery(select);
151     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
152     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
153     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
154     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
155     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
156     return model;
157 }