AggregateJoinableRequest is the new protocol that supports aggregate annotations

AssociationToMany is a subprotocol of AggregateJoinableRequest :-)
This commit is contained in:
Gwendal Roué committed 2021-03-07 19:15:09 +01:00
1 parent ea6a052b73
commit a4dca3390a
4 files changed
+244 -146

No files matched your search

@@ -299,6 +299,7 @@ extension QueryInterfaceRequest: TableRequest {
}
extension QueryInterfaceRequest: DerivableRequest where RowDecoder: TableRecord { }
extension QueryInterfaceRequest: AggregateJoinableRequest where RowDecoder: TableRecord { }
extension QueryInterfaceRequest {
/// Creates a request which returns distinct rows.
@@ -343,57 +344,6 @@ extension QueryInterfaceRequest {
}
}
// MARK: - Aggregates
extension QueryInterfaceRequest {
private func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest {
var request = self
let expressionPromise = aggregate.prepare(&request)
if let key = aggregate.key {
return request.annotated(with: { db in try [expressionPromise.resolve(db).forKey(key)] })
} else {
return request.annotated(with: { db in try [expressionPromise.resolve(db)] })
}
}
/// Creates a request which appends *aggregates* to the current selection.
///
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
/// // FROM player LEFT JOIN book ...
/// var request = Player.all()
/// request = request.annotated(with: Player.books.count)
public func annotated(with aggregates: AssociationAggregate<RowDecoder>...) -> QueryInterfaceRequest {
annotated(with: aggregates)
}
/// Creates a request which appends *aggregates* to the current selection.
///
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
/// // FROM player LEFT JOIN book ...
/// var request = Player.all()
/// request = request.annotated(with: [Player.books.count])
public func annotated(with aggregates: [AssociationAggregate<RowDecoder>]) -> QueryInterfaceRequest {
aggregates.reduce(self) { request, aggregate in
request.annotated(with: aggregate)
}
}
/// Creates a request which appends the provided aggregate *predicate* to
/// the eventual set of already applied predicates.
///
/// // SELECT player.*
/// // FROM player LEFT JOIN book ...
/// // HAVING COUNT(DISTINCT book.id) = 0
/// var request = Player.all()
/// request = request.having(Player.books.isEmpty)
public func having(_ predicate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest {
var request = self
let expressionPromise = predicate.prepare(&request)
return request.having(expressionPromise.resolve)
}
}
// MARK: - Batch Delete
extension QueryInterfaceRequest where RowDecoder: MutablePersistableRecord {