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