diff --git a/GRDB/QueryInterface/Request/Association/Association.swift b/GRDB/QueryInterface/Request/Association/Association.swift index a7fb1563c..105e3ca48 100644 --- a/GRDB/QueryInterface/Request/Association/Association.swift +++ b/GRDB/QueryInterface/Request/Association/Association.swift @@ -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)) + } +} diff --git a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift index 371a77e8c..1118f8648 100644 --- a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift +++ b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift @@ -215,7 +215,7 @@ public struct AssociationAggregate { /// SQL selection, or in the HAVING clause. /// - It helps implementing aggregate operators such as `&&`, `+`, etc. func prepare(_ 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 { func prepare(_ 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: } override func prepare(_ 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: AssociationAggregatePreparation(_ 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: AssociationAggregatePreparation(_ 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) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 50a303612..0ec8e991c 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -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(_ cte: CommonTableExpression) -> 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, diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index de3af7149..c926c0a05 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -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(_ cte: CommonTableExpression) -> 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) -> Self { var request = self let expression = aggregate.prepare(&request)