*/
QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
{
- qDebug() << "Storing media images to database.";
QList<int> ids = QList<int>();
QMapIterator<QString, EmuFrontObject*> it(images);
MediaImage *mi = 0;
{
it.next();
mi = dynamic_cast<MediaImage*>(it.value());
- QString cksum = mi->getCheckSum();
- qDebug() << "Storing media image " << mi->getName()
- << " with checksum " << cksum;
- EmuFrontObject *o = getFileByChecksum(cksum);
- int id = o ? o->getId() : -1;
- if (id >= 0)
- {
- qDebug() << "This media image already exists with id " << id;
-
- // this media image is already stored to db
- // we will append the id of this media image to the list of media images
- // and we will link this media image to the container later
- delete o;
+ int id = insertDataObjectToModel(mi);
+ if (id < 0) {
+ // TODO: Build an error message of failed inserts
+ qDebug() << "Failed inserting media image" << mi->getName();
}
- else if (id < 0)
- {
- qDebug() << "This media image is not yet in the db.";
- id = insertDataObjectToModel(mi);
- if (id < 0)
- {
- // TODO: Build an error message of failed inserts
- qDebug() << "Failed inserting media image" << mi->getName();
- }
- }
- if (id > 0) {
+ else if (id >= 0) {
ids.append(id);
mi->setId(id);
}
throw new EmuFrontException("Cannot install media image "
"container to database without a file path object!");
- // check if this media image container is already in the database
-
// multiple media image containers with matching checksum will be stored
// if each instance is in a different file path
- EmuFrontObject *o = getMediaImageContainerByChecksum(mic->getCheckSum());
- int fileId = o ? o->getId() : -1;
- if (fileId >= 0) {
- qDebug() << "Media image container already in db with id " << fileId << ".";
- // ok, we have a matching file
- MediaImageContainer *tmpMic = dynamic_cast<MediaImageContainer*>(o);
- // this will test if media image container is already in given path (media image container has a path spesific name!)
- QString name = getMediaImageContainerName(mic->getFilePath()->getId(), mic->getId());
- if (name.isEmpty()) {
- // if file path differs, link the existing media image container to this file path with mic->getName() name
- linkMediaImageContainerToPath(mic);
- }
- else if (name != mic->getName()) {
- // if the file path is same but the file name differs update the mediaimagecontainer_filepath table entry
- updateMediaImageContainerToPath(mic);
- }
- delete tmpMic;
- return fileId;
- }
-
+ int fileId = -1;
QMap<QString, EmuFrontObject*> images = mic->getMediaImages();
QList<int> ids = dbMediaImage->storeMediaImages(images);
{
// TODO, for a usual search we need a "light" version of this select
// and MediaImageContainer (only id, name)
- QString select = QString("SELECT file.id, mediaimagecontainer_filepath.mediaimagecontainername, file.checksum, file.size, "
+ QString select = QString("SELECT file.id, file.name, file.checksum, file.size, "
" filepath.id, filepath.name, "
" setup.id, "
" platform.id, platform.name, "
mi = dynamic_cast<MediaImage*>(efo);
qDebug() << "Linking media image container " << micId
<< " to media image " << mi->getId() << ", " << mi->getName() << ".";
- QString name = getMediaImageContainerName(micId, mi->getId());
- if (name.isEmpty() && !linkMediaImageToMediaImageContainer(mi, micId)) {
+ if (!linkMediaImageToMediaImageContainer(mi, micId)) {
throw new EmuFrontException(QString("Failed linking media "
"image container %1 to a media image %2").arg(micId).arg(mi->getId()));
}
- else if (name != mi->getName() && !updateMediaImageToMediaImageContainer(mi, micId)) {
- throw new EmuFrontException(QString("Failed updating media "
- "image container %1 to a media image %2").arg(micId).arg(mi->getId()));
- }
- // else already linked and name is up to date.
}
}
{
QSqlQuery q;
q.prepare("INSERT INTO mediaimagecontainer_filepath "
- "(fileid, filepathid, mediaimagecontainername, updatetime) "
- "VALUES (:fileid, :filepathid, :mediaimagecontainername, :updatetime)");
- q.bindValue(":fileid", mic->getId());
- q.bindValue(":filepathid", mic->getFilePath()->getId());
- q.bindValue(":mediaimagecontainername", mic->getName());
- q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
- return q.exec();
-}
-
-bool DbMediaImageContainer::updateMediaImageContainerToPath(const MediaImageContainer *mic) const
-{
- QSqlQuery q;
- q.prepare("UPDATE mediaimagecontainer_filepath "
- "SET mediaimagecontainername=:mediaimagecontainername, "
- "updatetime=:updatetime "
- "WHERE fileid=:fileid AND filepathid=:filepathid");
+ "(fileid, filepathid, updatetime) "
+ "VALUES (:fileid, :filepathid, :updatetime)");
q.bindValue(":fileid", mic->getId());
q.bindValue(":filepathid", mic->getFilePath()->getId());
- q.bindValue(":mediaimagecontainername", mic->getName());
q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
return q.exec();
}
-QString DbMediaImageContainer::getMediaImageContainerName(int filePathId, int micId) const
-{
- QString name = "";
- QSqlQuery q;
- q.prepare("SELECT mediaimagecontainername FROM mediaimagecontainer_filepath "
- "WHERE fileid=:fileid AND filepathid=:filepathid");
- q.bindValue(":fileid", micId);
- q.bindValue(":filepathid", filePathId);
- q.exec();
- if (q.next())
- name = q.value(0).toString();
- return name;
-}
-
bool DbMediaImageContainer::linkMediaImageToMediaImageContainer(const MediaImage *mi, int micId) const
{
QSqlQuery q;
q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
- "(mediaimagecontainerid, mediaimageid, mediaimagename) "
- "VALUES (:micid, :miid, :miname) ");
- q.bindValue(":micid", micId);
- q.bindValue(":miid", mi->getId());
- q.bindValue(":miname", mi->getName());
- return q.exec();
-}
-
-bool DbMediaImageContainer::updateMediaImageToMediaImageContainer(const MediaImage *mi, int micId) const
-{
- QSqlQuery q;
- q.prepare("UPDATE mediaimagecontainer_mediaimage "
- " SET mediaimagename=:miname "
- " WHERE mediaimagecontainerid=:micid AND mediaimageid=:miid");
+ "(mediaimagecontainerid, mediaimageid) "
+ "VALUES (:micid, :miid) ");
q.bindValue(":micid", micId);
q.bindValue(":miid", mi->getId());
- q.bindValue(":miname", mi->getName());
return q.exec();
}
+++ /dev/null
-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as published by
-// the Free Software Foundation and appearing in the file gpl.txt included in the
-// packaging of this file.
-//
-// EmuFront is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
-
-#include <QSqlRecord>
-#include <QSqlQuery>
-#include <QSqlRelationalTableModel>
-#include "dbmultiinstancefile.h"
-
-DbMultiInstanceFile::DbMultiInstanceFile(QObject *parent) :
- DbFile(parent)
-{
-}
-
-int DbMultiInstanceFile::insertDataObjectToModel(const EmuFrontObject *efo)
-{
- const EmuFrontFile *fi = dynamic_cast<const EmuFrontFile*>(efo);
- QSqlQuery q;
- q.prepare("INSERT INTO file "
- "(id, name, type, checksum, size, updatetime) "
- "VALUES (NULL, "", :type, :checksum, :size, :updatetime)"); // NOTE: in this class it is ok to leave the name field empty!
- q.bindValue(":type", fi->getType());
- q.bindValue(":checksum", fi->getCheckSum());
- q.bindValue(":size", fi->getSize());
- q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
- int id = -1;
- if (q.exec())
- id = q.lastInsertId().toInt();
- return id;
-}