From e7fdd65072602c9405dc589d64ae505954100cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 12 Apr 2025 18:27:12 +0200 Subject: [PATCH] deleteAndFetchStatement(_,selection:) with ColumnsProvider --- .../Request/QueryInterfaceRequest.swift | 74 ++++++++++++++++++- Tests/GRDBTests/TableRecordDeleteTests.swift | 2 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 773a226a6..46700f346 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -586,6 +586,42 @@ extension QueryInterfaceRequest { return try SQLQueryGenerator(relation: relation).makeDeleteStatement(db, selection: selection) } + /// Returns a `DELETE RETURNING` prepared statement. + /// + /// For example: + /// + /// ```swift + /// struct Player: TableRecord { + /// enum Columns { + /// static let name = Column("name") + /// } + /// } + /// + /// // Delete all players and return their names + /// // DELETE FROM player RETURNING name + /// let request = Player.all() + /// let statement = try request.deleteAndFetchStatement(db, selection: \.name) + /// let deletedNames = try String.fetchSet(statement) + /// ``` + /// + /// - important: Make sure you check the documentation of the `RETURNING` + /// clause, which describes important limitations and caveats: + /// . + /// + /// - parameter db: A database connection. + /// - parameter selection: The returned columns (must not be empty). + /// - returns: A prepared statement. + /// - throws: A ``DatabaseError`` whenever an SQLite error occurs. + /// - precondition: `selection` is not empty. + public func deleteAndFetchStatement( + _ db: Database, + selection: (RowDecoder.ColumnsProvider) -> any SQLSelectable) + throws -> Statement + where RowDecoder: TableRecord + { + try deleteAndFetchStatement(db, selection: [selection(RowDecoder.columns)]) + } + /// Returns a cursor over the records deleted by a /// `DELETE RETURNING` statement. /// @@ -702,7 +738,6 @@ extension QueryInterfaceRequest { return try RowDecoder.ID.fetchSet(statement) } #else - // TODO: provide a ColumnsProvider version /// Returns a `DELETE RETURNING` prepared statement. /// /// For example: @@ -734,6 +769,43 @@ extension QueryInterfaceRequest { return try SQLQueryGenerator(relation: relation).makeDeleteStatement(db, selection: selection) } + /// Returns a `DELETE RETURNING` prepared statement. + /// + /// For example: + /// + /// ```swift + /// struct Player: TableRecord { + /// enum Columns { + /// static let name = Column("name") + /// } + /// } + /// + /// // Delete all players and return their names + /// // DELETE FROM player RETURNING name + /// let request = Player.all() + /// let statement = try request.deleteAndFetchStatement(db, selection: \.name) + /// let deletedNames = try String.fetchSet(statement) + /// ``` + /// + /// - important: Make sure you check the documentation of the `RETURNING` + /// clause, which describes important limitations and caveats: + /// . + /// + /// - parameter db: A database connection. + /// - parameter selection: The returned columns (must not be empty). + /// - returns: A prepared statement. + /// - throws: A ``DatabaseError`` whenever an SQLite error occurs. + /// - precondition: `selection` is not empty. + @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+ + public func deleteAndFetchStatement( + _ db: Database, + selection: (RowDecoder.ColumnsProvider) -> any SQLSelectable) + throws -> Statement + where RowDecoder: TableRecord + { + try deleteAndFetchStatement(db, selection: [selection(RowDecoder.columns)]) + } + /// Returns a cursor over the records deleted by a /// `DELETE RETURNING` statement. /// diff --git a/Tests/GRDBTests/TableRecordDeleteTests.swift b/Tests/GRDBTests/TableRecordDeleteTests.swift index 3935f97f4..23a0357ce 100644 --- a/Tests/GRDBTests/TableRecordDeleteTests.swift +++ b/Tests/GRDBTests/TableRecordDeleteTests.swift @@ -245,7 +245,7 @@ class TableRecordDeleteTests: GRDBTestCase { try dbQueue.inDatabase { db in do { let request = Person.all() - let statement = try request.deleteAndFetchStatement(db, selection: [Column("name")]) + let statement = try request.deleteAndFetchStatement(db, selection: \.name) XCTAssertEqual(statement.sql, "DELETE FROM \"persons\" RETURNING \"name\"") XCTAssertEqual(statement.columnNames, ["name"]) }