]> ToastFreeware Gitweb - toast/confclerk.git/blobdiff - src/sql/sqlengine.cpp
Stale events are deleted now after reloading conference.
[toast/confclerk.git] / src / sql / sqlengine.cpp
index 54228c35742a4cb90044fe294ce0fb911f4c987e..2d8672c61e9aa111edde6f7fc354d7303630511c 100644 (file)
@@ -459,6 +459,42 @@ bool SqlEngine::deleteConference(int id) {
 }
 
 
+bool SqlEngine::deleteStaleEvents(int conferenceId, QSet<QString> eventIdsToKeep) {
+    QSqlQuery query(db);
+
+    // get all event IDs from conference
+    query.prepare("SELECT id FROM event WHERE xid_conference = ?");
+    query.bindValue(0, conferenceId);
+    bool success = query.exec();
+    emitSqlQueryError(query);
+    if (!success) return false;
+    QSet<QString> existingEventIds;
+    while (query.next()) existingEventIds.insert(query.value(0).toString());
+
+    // determine events that are not existing anymore
+    QSet<QString> eventIdsToRemove = existingEventIds.subtract(eventIdsToKeep);
+
+    // delete events including entries from referencing tables
+    QList<QString> tables = {"link", "event_room", "event_person"};
+    for(const QString& eventId: eventIdsToRemove) {
+        for(const QString& table: tables) {
+            query.prepare(QString("DELETE FROM %1 WHERE xid_conference = ? AND xid_event = ?").arg(table));
+            query.bindValue(0, conferenceId);
+            query.bindValue(1, eventId);
+            success &= query.exec();
+            emitSqlQueryError(query);
+        }
+        query.prepare("DELETE FROM event WHERE xid_conference = ? AND id = ?");
+        query.bindValue(0, conferenceId);
+        query.bindValue(1, eventId);
+        success &= query.exec();
+        emitSqlQueryError(query);
+    }
+
+    return success;
+}
+
+
 void SqlEngine::emitSqlQueryError(const QSqlQuery &query) {
     QSqlError error = query.lastError();
     if (error.type() == QSqlError::NoError) return;