From a655974eb0e96127cf84b8e08aa35884f0f0112e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 8 Mar 2021 14:10:10 +0100 Subject: [PATCH] Let joined associations use CTEs --- GRDB/QueryInterface/SQL/SQLRelation.swift | 22 ++++++--- .../SQLGeneration/SQLQueryGenerator.swift | 2 +- GRDB/Utils/OrderedDictionary.swift | 46 +++++++++++++------ 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index 18455d4da..b1a18ec49 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -132,10 +132,6 @@ struct SQLRelation { if relation.limit != nil { fatalError("Can't join an association with a LIMIT clause") } - if relation.ctes.isEmpty == false { - // TODO: maybe we could lift ctes up to the base relation - fatalError("Not implemented: joining an association that embeds a common table expression") - } case .all: break } @@ -169,6 +165,7 @@ struct SQLRelation { #warning("TODO: turn into DatabasePromise<[SQLExpression]>? like havingExpressionPromise") var filterPromise: DatabasePromise = DatabasePromise(value: nil) var ordering: SQLRelation.Ordering = SQLRelation.Ordering() + var ctes: OrderedDictionary = [:] // See also `allCTEs` var children: OrderedDictionary = [:] // Properties below MUST NOT be used when joining to-one associations. @@ -177,7 +174,6 @@ struct SQLRelation { var groupPromise: DatabasePromise<[SQLExpression]>? var havingExpressionPromise: DatabasePromise? var limit: SQLLimit? - var ctes: OrderedDictionary = [:] } extension SQLRelation { @@ -309,6 +305,18 @@ extension SQLRelation { } } + /// All common table expressions, including those of joined children. + var allCTEs: OrderedDictionary { + children.values.reduce(into: ctes) { (ctes, child) in + switch child.kind { + case .all, .bridge: + break + case .oneOptional, .oneRequired: + ctes.merge(child.relation.allCTEs, uniquingKeysWith: { (_, new) in new }) + } + } + } + /// Returns a relation extended with an association. /// /// This method provides support for public joining methods such @@ -1032,12 +1040,12 @@ extension SQLRelation { selectionPromise: mergedSelectionPromise, filterPromise: mergedFilterPromise, ordering: mergedOrdering, + ctes: mergedCTEs, children: mergedChildren, isDistinct: mergedDistinct, groupPromise: mergedGroupPromise, havingExpressionPromise: mergedHavingExpressionPromise, - limit: mergedLimit, - ctes: mergedCTEs) + limit: mergedLimit) } } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 61bee4b80..869cbda07 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -588,7 +588,7 @@ private struct SQLQualifiedRelation { // Copy other flags limit = relation.limit isDistinct = relation.isDistinct - ctes = relation.ctes + ctes = relation.allCTEs } /// See SQLQueryGenerator.rowAdapter(_:) diff --git a/GRDB/Utils/OrderedDictionary.swift b/GRDB/Utils/OrderedDictionary.swift index 0d82ba25c..4e2b6dfa5 100644 --- a/GRDB/Utils/OrderedDictionary.swift +++ b/GRDB/Utils/OrderedDictionary.swift @@ -119,6 +119,36 @@ struct OrderedDictionary { return OrderedDictionary(keys: keys, dictionary: dictionary) } + mutating func merge( + _ other: S, + uniquingKeysWith combine: (Value, Value) throws -> Value) + rethrows + where S: Sequence, S.Element == (Key, Value) + { + for (key, value) in other { + if let current = self[key] { + self[key] = try combine(current, value) + } else { + self[key] = value + } + } + } + + mutating func merge( + _ other: S, + uniquingKeysWith combine: (Value, Value) throws -> Value) + rethrows + where S: Sequence, S.Element == (key: Key, value: Value) + { + for (key, value) in other { + if let current = self[key] { + self[key] = try combine(current, value) + } else { + self[key] = value + } + } + } + func merging( _ other: S, uniquingKeysWith combine: (Value, Value) throws -> Value) @@ -126,13 +156,7 @@ struct OrderedDictionary { where S: Sequence, S.Element == (Key, Value) { var result = self - for (key, value) in other { - if let current = result[key] { - result[key] = try combine(current, value) - } else { - result[key] = value - } - } + try result.merge(other, uniquingKeysWith: combine) return result } @@ -143,13 +167,7 @@ struct OrderedDictionary { where S: Sequence, S.Element == (key: Key, value: Value) { var result = self - for (key, value) in other { - if let current = result[key] { - result[key] = try combine(current, value) - } else { - result[key] = value - } - } + try result.merge(other, uniquingKeysWith: combine) return result } }