Updated the git clone command.
[emufront] / src / db / dbfile.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QDebug>
22 #include <QSqlRecord>
23 #include <QSqlQuery>
24 #include <QSqlRelationalTableModel>
25 #include "dbfile.h"
26
27
28 DbFile::DbFile(QObject *parent) : DbQueryModelManager(parent)
29 {
30     tableName = DbFile::DB_TABLE_NAME_FILE;
31     type = -1;
32 }
33
34 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
35 {
36     int id = record->value(File_Id).toInt();
37     QString name = record->value(File_Name).toString();
38     QString checksum = record->value(File_CheckSum).toString();
39     int size = record->value(File_FileSize).toInt();
40     int type = record->value(File_FileType).toInt();
41     return new EmuFrontFile(id, name, checksum, size, type);
42 }
43
44 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
45 {
46     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
47     bool ret = false;
48
49     QSqlQuery query;
50     query.prepare(QString("UPDATE file SET"
51                           "name=:name, "
52                           "type=:type, "
53                           "checksum=:checksum, "
54                           "size=:size, "
55                           "updatetime:=updatetime "
56                           "WHERE id=:id"));
57     query.bindValue(":name", plf->getName());
58     query.bindValue(":type", plf->getType());
59     query.bindValue(":checksum", plf->getCheckSum());
60     query.bindValue(":size", plf->getSize());
61     query.bindValue(":updatetime", getCurrentTimeStamp());
62     ret = query.exec();
63     if (ret) resetModel();
64     return ret;
65 }
66
67 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
68 int DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
69 {
70     const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
71     QSqlQuery q;
72     q.prepare("INSERT INTO file "
73               "(id, name, type, checksum, size, updatetime) "
74               "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
75     q.bindValue(":name", fi->getName());
76     q.bindValue(":type", fi->getType());
77     q.bindValue(":checksum", fi->getCheckSum());
78     q.bindValue(":size", fi->getSize());
79     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
80     int id = -1;
81     if (q.exec())
82         id = q.lastInsertId().toInt();
83     return id;
84 }
85
86 // WARNING: this will delete also all the databindings to selected platform
87 // the delete must be confirmed in the UI
88 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
89 {
90     return false;
91 }
92
93 bool DbFile::deleteDataObject(int id) const
94 {
95     if (countDataObjectRefs(id) > 0)
96         // TODO
97         return false;
98     QSqlQuery q;
99     q.prepare(QString("DELETE FROM file WHERE id=:id"));
100     q.bindValue(":id", id);
101     return q.exec();
102 }
103
104 QString DbFile::constructSelect(QString where) const
105 {
106     return QString("SELECT file.id AS FileId, "
107                    "file.name AS Name, "
108                    "file.type AS FileType, "
109                    "file.checksum AS Checksum, "
110                    "file.size AS FileSize, "
111                    "file.updatetime AS UpdateTime "
112                    "FROM file "
113                    "%1 "
114                    "ORDER BY Name").arg(where);
115 }
116
117 QString DbFile::constructFilterById(int id) const
118 {
119     return QString("file.id = %1").arg(id);
120 }
121
122 QString DbFile::constructSelectById(int id) const
123 {
124     return constructSelect(QString("WHERE %1").arg(constructFilterById(id)));
125 }
126
127 QSqlQueryModel* DbFile::getData()
128 {
129     QSqlQueryModel *model = new QSqlQueryModel(this);
130     model->setQuery(constructSelect());
131     model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
132     model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
133     model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
134     model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
135     model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
136     return model;
137 }
138
139 /* Throws EmuFrontException */
140 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
141 {
142     return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
143 }
144
145 QString DbFile::getCountRefsSelect(int id) const
146 {
147     /* files are referenced from platform, mediatype, mediaimagecontainer_mediaimage and mediaimagecontainer. */
148     return QString("SELECT count(*) FROM ("
149               "SELECT file.id FROM file "
150               "INNER JOIN platform ON file.id=platform.fileid "
151               "WHERE file.id=%1 "
152               "UNION ALL "
153               "SELECT file.id FROM file "
154               "INNER JOIN mediatype ON file.id=mediatype.fileid "
155               "WHERE file.id=%1 "
156               "UNION ALL "
157               "SELECT file.id FROM file "
158               "INNER JOIN mediaimagecontainer_mediaimage "
159               "ON (mediaimagecontainerid=file.id OR mediaimageid=file.id) "
160               "WHERE file.id=%1 "
161               ")").arg(id);
162 }