deleteAndFetchStatement(_,selection:) with ColumnsProvider

This commit is contained in:
Gwendal Roué committed 2025-05-10 09:36:14 +02:00
1 parent ff15f30a3b
commit e7fdd65072
2 files changed
+74 -2

No files matched your search

@@ -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:
/// <https://www.sqlite.org/lang_returning.html#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:
/// <https://www.sqlite.org/lang_returning.html#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.
///
+1 -1
View File
@@ -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"])
}