diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index b83650e4f..612c2544b 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -406,83 +406,3 @@ extension CommonTableExpression { JoinAssociation(to: Destination.relationForAll, condition: .none) } } - -// MARK: - With - -extension QueryInterfaceRequest { - /// 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 TableRecord { - /// 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 post: - /// - /// 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 - /// .with(latestMessageCTE) - /// .including(optional: latestMessage) - /// - /// - parameter cte: A common table expression. - /// - returns: A request. - public static func with(_ cte: CommonTableExpression) -> QueryInterfaceRequest { - all().with(cte) - } -} diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 1e91e60ec..50a303612 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -326,6 +326,43 @@ extension 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) + } + /// Creates a request bound to type RowDecoder. /// /// The returned request can fetch if the type RowDecoder is fetchable (Row, diff --git a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift index e0f1f2ac7..a0793ce6b 100644 --- a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift @@ -373,4 +373,41 @@ extension TableRecord { public static func aliased(_ alias: TableAlias) -> QueryInterfaceRequest { all().aliased(alias) } + + /// 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 post: + /// + /// 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 + /// .with(latestMessageCTE) + /// .including(optional: latestMessage) + /// + /// - parameter cte: A common table expression. + /// - returns: A request. + public static func with(_ cte: CommonTableExpression) -> QueryInterfaceRequest { + all().with(cte) + } }