//
//
// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
//
// EmuFront is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
{
+ tableName = DbSetup::DB_TABLE_NAME_SETUP;
dbPlatform = new DbPlatform(this);
dbMediaType = new DbMediaType(this);
}
+/* Throws EmuFrontException */
EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
{
Setup *s = 0;
if (!rec) return s;
int id = rec->value(Setup_Id).toInt();
- QString extensions = rec->value(Setup_FileTypeExtensions).toString();
- QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
+ QString extensions = rec->value(Setup_FileTypeExtensions).toString().trimmed();
+ QStringList list;
+ if (!extensions.isEmpty())
+ list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
int plfId = rec->value(Setup_PlatformId).toInt();
int mtId = rec->value(Setup_MediaTypeId).toInt();
- Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
- MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
+ Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId)); /* Throws EmuFrontException */
+ MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId)); /* Throws EmuFrontException */
s = new Setup(id, plf, mt, list);
return s;
}
query.bindValue(":platformid", fpo->getPlatform()->getId());
if (fpo->getMediaType())
query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
- query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
+ query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
query.bindValue(":id", fpo->getId());
ret = query.exec();
+ if (ret) resetModel();
if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
return ret;
}
+QString DbSetup::supportedExtensionsToDb(QStringList list)
+{
+ return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
+}
+
int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
{
qDebug() << "Inserting setup to database...";
"VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
- QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
- qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
query.bindValue(":platformid", plfId);
query.bindValue(":mediatypeid", mtId);
- query.bindValue(":filetypeextensions", exts);
+ query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
int id = -1;
if (query.exec())
id = query.lastInsertId().toInt();
return id;
}
-int DbSetup::countDataObjectRefs(int ) const
-{
- // TODO
- return -1;
-}
-
QString DbSetup::constructSelect(QString where) const
{
- /*QString where = whereClause.isEmpty()
- ? "" : QString("WHERE ").append(whereClause);*/
return QString(
"SELECT setup.id AS SetupId, "
"setup.platformid AS PlatformId, "
return false;
}
-bool DbSetup::deleteDataObject(int id) const
-{
- int c = countDataObjectRefs(id);
- if (c != 0)
- // TODO
- return false;
- QSqlQuery q;
- q.prepare(QString("DELETE FROM setup WHERE id=:id"));
- q.bindValue(":id", id);
- return q.exec();
-}
-
QSqlQueryModel* DbSetup::getData()
{
- QSqlQueryModel *model = new QSqlQueryModel;
+ QSqlQueryModel *model = new QSqlQueryModel(this);
QString select = constructSelect();
qDebug() << select;
model->setQuery(select);
model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
return model;
}
+
+QString DbSetup::getCountRefsSelect(int id) const
+{
+ /* setups are referenced by executable and filepath */
+ return QString("SELECT count(*) FROM "
+ "(SELECT setup.id FROM setup "
+ "INNER JOIN executable ON setup.id=executable.setupid "
+ "WHERE setup.id=%1 "
+ "UNION ALL "
+ "SELECT setup.id FROM setup "
+ "INNER JOIN filepath ON setup.id=filepath.setupid "
+ "WHERE setup.id=%1)").arg(id);
+}