Restored some abstraction to Databasemanager.
[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
62     /*QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
63     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
64     tmodel->select();
65     if (tmodel->rowCount() == 1)
66     {
67         QSqlRecord record = tmodel->record(0);
68         record.setValue("name", plf->getName());
69         record.setValue("type", plf->getType());
70         record.setValue("checksum", plf->getCheckSum());
71         record.setValue("size", plf->getSize());
72         record.setValue("updatetime", getCurrentTimeStamp());
73         tmodel->setRecord(0, record);
74         ret = tmodel->submitAll();
75     }
76     resetModel();*/
77     return ret;
78 }
79
80 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
81 {
82     const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(ob);
83     QSqlQuery q;
84     q.prepare("INSERT INTO file "
85               "(id, name, type, checksum, size, updatetime) "
86               "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
87     q.bindValue(":name", fi->getName());
88     q.bindValue(":type", fi->getType());
89     q.bindValue(":checksum", fi->getCheckSum());
90     q.bindValue(":size", fi->getSize());
91     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
92     int id = -1;
93     if (q.exec())
94         id = q.lastInsertId().toInt();
95     return id;
96
97     /*int row = 0;
98     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
99     tmodel->insertRows(row, 1);
100     // the null value for index will be set implicitily
101     // when we don't assign any value to cell 0 in the sql table model
102     //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
103     tmodel->setData(sqlTableModel->index(row, File_Name), plf->getName());
104     tmodel->setData(sqlTableModel->index(row, File_FileType), plf->getType());
105     tmodel->setData(sqlTableModel->index(row, File_CheckSum), plf->getCheckSum());
106     tmodel->setData(sqlTableModel->index(row, File_FileSize), plf->getSize());
107     tmodel->setData(sqlTableModel->index(row, File_UpdateTime), getCurrentTimeStamp());
108     return tmodel->submitAll();*/
109 }
110
111 /*int DbFile::insertFile(const EmuFrontFile *mi)
112 {
113     qDebug() << "Inserting file " << mi->getName() << " to db.";
114     QSqlQuery q;
115     q.prepare("INSERT INTO file "
116         "(id, name, type, checksum, size, updatetime) "
117         "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
118     q.bindValue(":name", mi->getName());
119     q.bindValue(":type", mi->getType());
120     q.bindValue(":checksum", mi->getCheckSum());
121     q.bindValue(":size", mi->getSize());
122     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
123     int id = -1;
124     if (q.exec())
125         id = q.lastInsertId().toInt();
126    return id;
127
128 }*/
129
130 int DbFile::countDataObjectRefs(int id) const
131 {
132     return 0; // TODO
133     // return countRows("imagecontainer", "platformid", id);
134 }
135
136 // WARNING: this will delete also all the databindings to selected platform
137 // the delete must be confirmed in the UI
138 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
139 {
140     return false;
141
142     /*QSqlDatabase::database().transaction();*/
143     //QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
144     /*QSqlRecord record = tmodel->record(index->row());
145     int id = record.value(File_Id).toInt();
146     int count = countDataObjectRefs(id);
147     if (count > 0)
148     {
149         QSqlQuery query;
150         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
151         {
152             qDebug() << "Deleting data bindings failed!";
153             QSqlDatabase::database().rollback();
154             return false;
155         }
156     }*/
157     //tmodel->removeRow(index->row());
158     //tmodel->submitAll();
159     //return QSqlDatabase::database().commit();
160 }
161
162 QString DbFile::constructSelect(QString whereClause) const
163 {
164     QString where = whereClause.isEmpty()
165                     ? "" : QString("WHERE ").append(whereClause);
166
167     return QString("SELECT file.id AS FileId, "
168                    "file.name AS Name, "
169                    "file.type AS FileType, "
170                    "file.checksum AS Checksum, "
171                    "file.size AS FileSize, "
172                    "file.updatetime AS UpdateTime "
173                    "FROM file "
174                    "%1 "
175                    "ORDER BY Name").arg(where);
176 }
177
178 QString DbFile::constructSelectById(int id) const
179 {
180     return constructSelect(QString("file.id = %1").arg(id));
181 }
182
183 QSqlQueryModel* DbFile::getData()
184 {
185     QSqlQueryModel *model = new QSqlQueryModel(this);
186     model->setQuery(constructSelect());
187     model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
188     model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
189     model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
190     model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
191     model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
192     return model;
193 }
194
195 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
196 {
197     return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
198 }