0b648bf2222e405c97066d60865e8e231edf7618
[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().trimmed();
43     QStringList list;
44     if (!extensions.isEmpty())
45         list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
46     int plfId = rec->value(Setup_PlatformId).toInt();
47     int mtId = rec->value(Setup_MediaTypeId).toInt();
48     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
49     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
50     s = new Setup(id, plf, mt, list);
51     return s;
52 }
53
54 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
55 {
56     const Setup *fpo = dynamic_cast<const Setup*>(ob);
57     bool ret = false;
58     QSqlQuery query;
59     query.prepare(QString("UPDATE setup SET "
60         "platformid=:platformid, "
61         "mediatypeid=:mediatypeid, "
62         "filetypeextensions=:filetypeextensions "
63         "WHERE id = :id"));
64     if (fpo->getPlatform())
65         query.bindValue(":platformid", fpo->getPlatform()->getId());
66     if (fpo->getMediaType())
67         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
68     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
69     query.bindValue(":id", fpo->getId());
70     ret = query.exec();
71     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
72     return ret;
73 }
74
75 QString DbSetup::supportedExtensionsToDb(QStringList list)
76 {
77     return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
78 }
79
80 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
81 {
82     qDebug() << "Inserting setup to database...";
83     const Setup *fpo = dynamic_cast<const Setup*>(ob);
84     QSqlQuery query;
85     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
86         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
87     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
88     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
89     query.bindValue(":platformid", plfId);
90     query.bindValue(":mediatypeid", mtId);
91     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
92     int id = -1;
93     if (query.exec())
94         id = query.lastInsertId().toInt();
95     else
96         qDebug() << query.lastError().text() << query.executedQuery();
97     return id;
98 }
99
100 QString DbSetup::constructSelect(QString where) const
101 {
102     return QString(
103         "SELECT setup.id AS SetupId, "
104         "setup.platformid AS PlatformId, "
105         "setup.mediatypeid AS MediaTypeId, "
106         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
107         "platform.name || ' ' || mediatype.name AS SetupName "
108         "FROM setup "
109         "INNER JOIN platform ON setup.platformid=platform.id "
110         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
111         "ORDER BY SetupName"
112         ).arg(where);
113 }
114
115 QString DbSetup::constructFilterById(int id) const
116 {
117      return QString("setup.id = %1").arg(id);
118 }
119
120
121 QString DbSetup::constructSelectById(int id) const
122 {
123     return constructSelect(
124         QString("WHERE %1").arg(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 QSqlQueryModel* DbSetup::getData()
135 {
136     QSqlQueryModel *model = new QSqlQueryModel(this);
137     QString select = constructSelect();
138     qDebug() << select;
139     model->setQuery(select);
140     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
141     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
142     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
143     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
144     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
145     return model;
146 }
147
148 QString DbSetup::getCountRefsSelect(int id) const
149 {
150     /* setups are referenced by executable and filepath */
151     return QString("SELECT count(*) FROM "
152             "(SELECT setup.id FROM setup "
153               "INNER JOIN executable ON setup.id=executable.setupid "
154               "WHERE setup.id=%1 "
155               "UNION ALL "
156               "SELECT setup.id FROM setup "
157               "INNER JOIN filepath ON setup.id=filepath.setupid "
158               "WHERE setup.id=%1)").arg(id);
159 }