Implemented initial deleteDataObject(id) in Db-layer. Implemented
authorMikko Keinänen <mikko.keinanen@gmail.com>
Mon, 27 Sep 2010 20:01:17 +0000 (23:01 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Mon, 27 Sep 2010 20:01:17 +0000 (23:01 +0300)
linking of media image container with media images. Refactored code in
DbMediaImageContainer. NOT TESTED yet.

doc/uml-db_layer.dia
src/db/dbemufrontfileobject.cpp
src/db/dbemufrontfileobject.h
src/db/dbfile.cpp
src/db/dbfile.h
src/db/dbfilepath.cpp
src/db/dbfilepath.h
src/db/dbmediaimagecontainer.cpp
src/db/dbquerymodelmanager.h
src/db/dbsetup.cpp
src/db/dbsetup.h

index aa2bcbc..7191859 100644 (file)
Binary files a/doc/uml-db_layer.dia and b/doc/uml-db_layer.dia differ
index fa662ee..0193e66 100644 (file)
@@ -72,8 +72,9 @@ int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
     QSqlQuery query;
-    query.prepare(QString("INSERT INTO %1 (id, name, fileid) "
-        "VALUES (NULL, :name, :fileid) ").arg(tableName));
+    query.prepare(QString("INSERT INTO :table (id, name, fileid) "
+        "VALUES (NULL, :name, :fileid) "));
+    query.bindValue(":table", tableName);
     query.bindValue(":name", plf->getName());
     if (plf->getFile())
         query.bindValue(":fileid", plf->getFile()->getId());
@@ -87,6 +88,18 @@ int DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
     return id;
 }
 
+bool DbEmuFrontFileObject::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM :table WHERE id=:id"));
+    q.bindValue(":table", tableName);
+    q.bindValue(":id", id);
+    return q.exec();
+}
+
 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
 {
     return 0; // TODO
index e9535d4..ae981ff 100644 (file)
@@ -49,7 +49,8 @@ protected:
    virtual QString constructFilterById(int id) const;
    virtual EmuFrontObject* recordToDataObject(const QSqlRecord* );
    QString tableName;
-    virtual EmuFrontObject* createEmuFrontFileObject(int id, QString name, EmuFrontFile *f) = 0;
+   virtual EmuFrontObject* createEmuFrontFileObject(int id, QString name, EmuFrontFile *f) = 0;
+   virtual bool deleteDataObject(int id) const;
 
 private:
     virtual QSqlQueryModel* getData();
index 7bf64e3..950a0c3 100644 (file)
@@ -130,6 +130,17 @@ bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
     //return QSqlDatabase::database().commit();
 }
 
+bool DbFile::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM file WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
+}
+
 QString DbFile::constructSelect(QString whereClause) const
 {
     QString where = whereClause.isEmpty()
index 2ce3172..80144b3 100644 (file)
@@ -47,6 +47,7 @@ protected:
     virtual QString constructSelectById(int id) const;
     virtual QString constructFilterById(int id) const;
     virtual QString constructSelect(QString whereClause = "") const;
+    virtual bool deleteDataObject(int id) const;
     int type;
 
 private:
index a86da93..0c4285a 100644 (file)
@@ -89,6 +89,17 @@ bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
     return false;
 }
 
+bool DbFilePath::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
+}
+
 QString DbFilePath::constructSelect(QString whereClause) const
 {
     QString where = whereClause.isEmpty()
index 50d0cae..9448074 100644 (file)
@@ -43,6 +43,7 @@ protected:
     virtual QString constructSelectById(int id) const;
     virtual QString constructFilterById(int id) const;
     virtual QString constructSelect(QString whereClause = "") const;
+    virtual bool deleteDataObject(int id) const;
 
 private:
     virtual QSqlQueryModel* getData();
index 0f68a96..0705f3c 100644 (file)
@@ -39,56 +39,64 @@ bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
 
 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
 {
-    /* "CREATE TABLE IF NOT EXISTS mediaimagecontainer "
-                        "(id INTEGER PRIMARY KEY, "
-                        "fileid INTEGER REFERENCES file(id), "
-                        "filepathid INTEGER REFERENCES filepath(id), "
-                        "updatetime NUMERIC)"*/
-
     const MediaImageContainer *mic
         = dynamic_cast<const MediaImageContainer *>(efo);
 
+    // check if this media image container is already in the database
+    int fileId = -1;
+    if ((fileId = getMediaImageContainer(mic->getCheckSum())) >= 0)
+        return fileId;
+
     if (!mic->getFilePath())
-        // TODO: note we most surely need to catch the exception
-        // in the calling code block and clean
-        // all the media image and ...containers from
-        // the memory!
         throw new EmuFrontException("Cannot install media image "
             "container to database without a file path object!");
 
-    // Insert MediaImageContainer first as a EmuFrontFile object to file table.
-    // File id is used to store the media image container instance to database,
-    // file id is also the media image container id
-    int fileId = DbFile::insertDataObjectToModel(mic);
-
-    if (fileId < 0) {
-        // TODO: note we most surely need to catch the exception
-        // in the calling code block and clean
-        // all the media image and ...containers from
-        // the memory!
-        throw new EmuFrontException(
-                QString(tr("Inserting media image container %1 to file database failed"))
-                .arg(mic->getName()));
-    }
+    QList<MediaImage*> images = mic->getMediaImages();
+    QList<int> ids = dbMediaImage->storeMediaImages(images);
 
-    // Insert to mediaimagecontainer table
+    if (ids.count() <= 0)
+        return -1;
+
+    /* Contained Media images successfully stored to db,
+        storing media image container also */
+
+    try {
+
+        // Insert MediaImageContainer first as a EmuFrontFile object to file table.
+
+        // File id is used to store the media image container instance to database,
+        // file id is also the media image container id
+        fileId = DbFile::insertDataObjectToModel(mic);
+
+        if (fileId < 0) {
+            // TODO: note we most surely need to catch the exception
+            // in the calling code block and clean
+            // all the media image and ...containers from
+            // the memory!
+            throw new EmuFrontException(
+                    QString(tr("Inserting media image container %1 to file database failed"))
+                    .arg(mic->getName()));
+        }
+
+        // Insert to mediaimagecontainer table
+
+        QSqlQuery q;
+        q.prepare("INSERT INTO mediaimagecontainer "
+                  "(fileid, filepathid, updatetime) "
+                  "VALUES (:fileid, :filepathid, :updatetime)");
+        q.bindValue(":fileid", fileId);
+        q.bindValue(":filepathid", mic->getFilePath()->getId());
+        q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
+        if (!q.exec()){
+            DbFile::deleteDataObject(fileId);
+            throw new EmuFrontException("Failed inserting media image to database!");
+        }
+        linkMediaImagesWithContainer(fileId, ids);
+    } catch (EmuFrontException e) {
+        dbMediaImage->removeOrphanedMediaImages(ids);
+        throw e;
+    }
 
-    QSqlQuery q;
-    q.prepare("INSERT INTO mediaimagecontainer "
-        "(fileid, filepathid, updatetime) "
-        "VALUES (:fileid, :filepathid, :updatetime)");
-    q.bindValue(":fileid", fileId);
-    q.bindValue(":filepathid", mic->getFilePath()->getId());
-    q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
-    if (!q.exec())
-        // TODO: failed inserting, remove orphaned media images
-        // (this is actually done in the storeContainers catch block
-        // but maybe it should be done here also, if this function is called independently!
-        // TODO: note we most surely need to catch the exception
-        // in the calling code block and clean
-        // all the media image and ...containers from
-        // the memory!
-        throw new EmuFrontException("Failed inserting media image to database!");
     return fileId;
 }
 
@@ -134,6 +142,7 @@ QSqlQueryModel* DbMediaImageContainer::getData()
     return 0;
 }
 
