2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
12 // Foobar 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.
17 // You should have received a copy of the GNU General Public License
18 // along with Foobar. If not, see <http://www.gnu.org/licenses/>.
20 #include <QSqlRelationalTableModel>
23 #include "../dataobjects/filepathobject.h"
24 #include "dbfilepath.h"
27 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
29 dbSetup = new DbSetup(this);
30 sqlTableModel = 0; //getData();
33 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
35 int id = rec->value(FilePath_Id).toInt();
36 QString fpath = rec->value(FilePath_Name).toString();
37 int setupId = rec->value(FilePath_SetupId).toInt();
38 //int fileType = rec->value(FilePath_FileTypeId).toInt();
39 Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
41 //int lastScanned = 0;
42 return new FilePathObject(id, fpath, fpath, 0, sup);
45 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
47 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
50 query.prepare(QString("UPDATE filepath SET "
52 "filetypeid=:filetypeid, "
54 "lastscanned=:lastscanned "
56 query.bindValue(":name", fpo->getName());
57 query.bindValue(":filetypeid", fpo->getFiletype());
58 query.bindValue(":lastscanned", 0); // TODO
59 query.bindValue(":id", fpo->getId());
61 if (ret) resetModel();
62 /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
63 sqlTableModel->select();
64 if (sqlTableModel->rowCount() == 1)
66 QSqlRecord rec = sqlTableModel->record(0);
67 rec.setValue("name", fpo->getName());
68 rec.setValue("filetypeid", fpo->getFilet.bype());
70 Setup *sup = fpo->getSetup();
71 if (sup) rec.setValue("setupid", sup->getId());
74 //rec.setValue("lastscanned", 0);
79 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
81 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
83 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
84 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
85 query.bindValue(":name", fpo->getName());
86 query.bindValue(":filetypeid", fpo->getFiletype());
88 query.bindValue(":setupid", fpo->getSetup()->getId());
89 query.bindValue(":lastscanned", 0); // TODO
93 sqlTableModel->insertRows(row, 1);
96 Setup *sup = fpo->getSetup();
97 //Platform *pl = fpo->getPlatform();
98 //MediaType *mt = fpo->getMediaType();
100 //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
101 sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
102 sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
103 // not all the file path types have platform and/or media type
104 //if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
105 //if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
106 if (sup) sqlTableModel->setData(sqlTableModel->index(row, FilePath_SetupId), sup->getId());
108 sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
109 return sqlTableModel->submitAll();*/
112 int DbFilePath::countDataObjectRefs(int id) const
117 // WARNING: this will delete also all the databindings to selected media image path
118 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
123 QString DbFilePath::constructSelect(QString whereClause) const
125 QString where = whereClause.isEmpty()
126 ? "" : QString("WHERE ").append(whereClause);
128 return QString("SELECT filepath.id AS FilePathId, "
129 "filepath.name AS Name, "
130 "filepath.lastscanned AS LastScanned, "
131 "setup.id AS SetupId, "
132 "platform.name || ' ' || mediatype.name AS SetupName "
134 "INNER JOIN setup ON filepath.setupid=setup.id "
135 "INNER JOIN platform ON setup.platformid=platform.id "
136 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
138 "ORDER BY SetupName").arg(where);
141 QString DbFilePath::constructSelectById(int id) const
143 return constructSelect(QString("filepath.id = %1").arg(id));
146 QSqlQueryModel* DbFilePath::getData()
148 QSqlQueryModel *model = new QSqlQueryModel;
149 model->setQuery(constructSelect());
150 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
151 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
152 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
153 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
154 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
157 //"platform.name, mediatype.name
158 /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
159 model->setTable(DB_TABLE_NAME_FILEPATH);*/
160 /*model->setRelation(FilePath_PlatformId,
161 QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
162 model->setRelation(FilePath_MediaTypeId,
163 QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"e));*/
164 /*model->setRelation(FilePath_SetupId,
165 QSqlRelation(DB_TABLE_NAME_SETUP, "id", ""));
166 model->setSort(FilePath_Name, Qt::AscendingOrder);*/
168 //model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
169 //model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));