Another SQL error fixed.
[emufront] / src / db / dbmediaimagecontainer.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 <QSqlError>
25 #include "dbmediaimagecontainer.h"
26
27 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
28     : DbFile(parent) // DbQueryModelManager(parent)
29 {
30     dbMediaImage = new DbMediaImage(parent);
31     //dbFile = new DbFile(parent);
32 }
33
34 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
35 {
36     // TODO
37     return false;
38 }
39
40 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
41 {
42     /* "CREATE TABLE IF NOT EXISTS mediaimagecontainer "
43                         "(id INTEGER PRIMARY KEY, "
44                         "fileid INTEGER REFERENCES file(id), "
45                         "filepathid INTEGER REFERENCES filepath(id), "
46                         "updatetime NUMERIC)"*/
47
48     const MediaImageContainer *mic
49         = dynamic_cast<const MediaImageContainer *>(efo);
50
51     if (!mic->getFilePath())
52         // TODO: note we most surely need to catch the exception
53         // in the calling code block and clean
54         // all the media image and ...containers from
55         // the memory!
56         throw new EmuFrontException("Cannot install media image "
57             "container to database without a file path object!");
58
59     // Insert MediaImageContainer first as a EmuFrontFile object to file table.
60     // File id is used to store the media image container instance to database,
61     // file id is also the media image container id
62     int fileId = DbFile::insertDataObjectToModel(mic);
63
64     if (fileId < 0) {
65         // TODO: note we most surely need to catch the exception
66         // in the calling code block and clean
67         // all the media image and ...containers from
68         // the memory!
69         throw new EmuFrontException(
70                 QString(tr("Inserting media image container %1 to file database failed"))
71                 .arg(mic->getName()));
72     }
73
74     // Insert to mediaimagecontainer table
75
76     QSqlQuery q;
77     q.prepare("INSERT INTO mediaimagecontainer "
78         "(fileid, filepathid, updatetime) "
79         "VALUES (:fileid, :filepathid, :updatetime)");
80     q.bindValue(":fileid", fileId);
81     q.bindValue(":filepathid", mic->getFilePath()->getId());
82     q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
83     if (!q.exec())
84         // TODO: failed inserting, remove orphaned media images
85         // (this is actually done in the storeContainers catch block
86         // but maybe it should be done here also, if this function is called independently!
87         // TODO: note we most surely need to catch the exception
88         // in the calling code block and clean
89         // all the media image and ...containers from
90         // the memory!
91         throw new EmuFrontException("Failed inserting media image to database!");
92     return fileId;
93 }
94
95 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
96 {
97     // TODO
98     return false;
99 }
100
101 int DbMediaImageContainer::countDataObjectRefs(int id) const
102 {
103     // TODO
104     return -1;
105 }
106
107 QString DbMediaImageContainer::constructSelect(QString whereClause) const
108 {
109     // TODO
110     return "";
111 }
112
113 QString DbMediaImageContainer::constructFilterById(int id) const
114 {
115     // TODO
116     return "";
117 }
118
119 QString DbMediaImageContainer::constructSelectById(int id) const
120 {
121     // TODO
122     return "";
123 }
124
125 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *)
126 {
127     // TODO
128     return 0;
129 }
130
131 QSqlQueryModel* DbMediaImageContainer::getData()
132 {
133     // TODO
134     return 0;
135 }
136
137 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
138 {
139     // TODO
140     return -1;
141 }
142
143
144 /**
145 * Stores media image containers, including the media images included
146 * to database.
147 */
148 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
149 {
150     qDebug() << "Storing media image containers to database.";
151     foreach(MediaImageContainer *mic, lst)
152     {
153         qDebug() << "Media image container " << mic->getName();
154         QList<MediaImage*> images = mic->getMediaImages();
155
156         /* If media image container is already in the db, continue */
157         if (getMediaImageContainer(mic->getCheckSum()) >= 0)
158             continue;
159
160         // this is a new media image container, lets build a list
161         // of media image id's for this container
162         QList<int> ids = dbMediaImage->storeMediaImages(images);
163
164         if (ids.count() > 0)
165         {
166             try {
167                 // mediaimagecontainer table: id, fileid, filepathid, updatetime
168
169                 // insert the media image container file to file table
170                 int micFileId = insertDataObjectToModel(mic);
171                 if (micFileId < 0) {
172                     // TODO: note we most surely need to catch the exception
173                     // in the calling code block and clean
174                     // all the media image and ...containers from
175                     // the memory!
176                     throw new EmuFrontException(
177                         QString(tr("Inserting media image container %1 to file database failed"))
178                             .arg(mic->getName()));
179                 }
180                 // link all the media image ids in list to media image container id
181                 linkMediaImagesWithContainer(micFileId, ids);
182             } catch (EmuFrontException e) {
183                 // need to remove the media images without media image container in list 'ids'
184                     // TODO: clean
185                     // all the media image and ...containers from
186                     // the memory! (maybe throw another exception on calling code block for this)
187
188                 dbMediaImage->removeOrphanedMediaImages(ids);
189             }
190         }
191     }
192 }
193
194 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
195 {
196     // TODO
197 }