X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/blobdiff_plain/4d221497aee7d8eec52f10479bb4d480c4f14bb8..8af1fb492850ef96d7ba413bdd7be6a5fa63419d:/src/sql/sqlengine.cpp diff --git a/src/sql/sqlengine.cpp b/src/sql/sqlengine.cpp index 54228c3..2d8672c 100644 --- a/src/sql/sqlengine.cpp +++ b/src/sql/sqlengine.cpp @@ -459,6 +459,42 @@ bool SqlEngine::deleteConference(int id) { } +bool SqlEngine::deleteStaleEvents(int conferenceId, QSet 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 existingEventIds; + while (query.next()) existingEventIds.insert(query.value(0).toString()); + + // determine events that are not existing anymore + QSet eventIdsToRemove = existingEventIds.subtract(eventIdsToKeep); + + // delete events including entries from referencing tables + QList 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;