mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Merge AggregateJoinableRequest into DerivableRequest
Also put distinct(), limit(_:offset:) and with(_:) into DerivableRequest
This commit is contained in:
1 parent
6b733cfc0a
commit
f9b554d00a
4 files changed
+103
-90
No files matched your search
@@ -314,29 +314,7 @@ extension Association where Self: AggregatingRequest {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AssociationToOne
|
||||
|
||||
/// The base protocol for all associations that define a one-to-one connection.
|
||||
public protocol AssociationToOne: Association { }
|
||||
|
||||
extension AssociationToOne {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedSingular(key)
|
||||
return .init(sqlAssociation: _sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AssociationToMany
|
||||
|
||||
/// The base protocol for all associations that define a one-to-many connection.
|
||||
public protocol AssociationToMany: Association, AggregateJoinableRequest { }
|
||||
|
||||
extension AssociationToMany {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedPlural(key)
|
||||
return .init(sqlAssociation: _sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
|
||||
extension Association where Self: DerivableRequest {
|
||||
/// Creates an association for returns distinct rows.
|
||||
public func distinct() -> Self {
|
||||
mapDestinationRelation { $0.with(\.isDistinct, true) }
|
||||
@@ -356,3 +334,27 @@ extension AssociationToMany {
|
||||
mapDestinationRelation { $0.with(\.ctes[cte.tableName], cte.cte) }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AssociationToOne
|
||||
|
||||
/// The base protocol for all associations that define a one-to-one connection.
|
||||
public protocol AssociationToOne: Association { }
|
||||
|
||||
extension AssociationToOne {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedSingular(key)
|
||||
return .init(sqlAssociation: _sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AssociationToMany
|
||||
|
||||
/// The base protocol for all associations that define a one-to-many connection.
|
||||
public protocol AssociationToMany: Association { }
|
||||
|
||||
extension AssociationToMany {
|
||||
public func forKey(_ key: String) -> Self {
|
||||
let associationKey = SQLAssociationKey.fixedPlural(key)
|
||||
return .init(sqlAssociation: _sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
}
|
||||
@@ -215,7 +215,7 @@ public struct AssociationAggregate<RowDecoder> {
|
||||
/// SQL selection, or in the HAVING clause.
|
||||
/// - It helps implementing aggregate operators such as `&&`, `+`, etc.
|
||||
func prepare<Request>(_ request: inout Request) -> SQLExpression
|
||||
where Request: AggregateJoinableRequest, Request.RowDecoder == RowDecoder
|
||||
where Request: DerivableRequest, Request.RowDecoder == RowDecoder
|
||||
{
|
||||
preparation.prepare(&request)
|
||||
}
|
||||
@@ -262,7 +262,7 @@ extension AssociationAggregate: Refinable {
|
||||
/// It only exists as support for `AssociationAggregate.prepare(_:)`.
|
||||
private class AssociationAggregatePreparation<RowDecoder> {
|
||||
func prepare<Request>(_ request: inout Request) -> SQLExpression
|
||||
where Request: AggregateJoinableRequest, Request.RowDecoder == RowDecoder
|
||||
where Request: DerivableRequest, Request.RowDecoder == RowDecoder
|
||||
{
|
||||
fatalError("subclass must override")
|
||||
}
|
||||
@@ -281,7 +281,7 @@ private class BasePreparation<Association: AssociationToMany>:
|
||||
}
|
||||
|
||||
override func prepare<Request>(_ request: inout Request) -> SQLExpression
|
||||
where Request: AggregateJoinableRequest, Request.RowDecoder == Association.OriginRowDecoder
|
||||
where Request: DerivableRequest, Request.RowDecoder == Association.OriginRowDecoder
|
||||
{
|
||||
let tableAlias = TableAlias()
|
||||
request = request
|
||||
@@ -305,7 +305,7 @@ private class MapPreparation<RowDecoder>: AssociationAggregatePreparation<RowDec
|
||||
}
|
||||
|
||||
override func prepare<Request>(_ request: inout Request) -> SQLExpression
|
||||
where Request: AggregateJoinableRequest, Request.RowDecoder == RowDecoder
|
||||
where Request: DerivableRequest, Request.RowDecoder == RowDecoder
|
||||
{
|
||||
transform(base.prepare(&request))
|
||||
}
|
||||
@@ -335,7 +335,7 @@ private class CombinePreparation<RowDecoder>: AssociationAggregatePreparation<Ro
|
||||
}
|
||||
|
||||
override func prepare<Request>(_ request: inout Request) -> SQLExpression
|
||||
where Request: AggregateJoinableRequest, Request.RowDecoder == RowDecoder
|
||||
where Request: DerivableRequest, Request.RowDecoder == RowDecoder
|
||||
{
|
||||
let lhsExpression = lhs.prepare(&request)
|
||||
let rhsExpression = rhs.prepare(&request)
|
||||
|
||||
@@ -298,71 +298,21 @@ extension QueryInterfaceRequest: TableRequest {
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest: DerivableRequest where RowDecoder: TableRecord { }
|
||||
extension QueryInterfaceRequest: AggregateJoinableRequest where RowDecoder: TableRecord { }
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
/// Creates a request which returns distinct rows.
|
||||
///
|
||||
/// // SELECT DISTINCT * FROM player
|
||||
/// var request = Player.all()
|
||||
/// request = request.distinct()
|
||||
///
|
||||
/// // SELECT DISTINCT name FROM player
|
||||
/// var request = Player.select(Column("name"))
|
||||
/// request = request.distinct()
|
||||
extension QueryInterfaceRequest: DerivableRequest {
|
||||
public func distinct() -> QueryInterfaceRequest {
|
||||
with(\.relation.isDistinct, true)
|
||||
}
|
||||
|
||||
/// Creates a request which fetches *limit* rows, starting at *offset*.
|
||||
///
|
||||
/// // SELECT * FROM player LIMIT 1
|
||||
/// var request = Player.all()
|
||||
/// request = request.limit(1)
|
||||
///
|
||||
/// Any previous limit is replaced.
|
||||
public func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceRequest {
|
||||
public func limit(_ limit: Int, offset: Int?) -> QueryInterfaceRequest {
|
||||
with(\.relation.limit, SQLLimit(limit: limit, offset: offset))
|
||||
}
|
||||
|
||||
/// Returns a request which embeds the common table expression.
|
||||
///
|
||||
/// If a common table expression with the same table name had already been
|
||||
/// embedded, it is replaced by the new one.
|
||||
///
|
||||
/// For example, you can build a request that fetches all chats with their
|
||||
/// latest message:
|
||||
///
|
||||
/// let latestMessageRequest = Message
|
||||
/// .annotated(with: max(Column("date")))
|
||||
/// .group(Column("chatID"))
|
||||
///
|
||||
/// let latestMessageCTE = CommonTableExpression(
|
||||
/// named: "latestMessage",
|
||||
/// request: latestMessageRequest)
|
||||
///
|
||||
/// let latestMessage = Chat.association(
|
||||
/// to: latestMessageCTE,
|
||||
/// on: { chat, latestMessage in
|
||||
/// chat[Column("id")] == latestMessage[Column("chatID")]
|
||||
/// })
|
||||
///
|
||||
/// // WITH latestMessage AS
|
||||
/// // (SELECT *, MAX(date) FROM message GROUP BY chatID)
|
||||
/// // SELECT chat.*, latestMessage.*
|
||||
/// // FROM chat
|
||||
/// // LEFT JOIN latestMessage ON chat.id = latestMessage.chatID
|
||||
/// let request = Chat.all()
|
||||
/// .with(latestMessageCTE)
|
||||
/// .including(optional: latestMessage)
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - returns: A request.
|
||||
public func with<RowDecoder>(_ cte: CommonTableExpression<RowDecoder>) -> Self {
|
||||
with(\.relation.ctes[cte.tableName], cte.cte)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
/// Creates a request bound to type RowDecoder.
|
||||
///
|
||||
/// The returned request can fetch if the type RowDecoder is fetchable (Row,
|
||||
|
||||
@@ -590,14 +590,75 @@ extension JoinableRequest {
|
||||
// MARK: - DerivableRequest
|
||||
|
||||
/// The base protocol for all requests that can be refined.
|
||||
public protocol DerivableRequest: FilteredRequest, JoinableRequest, OrderedRequest, SelectionRequest, TableRequest { }
|
||||
public protocol DerivableRequest: AggregatingRequest, FilteredRequest,
|
||||
JoinableRequest, OrderedRequest,
|
||||
SelectionRequest, TableRequest
|
||||
{
|
||||
/// Creates a request which returns distinct rows.
|
||||
///
|
||||
/// // SELECT DISTINCT * FROM player
|
||||
/// var request = Player.all()
|
||||
/// request = request.distinct()
|
||||
///
|
||||
/// // SELECT DISTINCT name FROM player
|
||||
/// var request = Player.select(Column("name"))
|
||||
/// request = request.distinct()
|
||||
func distinct() -> Self
|
||||
|
||||
/// Creates a request which fetches *limit* rows, starting at *offset*.
|
||||
///
|
||||
/// // SELECT * FROM player LIMIT 10 OFFSET 20
|
||||
/// var request = Player.all()
|
||||
/// request = request.limit(10, offset: 20)
|
||||
///
|
||||
/// Any previous limit is replaced.
|
||||
func limit(_ limit: Int, offset: Int?) -> Self
|
||||
|
||||
/// Returns a request which embeds the common table expression.
|
||||
///
|
||||
/// If a common table expression with the same table name had already been
|
||||
/// embedded, it is replaced by the new one.
|
||||
///
|
||||
/// For example, you can build a request that fetches all chats with their
|
||||
/// latest message:
|
||||
///
|
||||
/// let latestMessageRequest = Message
|
||||
/// .annotated(with: max(Column("date")))
|
||||
/// .group(Column("chatID"))
|
||||
///
|
||||
/// let latestMessageCTE = CommonTableExpression(
|
||||
/// named: "latestMessage",
|
||||
/// request: latestMessageRequest)
|
||||
///
|
||||
/// let latestMessage = Chat.association(
|
||||
/// to: latestMessageCTE,
|
||||
/// on: { chat, latestMessage in
|
||||
/// chat[Column("id")] == latestMessage[Column("chatID")]
|
||||
/// })
|
||||
///
|
||||
/// // WITH latestMessage AS
|
||||
/// // (SELECT *, MAX(date) FROM message GROUP BY chatID)
|
||||
/// // SELECT chat.*, latestMessage.*
|
||||
/// // FROM chat
|
||||
/// // LEFT JOIN latestMessage ON chat.id = latestMessage.chatID
|
||||
/// let request = Chat.all()
|
||||
/// .with(latestMessageCTE)
|
||||
/// .including(optional: latestMessage)
|
||||
func with<RowDecoder>(_ cte: CommonTableExpression<RowDecoder>) -> Self
|
||||
}
|
||||
|
||||
// MARK: - AggregateJoinableRequest
|
||||
|
||||
/// The base protocol for all requests that can use association aggregates.
|
||||
public protocol AggregateJoinableRequest: AggregatingRequest & JoinableRequest & SelectionRequest & TableRequest { }
|
||||
|
||||
extension AggregateJoinableRequest {
|
||||
extension DerivableRequest {
|
||||
/// Creates a request which fetches *limit* rows.
|
||||
///
|
||||
/// // SELECT * FROM player LIMIT 1
|
||||
/// var request = Player.all()
|
||||
/// request = request.limit(1)
|
||||
///
|
||||
/// Any previous limit is replaced.
|
||||
public func limit(_ limit: Int) -> Self {
|
||||
self.limit(limit, offset: nil)
|
||||
}
|
||||
|
||||
private func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> Self {
|
||||
var request = self
|
||||
let expression = aggregate.prepare(&request)
|
||||
|
||||
Reference in new issue
Block a user