Initial implementation DbExecutable: data base functionality for
[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     // TODO
97     return -1;
98 }
99
100 QString DbSetup::constructSelect(QString where) const
101 {
102     /*QString where = whereClause.isEmpty()
103         ? "" : QString("WHERE ").append(whereClause);*/
104     return QString(
105         "SELECT setup.id AS SetupId, "
106         "setup.platformid AS PlatformId, "
107         "setup.mediatypeid AS MediaTypeId, "
108         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
109         "platform.name || ' ' || mediatype.name AS SetupName "
110         "FROM setup "
111         "INNER JOIN platform ON setup.platformid=platform.id "
112         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
113         "ORDER BY SetupName"
114         ).arg(where);
115 }
116
117 QString DbSetup::constructFilterById(int id) const
118 {
119      return QString("setup.id = %1").arg(id);
120 }
121
122
123 QString DbSetup::constructSelectById(int id) const
124 {
125     return constructSelect(
126         QString("WHERE %1").arg(constructFilterById(id)));
127 }
128
129 // WARNING: this will delete also all the databindings to selected media image path
130 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
131 {
132     qDebug() << "This is not currently supported";
133     return false;
134 }
135
136 bool DbSetup::deleteDataObject(int id) const
137 {
138     int c = countDataObjectRefs(id);
139     if (c != 0)
140         // TODO
141         return false;
142     QSqlQuery q;
143     q.prepare(QString("DELETE FROM setup WHERE id=:id"));
144     q.bindValue(":id", id);
145     return q.exec();
146 }
147
148 QSqlQueryModel* DbSetup::getData()
149 {
150     QSqlQueryModel *model = new QSqlQueryModel;
151     QString select = constructSelect();
152     qDebug() << select;
153     model->setQuery(select);
154     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
155     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
156     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
157     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
158     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
159     return model;
160 }