+/* Returns the id of a media image container with a given cheksum or -1 if not found */
 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
 {
     // TODO
@@ -151,47 +160,26 @@ void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, Fi
     foreach(MediaImageContainer *mic, lst)
     {
         qDebug() << "Media image container " << mic->getName();
-        QList<MediaImage*> images = mic->getMediaImages();
-
-        /* If media image container is already in the db, continue */
-        if (getMediaImageContainer(mic->getCheckSum()) >= 0)
-            continue;
-
-        // this is a new media image container, lets build a list
-        // of media image id's for this container
-        QList<int> ids = dbMediaImage->storeMediaImages(images);
-
-        if (ids.count() > 0)
-        {
-            try {
-                // mediaimagecontainer table: id, fileid, filepathid, updatetime
-
-                // insert the media image container file to file table
-                int micFileId = insertDataObjectToModel(mic);
-                if (micFileId < 0) {
-                    // TODO: note we most surely need to catch the exception
-                    // in the calling code block and clean
-                    // all the media image and ...containers from
-                    // the memory!
-                    throw new EmuFrontException(
-                        QString(tr("Inserting media image container %1 to file database failed"))
-                            .arg(mic->getName()));
-                }
-                // link all the media image ids in list to media image container id
-                linkMediaImagesWithContainer(micFileId, ids);
-            } catch (EmuFrontException e) {
-                // need to remove the media images without media image container in list 'ids'
-                    // TODO: clean
-                    // all the media image and ...containers from
-                    // the memory! (maybe throw another exception on calling code block for this)
-
-                dbMediaImage->removeOrphanedMediaImages(ids);
-            }
-        }
+        int micFileId = insertDataObjectToModel(mic);
     }
 }
 
 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
 {
-    // TODO
+    if (micId < 0 || miIds.count() <= 0)
+        return;
+
+    QSqlQuery q;
+    q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
+        "(mediaimagecontainerid, mediaimageid) "
+        "VALUES (:micid, :miid) ");
+    q.bindValue(":micid", micId);
+
+    foreach(int miid, miIds) {
+        q.bindValue(":miid", miid);
+        if (!q.exec()) {
+            throw new EmuFrontException(QString("Failed linking media "
+                "image container %1 to a media image %2").arg(micId).arg(miid));
+        }
+    }
 }
index 5fe95f4..66c1e92 100644 (file)
@@ -33,6 +33,7 @@ protected:
     virtual QString constructSelectById(int id) const = 0;
     virtual QString constructFilterById(int id) const = 0;
     virtual QString constructSelect(QString whereClause = "") const = 0;
+    virtual bool deleteDataObject(int id) const = 0;
 };
 
 #endif // DBQUERYMODELMANAGER_H
index a8e7e75..443fb44 100644 (file)
@@ -131,6 +131,17 @@ bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
     return false;
 }
 
+bool DbSetup::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM setup WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
+}
+
 QSqlQueryModel* DbSetup::getData()
 {
     QSqlQueryModel *model = new QSqlQueryModel;
index b0c558e..775fdab 100644 (file)
@@ -45,6 +45,7 @@ protected:
     virtual QString constructSelectById(int id) const;
     virtual QString constructFilterById(int id) const;
     virtual QString constructSelect(QString whereClause = "") const;
+    virtual bool deleteDataObject(int id) const;
 
 private:
     virtual QSqlQueryModel* getData();