Deleting of Setup objects.
[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 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.
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     tableName = DbSetup::DB_TABLE_NAME_SETUP;
33     dbPlatform = new DbPlatform(this);
34     dbMediaType = new DbMediaType(this);
35 }
36
37 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
38 {
39     Setup *s = 0;
40     if (!rec) return s;
41     int id = rec->value(Setup_Id).toInt();
42     QString extensions = rec->value(Setup_FileTypeExtensions).toString();
43     QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
44     int plfId = rec->value(Setup_PlatformId).toInt();
45     int mtId = rec->value(Setup_MediaTypeId).toInt();
46     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
47     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
48     s = new Setup(id, plf, mt, list);
49     return s;
50 }
51
52 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
53 {
54     const Setup *fpo = dynamic_cast<const Setup*>(ob);
55     bool ret = false;
56     QSqlQuery query;
57     query.prepare(QString("UPDATE setup SET "
58         "platformid=:platformid, "
59         "mediatypeid=:mediatypeid, "
60         "filetypeextensions=:filetypeextensions "
61         "WHERE id = :id"));
62     if (fpo->getPlatform())
63         query.bindValue(":platformid", fpo->getPlatform()->getId());
64     if (fpo->getMediaType())
65         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
66     query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
67     query.bindValue(":id", fpo->getId());
68     ret = query.exec();
69     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
70     return ret;
71 }
72
73 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
74 {
75     qDebug() << "Inserting setup to database...";
76     const Setup *fpo = dynamic_cast<const Setup*>(ob);
77     QSqlQuery query;
78     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
79         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
80     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
81     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
82     QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
83     qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
84     query.bindValue(":platformid", plfId);
85     query.bindValue(":mediatypeid", mtId);
86     query.bindValue(":filetypeextensions", exts);
87     int id = -1;
88     if (query.exec())
89         id = query.lastInsertId().toInt();
90     else
91         qDebug() << query.lastError().text() << query.executedQuery();
92     return id;
93 }
94
95 QString DbSetup::constructSelect(QString where) const
96 {
97     /*QString where = whereClause.isEmpty()
98         ? "" : QString("WHERE ").append(whereClause);*/
99     return QString(
100         "SELECT setup.id AS SetupId, "
101         "setup.platformid AS PlatformId, "
102         "setup.mediatypeid AS MediaTypeId, "
103         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
104         "platform.name || ' ' || mediatype.name AS SetupName "
105         "FROM setup "
106         "INNER JOIN platform ON setup.platformid=platform.id "
107         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
108         "ORDER BY SetupName"
109         ).arg(where);
110 }
111
112 QString DbSetup::constructFilterById(int id) const
113 {
114      return QString("setup.id = %1").arg(id);
115 }
116
117
118 QString DbSetup::constructSelectById(int id) const
119 {
120     return constructSelect(
121         QString("WHERE %1").arg(constructFilterById(id)));
122 }
123
124 // WARNING: this will delete also all the databindings to selected media image path
125 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
126 {
127     qDebug() << "This is not currently supported";
128     return false;
129 }
130
131 /*bool DbSetup::deleteDataObject(int id) const
132 {
133     int c = countDataObjectRefs(id);
134     if (c != 0)
135         // TODO
136         return false;
137     QSqlQuery q;
138     q.prepare(QString("DELETE FROM setup WHERE id=:id"));
139     q.bindValue(":id", id);
140     return q.exec();
141 }*/
142
143 QSqlQueryModel* DbSetup::getData()
144 {
145     QSqlQueryModel *model = new QSqlQueryModel;
146     QString select = constructSelect();
147     qDebug() << select;
148     model->setQuery(select);
149     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
150     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
151     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
152     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
153     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
154     return model;
155 }
156
157 QString DbSetup::getCountRefsSelect(int id) const
158 {
159     /* setups are referenced by executable and filepath */
160     return QString("SELECT count(*) FROM "
161             "(SELECT setup.id FROM setup "
162               "INNER JOIN executable ON setup.id=executable.setupid "
163               "WHERE setup.id=%1 "
164               "UNION ALL "
165               "SELECT setup.id FROM setup "
166               "INNER JOIN filepath ON setup.id=filepath.setupid "
167               "WHERE setup.id=%1)").arg(id);
168 }