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