DatabasePromise is Sendable

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent 735175e902
commit 05899228fd
5 files changed
+53 -29

No files matched your search

@@ -173,7 +173,9 @@ extension Association {
// SelectionRequest conformance // SelectionRequest conformance
extension Association { extension Association {
public func selectWhenConnected(_ selection: @escaping (Database) throws -> [any SQLSelectable]) -> Self { public func selectWhenConnected(
_ selection: @escaping @Sendable (Database) throws -> [any SQLSelectable]
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.selectWhenConnected { db in relation = relation.selectWhenConnected { db in
try selection(db).map(\.sqlSelection) try selection(db).map(\.sqlSelection)
@@ -181,7 +183,9 @@ extension Association {
} }
} }
public func annotatedWhenConnected(with selection: @escaping (Database) throws -> [any SQLSelectable]) -> Self { public func annotatedWhenConnected(
with selection: @escaping @Sendable (Database) throws -> [any SQLSelectable]
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.annotatedWhenConnected { db in relation = relation.annotatedWhenConnected { db in
try selection(db).map(\.sqlSelection) try selection(db).map(\.sqlSelection)
@@ -192,7 +196,9 @@ extension Association {
// FilteredRequest conformance // FilteredRequest conformance
extension Association { extension Association {
public func filterWhenConnected(_ predicate: @escaping (Database) throws -> any SQLExpressible) -> Self { public func filterWhenConnected(
_ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.filterWhenConnected { db in relation = relation.filterWhenConnected { db in
try predicate(db).sqlExpression try predicate(db).sqlExpression
@@ -203,7 +209,9 @@ extension Association {
// OrderedRequest conformance // OrderedRequest conformance
extension Association { extension Association {
public func orderWhenConnected(_ orderings: @escaping (Database) throws -> [any SQLOrderingTerm]) -> Self { public func orderWhenConnected(
_ orderings: @escaping @Sendable (Database) throws -> [any SQLOrderingTerm]
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.orderWhenConnected { db in relation = relation.orderWhenConnected { db in
try orderings(db).map(\.sqlOrdering) try orderings(db).map(\.sqlOrdering)
@@ -239,7 +247,9 @@ extension Association {
// AggregatingRequest conformance // AggregatingRequest conformance
extension Association { extension Association {
public func groupWhenConnected(_ expressions: @escaping (Database) throws -> [any SQLExpressible]) -> Self { public func groupWhenConnected(
_ expressions: @escaping @Sendable (Database) throws -> [any SQLExpressible]
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.groupWhenConnected { db in relation = relation.groupWhenConnected { db in
try expressions(db).map(\.sqlExpression) try expressions(db).map(\.sqlExpression)
@@ -247,7 +257,9 @@ extension Association {
} }
} }
public func havingWhenConnected(_ predicate: @escaping (Database) throws -> any SQLExpressible) -> Self { public func havingWhenConnected(
_ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible
) -> Self {
withDestinationRelation { relation in withDestinationRelation { relation in
relation = relation.havingWhenConnected { db in relation = relation.havingWhenConnected { db in
try predicate(db).sqlExpression try predicate(db).sqlExpression
@@ -118,7 +118,7 @@ extension QueryInterfaceRequest: FetchRequest {
extension QueryInterfaceRequest: SelectionRequest { extension QueryInterfaceRequest: SelectionRequest {
public func selectWhenConnected( public func selectWhenConnected(
_ selection: @escaping (Database) throws -> [any SQLSelectable]) _ selection: @escaping @Sendable (Database) throws -> [any SQLSelectable])
-> Self -> Self
{ {
with { with {
@@ -282,7 +282,7 @@ extension QueryInterfaceRequest: SelectionRequest {
} }
public func annotatedWhenConnected( public func annotatedWhenConnected(
with selection: @escaping (Database) throws -> [any SQLSelectable]) with selection: @escaping @Sendable (Database) throws -> [any SQLSelectable])
-> Self -> Self
{ {
with { with {
@@ -295,7 +295,7 @@ extension QueryInterfaceRequest: SelectionRequest {
extension QueryInterfaceRequest: FilteredRequest { extension QueryInterfaceRequest: FilteredRequest {
public func filterWhenConnected( public func filterWhenConnected(
_ predicate: @escaping (Database) throws -> any SQLExpressible) _ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible)
-> Self -> Self
{ {
with { with {
@@ -308,7 +308,7 @@ extension QueryInterfaceRequest: FilteredRequest {
extension QueryInterfaceRequest: OrderedRequest { extension QueryInterfaceRequest: OrderedRequest {
public func orderWhenConnected( public func orderWhenConnected(
_ orderings: @escaping (Database) throws -> [any SQLOrderingTerm]) _ orderings: @escaping @Sendable (Database) throws -> [any SQLOrderingTerm])
-> Self -> Self
{ {
with { with {
@@ -355,7 +355,7 @@ extension QueryInterfaceRequest: OrderedRequest {
extension QueryInterfaceRequest: AggregatingRequest { extension QueryInterfaceRequest: AggregatingRequest {
public func groupWhenConnected( public func groupWhenConnected(
_ expressions: @escaping (Database) throws -> [any SQLExpressible]) _ expressions: @escaping @Sendable (Database) throws -> [any SQLExpressible])
-> Self -> Self
{ {
with { with {
@@ -366,7 +366,7 @@ extension QueryInterfaceRequest: AggregatingRequest {
} }
public func havingWhenConnected( public func havingWhenConnected(
_ predicate: @escaping (Database) throws -> any SQLExpressible) _ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible)
-> Self -> Self
{ {
with { with {
@@ -58,7 +58,9 @@ public protocol SelectionRequest {
/// ///
/// - parameter selection: A closure that accepts a database connection and /// - parameter selection: A closure that accepts a database connection and
/// returns an array of result columns. /// returns an array of result columns.
func selectWhenConnected(_ selection: @escaping (Database) throws -> [any SQLSelectable]) -> Self func selectWhenConnected(
_ selection: @escaping @Sendable (Database) throws -> [any SQLSelectable]
) -> Self
/// Appends result columns to the selected columns. /// Appends result columns to the selected columns.
/// ///
@@ -78,7 +80,9 @@ public protocol SelectionRequest {
/// ///
/// - parameter selection: A closure that accepts a database connection and /// - parameter selection: A closure that accepts a database connection and
/// returns an array of result columns. /// returns an array of result columns.
func annotatedWhenConnected(with selection: @escaping (Database) throws -> [any SQLSelectable]) -> Self func annotatedWhenConnected(
with selection: @escaping @Sendable (Database) throws -> [any SQLSelectable]
) -> Self
} }
extension SelectionRequest { extension SelectionRequest {
@@ -240,7 +244,9 @@ public protocol FilteredRequest {
/// ///
/// - parameter predicate: A closure that accepts a database connection and /// - parameter predicate: A closure that accepts a database connection and
/// returns a boolean SQL expression. /// returns a boolean SQL expression.
func filterWhenConnected(_ predicate: @escaping (Database) throws -> any SQLExpressible) -> Self func filterWhenConnected(
_ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible
) -> Self
} }
extension FilteredRequest { extension FilteredRequest {
@@ -729,7 +735,9 @@ public protocol AggregatingRequest {
/// ///
/// - parameter expressions: A closure that accepts a database connection /// - parameter expressions: A closure that accepts a database connection
/// and returns an array of SQL expressions. /// and returns an array of SQL expressions.
func groupWhenConnected(_ expressions: @escaping (Database) throws -> [any SQLExpressible]) -> Self func groupWhenConnected(
_ expressions: @escaping @Sendable (Database) throws -> [any SQLExpressible]
) -> Self
/// Filters the aggregated groups with a boolean SQL expression. /// Filters the aggregated groups with a boolean SQL expression.
/// ///
@@ -753,7 +761,9 @@ public protocol AggregatingRequest {
/// ///
/// - parameter predicate: A closure that accepts a database connection and /// - parameter predicate: A closure that accepts a database connection and
/// returns a boolean SQL expression. /// returns a boolean SQL expression.
func havingWhenConnected(_ predicate: @escaping (Database) throws -> any SQLExpressible) -> Self func havingWhenConnected(
_ predicate: @escaping @Sendable (Database) throws -> any SQLExpressible
) -> Self
} }
extension AggregatingRequest { extension AggregatingRequest {
@@ -934,7 +944,9 @@ public protocol OrderedRequest {
/// ///
/// - parameter orderings: A closure that accepts a database connection and /// - parameter orderings: A closure that accepts a database connection and
/// returns an array of SQL ordering terms. /// returns an array of SQL ordering terms.
func orderWhenConnected(_ orderings: @escaping (Database) throws -> [any SQLOrderingTerm]) -> Self func orderWhenConnected(
_ orderings: @escaping @Sendable (Database) throws -> [any SQLOrderingTerm]
) -> Self
/// Returns a request with reversed ordering. /// Returns a request with reversed ordering.
/// ///
@@ -23,7 +23,7 @@
/// see SQLRelation.filterPromise. /// see SQLRelation.filterPromise.
struct DatabasePromise<T> { struct DatabasePromise<T> {
/// Returns the resolved value. /// Returns the resolved value.
let resolve: (Database) throws -> T let resolve: @Sendable (Database) throws -> T
/// Creates a promise that resolves to a value. /// Creates a promise that resolves to a value.
init(value: T) { init(value: T) {
@@ -31,12 +31,12 @@ struct DatabasePromise<T> {
} }
/// Creates a promise from a closure. /// Creates a promise from a closure.
init(_ resolve: @escaping (Database) throws -> T) { init(_ resolve: @escaping @Sendable (Database) throws -> T) {
self.resolve = resolve self.resolve = resolve
} }
/// Returns a promise whose value is transformed by the given closure. /// Returns a promise whose value is transformed by the given closure.
func map<U>(_ transform: @escaping (T) throws -> U) -> DatabasePromise<U> { func map<U>(_ transform: @escaping @Sendable (T) throws -> U) -> DatabasePromise<U> {
DatabasePromise<U> { db in DatabasePromise<U> { db in
try transform(resolve(db)) try transform(resolve(db))
} }
+8 -8
View File
@@ -187,7 +187,7 @@ extension SQLRelation {
/// Convenience factory methods which selects all rows from a table. /// Convenience factory methods which selects all rows from a table.
static func all( static func all(
fromTable tableName: String, fromTable tableName: String,
selection: @escaping (Database) -> [SQLSelection] = { _ in [.allColumns] }) selection: @escaping @Sendable (Database) -> [SQLSelection] = { _ in [.allColumns] })
-> Self -> Self
{ {
SQLRelation( SQLRelation(
@@ -197,7 +197,7 @@ extension SQLRelation {
} }
extension SQLRelation: Refinable { extension SQLRelation: Refinable {
func selectWhenConnected(_ selection: @escaping (Database) throws -> [SQLSelection]) -> Self { func selectWhenConnected(_ selection: @escaping @Sendable (Database) throws -> [SQLSelection]) -> Self {
with { with {
$0.selectionPromise = DatabasePromise(selection) $0.selectionPromise = DatabasePromise(selection)
} }
@@ -228,7 +228,7 @@ extension SQLRelation: Refinable {
} }
} }
func annotatedWhenConnected(with selection: @escaping (Database) throws -> [SQLSelection]) -> Self { func annotatedWhenConnected(with selection: @escaping @Sendable (Database) throws -> [SQLSelection]) -> Self {
with { with {
let old = $0.selectionPromise let old = $0.selectionPromise
$0.selectionPromise = DatabasePromise { db in $0.selectionPromise = DatabasePromise { db in
@@ -242,7 +242,7 @@ extension SQLRelation: Refinable {
annotatedWhenConnected(with: { _ in selection }) annotatedWhenConnected(with: { _ in selection })
} }
func filterWhenConnected(_ predicate: @escaping (Database) throws -> SQLExpression) -> Self { func filterWhenConnected(_ predicate: @escaping @Sendable (Database) throws -> SQLExpression) -> Self {
with { with {
if let old = $0.filterPromise { if let old = $0.filterPromise {
$0.filterPromise = DatabasePromise { db in $0.filterPromise = DatabasePromise { db in
@@ -259,7 +259,7 @@ extension SQLRelation: Refinable {
filterWhenConnected { _ in predicate } filterWhenConnected { _ in predicate }
} }
func orderWhenConnected(_ orderings: @escaping (Database) throws -> [SQLOrdering]) -> Self { func orderWhenConnected(_ orderings: @escaping @Sendable (Database) throws -> [SQLOrdering]) -> Self {
with { with {
$0.ordering = SQLRelation.Ordering(orderings: orderings) $0.ordering = SQLRelation.Ordering(orderings: orderings)
} }
@@ -313,13 +313,13 @@ extension SQLRelation: Refinable {
} }
} }
func groupWhenConnected(_ expressions: @escaping (Database) throws -> [SQLExpression]) -> Self { func groupWhenConnected(_ expressions: @escaping @Sendable (Database) throws -> [SQLExpression]) -> Self {
with { with {
$0.groupPromise = DatabasePromise(expressions) $0.groupPromise = DatabasePromise(expressions)
} }
} }
func havingWhenConnected(_ predicate: @escaping (Database) throws -> SQLExpression) -> Self { func havingWhenConnected(_ predicate: @escaping @Sendable (Database) throws -> SQLExpression) -> Self {
with { with {
if let old = $0.havingExpressionPromise { if let old = $0.havingExpressionPromise {
$0.havingExpressionPromise = DatabasePromise { db in $0.havingExpressionPromise = DatabasePromise { db in
@@ -740,7 +740,7 @@ extension SQLRelation {
isReversed: false) isReversed: false)
} }
init(orderings: @escaping (Database) throws -> [SQLOrdering]) { init(orderings: @escaping @Sendable (Database) throws -> [SQLOrdering]) {
self.init( self.init(
elements: [.terms(DatabasePromise(orderings))], elements: [.terms(DatabasePromise(orderings))],
isReversed: false) isReversed: false)