Data object removal implemented in the db layer. UI refreshing added
[emufront] / src / db / dbfile.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 <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlRelationalTableModel>
24 #include "dbfile.h"
25
26
27 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
28 {
29     tableName = DbFile::DB_TABLE_NAME_FILE;
30     type = -1;
31 }
32
33 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
34 {
35     int id = record->value(File_Id).toInt();
36     QString name = record->value(File_Name).toString();
37     QString checksum = record->value(File_CheckSum).toString();
38     int size = record->value(File_FileSize).toInt();
39     int type = record->value(File_FileType).toInt();
40     return new EmuFrontFile(id, name, checksum, size, type);
41 }
42
43 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
44 {
45     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
46     bool ret = false;
47
48     QSqlQuery query;
49     query.prepare(QString("UPDATE file SET"
50                           "name=:name, "
51                           "type=:type, "
52                           "checksum=:checksum, "
53                           "size=:size, "
54                           "updatetime:=updatetime "
55                           "WHERE id=:id"));
56     query.bindValue(":name", plf->getName());
57     query.bindValue(":type", plf->getType());
58     query.bindValue(":checksum", plf->getCheckSum());
59     query.bindValue(":size", plf->getSize());
60     query.bindValue(":updatetime", getCurrentTimeStamp());
61     ret = query.exec();
62     if (ret) resetModel();
63     return ret;
64 }
65
66 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
67 {
68     const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
69     QSqlQuery q;
70     q.prepare("INSERT INTO file "
71               "(id, name, type, checksum, size, updatetime) "
72               "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
73     q.bindValue(":name", fi->getName());
74     q.bindValue(":type", fi->getType());
75     q.bindValue(":checksum", fi->getCheckSum());
76     q.bindValue(":size", fi->getSize());
77     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
78     int id = -1;
79     if (q.exec())
80         id = q.lastInsertId().toInt();
81     return id;
82 }
83
84 /*int DbFile::insertFile(const EmuFrontFile *mi)
85 {
86     qDebug() << "Inserting file " << mi->getName() << " to db.";
87     QSqlQuery q;
88     q.prepare("INSERT INTO file "
89         "(id, name, type, checksum, size, updatetime) "
90         "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
91     q.bindValue(":name", mi->getName());
92     q.bindValue(":type", mi->getType());
93     q.bindValue(":checksum", mi->getCheckSum());
94     q.bindValue(":size", mi->getSize());
95     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
96     int id = -1;
97     if (q.exec())
98         id = q.lastInsertId().toInt();
99    return id;
100
101 }*/
102
103 // WARNING: this will delete also all the databindings to selected platform
104 // the delete must be confirmed in the UI
105 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
106 {
107     return false;
108
109     /*QSqlDatabase::database().transaction();*/
110     //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
111     /*QSqlRecord record = tmodel->record(index->row());
112     int id = record.value(File_Id).toInt();
113     int count = countDataObjectRefs(id);
114     if (count > 0)
115     {
116         QSqlQuery query;
117         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
118         {
119             qDebug() << "Deleting data bindings failed!";
120             QSqlDatabase::database().rollback();
121             return false;
122         }
123     }*/
124     //tmodel->removeRow(index->row());
125     //tmodel->submitAll();
126     //return QSqlDatabase::database().commit();
127 }
128
129 bool DbFile::deleteDataObject(int id) const
130 {
131     if (countDataObjectRefs(id) > 0)
132         // TODO
133         return false;
134     QSqlQuery q;
135     q.prepare(QString("DELETE FROM file WHERE id=:id"));
136     q.bindValue(":id", id);
137     return q.exec();
138 }
139
140 QString DbFile::constructSelect(QString where) const
141 {
142     /*QString where = whereClause.isEmpty()
143                     ? "" : QString("WHERE ").append(whereClause);*/
144
145     return QString("SELECT file.id AS FileId, "
146                    "file.name AS Name, "
147                    "file.type AS FileType, "
148                    "file.checksum AS Checksum, "
149                    "file.size AS FileSize, "
150                    "file.updatetime AS UpdateTime "
151                    "FROM file "
152                    "%1 "
153                    "ORDER BY Name").arg(where);
154 }
155
156 QString DbFile::constructFilterById(int id) const
157 {
158     return QString("file.id = %1").arg(id);
159 }
160
161 QString DbFile::constructSelectById(int id) const
162 {
163     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
164 }
165
166 QSqlQueryModel* DbFile::getData()
167 {
168     QSqlQueryModel *model = new QSqlQueryModel(this);
169     model->setQuery(constructSelect());
170     model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
171     model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
172     model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
173     model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
174     model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
175     return model;
176 }
177
178 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
179 {
180     return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
181 }
182
183 QString DbFile::getCountRefsSelect(int id) const
184 {
185     /* files are referenced from platform, mediatype, mediaimagecontainer_mediaimage and mediaimagecontainer. */
186     return QString("SELECT count(*) FROM ("
187               "SELECT file.id FROM file "
188               "INNER JOIN platform ON file.id=platform.fileid "
189               "WHERE file.id=%1 "
190               "UNION ALL "
191               "SELECT file.id FROM file "
192               "INNER JOIN mediatype ON file.id=mediatype.fileid "
193               "WHERE file.id=%1 "
194               "UNION ALL "
195               "SELECT file.id FROM file "
196               "INNER JOIN mediaimagecontainer_mediaimage "
197               "ON (mediaimagecontainerid=file.id OR mediaimageid=file.id) "
198               "WHERE file.id=%1 "
199               ")").arg(id);
200 }