From 378279ee1acffe005042f89a723b71ebfb4f6c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 5 Dec 2020 10:32:46 +0100 Subject: [PATCH] Move the isRecursive flag where it belongs --- .../Request/CommonTableExpression.swift | 97 +++++++++++-------- GRDB/QueryInterface/SQL/SQLQuery.swift | 12 +-- .../SQLGeneration/SQLQueryGenerator.swift | 6 +- 3 files changed, 61 insertions(+), 54 deletions(-) diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index 816989684..4fbf72744 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -12,10 +12,6 @@ public struct CommonTableExpression { /// answer.tableName // "answer" public var tableName: String - /// Whether this common table expression needs a `WITH RECURSIVE` - /// sql clause. - public var isRecursive: Bool - var cte: SQLCTE /// Creates a common table expression from a request. @@ -44,9 +40,11 @@ public struct CommonTableExpression { columns: [String]? = nil, request: Request) { - self.isRecursive = recursive self.tableName = tableName - self.cte = SQLCTE(columns: columns, request: request) + self.cte = SQLCTE( + columns: columns, + request: request, + isRecursive: recursive) } /// Creates a common table expression from an SQL string and @@ -170,6 +168,10 @@ struct SQLCTE { /// ~~~~~~~~~~ var request: _FetchRequest + /// Whether this common table expression needs a `WITH RECURSIVE` + /// sql clause. + var isRecursive: Bool + /// The number of columns in the common table expression. func columnsCount(_ db: Database) throws -> Int { if let columns = columns { @@ -318,58 +320,73 @@ extension CommonTableExpression { extension QueryInterfaceRequest { /// Returns a request which embeds the common table expressions. /// - /// For example, you can build a request that fetches all chats with their - /// latest post: + /// If a common table expression with the same table name had already been + /// embedded, it is replaced by the new one. /// - /// // WITH latestPost AS (SELECT *, MAX(date) FROM post GROUP BY chatID) - /// // SELECT chat.*, latestPost.* + /// 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 latestPost ON chat.id = latestPost.chatID - /// let latestPostCTE = CommonTableExpression( - /// named: "latestPost", - /// request: Post - /// .annotated(with: max(Column("date"))) - /// .group(Column("chatID"))) - /// let latestPost = Chat.association(to: latestPostCTE, on: { chat, latestPost in - /// chat[Column("id")] == latestPost[Column("chatID")] - /// }) + /// // LEFT JOIN latestMessage ON chat.id = latestMessage.chatID /// let request = Chat.all() - /// .with(latestPostCTE) - /// .including(optional: latestPost) + /// .with(latestMessageCTE) + /// .including(optional: latestMessage) /// /// - parameter cte: A common table expression. /// - returns: A request. public func with(_ cte: CommonTableExpression) -> Self { - mapInto(\.query.ctes) { ctes in - if cte.isRecursive { - ctes.isRecursive = true - } - ctes.ctes[cte.tableName] = cte.cte - } + with(\.query.ctes[cte.tableName], cte.cte) } } extension TableRecord { /// Returns a request which embeds the common table expressions. /// + /// 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: /// - /// // WITH latestPost AS (SELECT *, MAX(date) FROM post GROUP BY chatID) - /// // SELECT chat.*, latestPost.* + /// 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 latestPost ON chat.id = latestPost.chatID - /// let latestPostCTE = CommonTableExpression( - /// named: "latestPost", - /// request: Post - /// .annotated(with: max(Column("date"))) - /// .group(Column("chatID"))) - /// let latestPost = Chat.association(to: latestPostCTE, on: { chat, latestPost in - /// chat[Column("id")] == latestPost[Column("chatID")] - /// }) + /// // LEFT JOIN latestMessage ON chat.id = latestMessage.chatID /// let request = Chat - /// .with(latestPostCTE) - /// .including(optional: latestPost) + /// .with(latestMessageCTE) + /// .including(optional: latestMessage) /// /// - parameter cte: A common table expression. /// - returns: A request. diff --git a/GRDB/QueryInterface/SQL/SQLQuery.swift b/GRDB/QueryInterface/SQL/SQLQuery.swift index 594c4af50..6b10df9a0 100644 --- a/GRDB/QueryInterface/SQL/SQLQuery.swift +++ b/GRDB/QueryInterface/SQL/SQLQuery.swift @@ -10,7 +10,7 @@ struct SQLQuery { // `(a AND b AND c)` instead of `((a AND b) AND c)`. var havingExpressionsPromise: DatabasePromise<[SQLExpression]> = DatabasePromise(value: []) var limit: SQLLimit? - var ctes = SQLQueryCTEs() + var ctes: OrderedDictionary = [:] } extension SQLQuery: Refinable { @@ -155,16 +155,6 @@ extension SQLQuery { } } -/// The collection of common table expressions in an `SQLQuery`. -struct SQLQueryCTEs: Refinable { - var isRecursive: Bool = false - - /// A [table name: CTE] dictionary - var ctes: OrderedDictionary = [:] - - var isEmpty: Bool { ctes.isEmpty } -} - struct SQLLimit { let limit: Int let offset: Int? diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 83efc9455..84c968749 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -6,7 +6,7 @@ struct SQLQueryGenerator: Refinable { private let havingExpressionsPromise: DatabasePromise<[SQLExpression]> private let limit: SQLLimit? private let singleResult: Bool - private let ctes: SQLQueryCTEs + private let ctes: OrderedDictionary // For database region private let prefetchedAssociations: [_SQLAssociation] @@ -74,10 +74,10 @@ struct SQLQueryGenerator: Refinable { if !ctes.isEmpty { sql += "WITH " - if ctes.isRecursive { + if ctes.values.contains(where: \.isRecursive) { sql += "RECURSIVE " } - sql += try ctes.ctes + sql += try ctes .map { tableName, cte in let columnsSQL = cte.columns .map { columns in