Repository: Automattic/pocket-casts-ios
File: Modules/DataModel/Sources/PocketCastsDataModel/Private/Managers/PlaylistDataManager.swift (Swift)

Code before:
        dbQueue.write { db in
            do {
                let inClause = DataHelper.convertArrayToInString(episodeUuids)
                try db.executeUpdate("DELETE FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? AND episodeUuid IN (\(inClause))", values: [playlist.uuid])
                let removedCount = db.changes
                if removedCount == 0 { return }

                // Reindex remaining
                let rs = try db.executeQuery("SELECT id FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? ORDER BY episodePosition ASC", values: [playlist.uuid])
                defer { rs.close() }
                var ids = [Int64]()
                while rs.next() { ids.append(rs.longLongInt(forColumn: "id")) }
                for (index, id) in ids.enumerated() {
                    try db.executeUpdate("UPDATE \(DataManager.playlistEpisodeTableName) SET episodePosition = ? WHERE id = ?", values: [index, id])
                }

                playlist.syncStatus = SyncStatus.notSynced.rawValue
                try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET syncStatus = ? WHERE uuid = ?", values: [playlist.syncStatus, playlist.uuid])
            } catch {
                FileLog.shared.addMessage("PlaylistDataManager.deleteEpisodes error: \(error)")
            }
        }
    }

    /// Delete all playlist-episode relationships for the given playlist
    func deleteAllEpisodes(in playlist: EpisodeFilter, dbQueue: PCDBQueue) {
        dbQueue.write { db in
            do {
                try db.executeUpdate("DELETE FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? OR playlist_id = ?", values: [playlist.uuid, playlist.id])

                let removedCount = db.changes
                if removedCount > 0 {
                    playlist.syncStatus = SyncStatus.notSynced.rawValue
                    try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET syncStatus = ? WHERE uuid = ?", values: [playlist.syncStatus, playlist.uuid])
                }
            } catch {
                FileLog.shared.addMessage("PlaylistDataManager.deleteAllEpisodes error: \(error)")
            }
        }
    }

    func save(playlist: EpisodeFilter, dbQueue: PCDBQueue) {
        dbQueue.write { db in
            do {
                if playlist.id == 0 {
                    playlist.id = DBUtils.generateUniqueId()
                    try db.executeUpdate("INSERT INTO \(DataManager.playlistsTableName) (\(self.columnNames.joined(separator: ","))) VALUES \(DBUtils.valuesQuestionMarks(amount: self.columnNames.count))", values: self.createValuesFrom(playlist: playlist))
                } else {
                    let setStatement = "\(self.columnNames.joined(separator: " = ?, ")) = ?"
                    try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET \(setStatement) WHERE uuid = ?", values: self.createValuesFrom(playlist: playlist, includeUuidForWhere: true))
                }

Review comment (suggestion):
This is ugly as hell.

But for this user case we don't want to mark playlist as unsynced and do all the work the other method does. We just want to delete this playlist.

Changing the current method is not a good choice so I'm just going with this one for now.

Code after:
        dbQueue.write { db in
            do {
                let inClause = DataHelper.convertArrayToInString(episodeUuids)
                try db.executeUpdate("DELETE FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? AND episodeUuid IN (\(inClause))", values: [playlist.uuid])
                let removedCount = db.changes
                if removedCount == 0 { return }

                // Reindex remaining
                let rs = try db.executeQuery("SELECT id FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? ORDER BY episodePosition ASC", values: [playlist.uuid])
                defer { rs.close() }
                var ids = [Int64]()
                while rs.next() { ids.append(rs.longLongInt(forColumn: "id")) }
                for (index, id) in ids.enumerated() {
                    try db.executeUpdate("UPDATE \(DataManager.playlistEpisodeTableName) SET episodePosition = ? WHERE id = ?", values: [index, id])
                }

                playlist.syncStatus = SyncStatus.notSynced.rawValue
                try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET syncStatus = ? WHERE uuid = ?", values: [playlist.syncStatus, playlist.uuid])
            } catch {
                FileLog.shared.addMessage("PlaylistDataManager.deleteEpisodes error: \(error)")
            }
        }
    }

    /// Just delete episodes from a playlist and nothing more
    func rawDeleteEpisodes(_ episodeUuids: [String], from playlist: EpisodeFilter, dbQueue: PCDBQueue) {
        guard !episodeUuids.isEmpty else { return }
        dbQueue.write { db in
            do {
                let inClause = DataHelper.convertArrayToInString(episodeUuids)
                try db.executeUpdate("DELETE FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? AND episodeUuid IN (\(inClause))", values: [playlist.uuid])
            } catch {
                FileLog.shared.addMessage("PlaylistDataManager.rawDeleteEpisodes error: \(error)")
            }
        }
    }

    /// Delete all playlist-episode relationships for the given playlist
    func deleteAllEpisodes(in playlist: EpisodeFilter, dbQueue: PCDBQueue) {
        dbQueue.write { db in
            do {
                try db.executeUpdate("DELETE FROM \(DataManager.playlistEpisodeTableName) WHERE playlist_uuid = ? OR playlist_id = ?", values: [playlist.uuid, playlist.id])

                let removedCount = db.changes
                if removedCount > 0 {
                    playlist.syncStatus = SyncStatus.notSynced.rawValue
                    try db.executeUpdate("UPDATE \(DataManager.playlistsTableName) SET syncStatus = ? WHERE uuid = ?", values: [playlist.syncStatus, playlist.uuid])
                }
            } catch {
                FileLog.shared.addMessage("PlaylistDataManager.deleteAllEpisodes error: \(error)")
            }