Cleaned up a bit after reorginizing db functionality.
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 22 May 2010 15:51:28 +0000 (18:51 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 22 May 2010 15:51:28 +0000 (18:51 +0300)
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbplatform.cpp
src/db/dbplatform.h
src/dialogs/platformdialog.cpp

index 29937fc..28102e7 100644 (file)
@@ -35,7 +35,11 @@ DatabaseManager::DatabaseManager(QObject *parent)
 {}
 
 DatabaseManager::~DatabaseManager()
-{}
+{
+    // no need to explicitily destroy sqlTableModel
+    // because it is parented QObject and will
+    // be destroyed when parent is destroyed
+}
 
 bool DatabaseManager::openDB()
 {
@@ -44,16 +48,11 @@ bool DatabaseManager::openDB()
     return db.open();
 }
 
-/*QSqlError DatabaseManager::lastError()
+bool DatabaseManager::deleteDB()
 {
-       return db.lastError();
-}*/
-
-/*bool DatabaseManager::deleteDB()
-{
-       db.close();
-       return QFile::remove(getDbPath());
-}*/
+    // return QFile::remove(getDbPath());
+    return false;
+}
 
 QString DatabaseManager::getDbPath()
 {
@@ -94,7 +93,7 @@ bool DatabaseManager::createDB()
        return ret;
 }
 
-void DatabaseManager::resetModel()
+void DatabaseManager::resetModel() const
 {
     if (!sqlTableModel) return;
     sqlTableModel->setFilter("");
index 80926ac..aeb2b65 100644 (file)
@@ -37,9 +37,8 @@ public:
     static bool openDB();
     static bool deleteDB();
     static bool dbExists();
-    static QSqlError lastError();
     static bool createDB();
-    void resetModel();
+    void resetModel() const;
 
 protected:
     QSqlTableModel *sqlTableModel;
index 4a71d07..7d4db6b 100644 (file)
@@ -41,8 +41,7 @@ Platform* DbPlatform::getPlatformFromModel(QModelIndex *index)
     int id = record.value(Platform_Id).toInt();
     QString name = record.value(Platform_Name).toString();
     QString fileName = record.value(Platform_Filename).toString();
-    qDebug() << "Name " << name << " id " << id;
-    //EmuFrontObject *plf = new Platform(id, name, fileName);
+    //qDebug() << "Got platform Name " << name << " id " << id;
     return new Platform(id, name, fileName);
 }
 
@@ -65,37 +64,43 @@ bool DbPlatform::updatePlatformToModel(const Platform *ob)
 
 bool DbPlatform::insertPlatformToModel(const Platform *ob)
 {
-    qDebug() << "Inserting platform " << ob->getName();
     int row = 0;
     sqlTableModel->insertRows(row, 1);
+    // the null value for index will be set implicitily
+    // when we don't assign any value to cell 0 in the sql table model
     //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
     sqlTableModel->setData(sqlTableModel->index(row, 1), ob->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, 2),
-                            ob->getFilename());
+    sqlTableModel->setData(sqlTableModel->index(row, 2), ob->getFilename());
     return sqlTableModel->submitAll();
 }
 
+int DbPlatform::countPlatformBindings(int id) const
+{
+    int numEntries = 0;
+    QSqlQuery query(QString("SELECT COUNT(*) FROM imagecontainer WHERE platformid = %1").arg(id));
+    if (query.next())
+        numEntries = query.value(0).toInt();
+    return numEntries;
+}
+
+// WARNING: this will delete also all the databindings to selected platform
 bool DbPlatform::deletePlatformFromModel(QModelIndex *index)
 {
     QSqlDatabase::database().transaction();
     QSqlRecord record = sqlTableModel->record(index->row());
     int id = record.value(Platform_Id).toInt();
-    /*int numEntries = 0;
-    QSqlQuery query(QString("SELECT COUNT(*) FROM imagecontainer WHERE platformid = %1").arg(id));
-    if (query.next())
-        numEntries = query.value(0).toInt();
-    if (numEntries > 0)
+    qDebug() << "Deleting platform " << id;
+    int count = countPlatformBindings(id);
+    if (count > 0)
     {
-        int r = QMessageBox::warning(this, tr("Delete platform"),
-                                     QString("Do you want to delete platform %1 and all the related data?")
-                                     .arg(record.value(Platform_Name).toString()), QMessageBox::Yes | QMessageBox::No);
-        if ( r == QMessageBox::No )
+        QSqlQuery query;
+        if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
         {
+            qDebug() << "Deleting data bindings failed!";
             QSqlDatabase::database().rollback();
             return false;
         }
-        query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id));
-    }*/
+    }
     sqlTableModel->removeRow(index->row());
     sqlTableModel->submitAll();
     return QSqlDatabase::database().commit();
index bed51e4..ce58f23 100644 (file)
@@ -34,6 +34,7 @@ public:
     bool updatePlatformToModel(const Platform *);
     bool insertPlatformToModel(const Platform *);
     bool deletePlatformFromModel(QModelIndex*);
+    int countPlatformBindings(int) const;
 
 private:
     enum {
index 94fceea..1fc1576 100644 (file)
@@ -122,11 +122,41 @@ void PlatformDialog::insertDb(const EmuFrontObject *ob) const
 
 bool PlatformDialog::deleteItem()
 {
+    qDebug() << "PlatformDialog::deleteItem()";
     QModelIndex index = objectList->currentIndex();
     if (!index.isValid()) return false;
-    (dynamic_cast<DbPlatform *>(dbManager))->deletePlatformFromModel(&index);
+
+    qDebug() << "Index is valid";
+
+    // TODO: when implementing data bindings to platform
+    // we need to check if platform being removed has bindings
+    // and a) ask user if this platform should be removed
+    // b) remove all the data associated to this platform
+
+    Platform *plf = dynamic_cast<DbPlatform*>(dbManager)->getPlatformFromModel(&index);
+    if (!plf) return false;
+
+    qDebug() << "Got platform" << plf->getName();
+
+    int numBindings = dynamic_cast<DbPlatform*>(dbManager)->countPlatformBindings(plf->getId());
+    if (numBindings > 0)
+    {
+        qDebug() << "Got " << numBindings << " bindings";
+        int r = QMessageBox::warning(this, tr("Delete platform"),
+                                     QString("Do you really want to delete platform %1 with %2 data bindings?")
+                                     .arg(plf->getName()).arg(numBindings),
+                                     QMessageBox::Yes | QMessageBox::No);
+        if ( r == QMessageBox::No )
+            return false;
+    }
+    delete plf;
+    bool delOk = (dynamic_cast<DbPlatform *>(dbManager))->deletePlatformFromModel(&index);
+    if (!delOk)
+    {
+        qDebug() << "delete failed";
+        return false;
+    }
     updateList();
     objectList->setFocus();
     return false;
 }
-