From a42a5cc2ca27251e06d56dc8167a636455317308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 29 Dec 2019 16:57:26 +0100 Subject: [PATCH] SQLQualifiedRelation.selectOnly(_:) --- .../SQLGeneration/SQLQueryGenerator.swift | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 789f0ab4b..c56dcf029 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -322,7 +322,7 @@ private struct SQLQualifiedRelation { let source: SQLQualifiedSource /// The selection, not including selection of joined relations - private let ownSelection: [SQLSelectable] + private var ownSelection: [SQLSelectable] /// The full selection, including selection of joined relations /// @@ -361,7 +361,7 @@ private struct SQLQualifiedRelation { /// SELECT ... FROM ... AS ... JOIN ... WHERE ... ORDER BY ... /// | /// • joins - let joins: OrderedDictionary + var joins: OrderedDictionary init(_ relation: SQLRelation) { // Qualify the source, so that it be disambiguated with an SQL alias @@ -456,6 +456,14 @@ private struct SQLQualifiedRelation { return (adapter: adapter, endIndex: endIndex) } + + /// Removes all selections from joins + func selectOnly(_ selection: [SQLSelectable]) -> SQLQualifiedRelation { + var relation = self + relation.ownSelection = selection.map { $0.qualifiedSelectable(with: alias) } + relation.joins = relation.joins.mapValues { $0.selectOnly([]) } + return relation + } } /// A "qualified" source, where all tables are identified with a table alias. @@ -520,6 +528,14 @@ private struct SQLQualifiedJoin { return try sql(db, &context, leftAlias: leftAlias, allowingInnerJoin: true) } + /// Removes all selections from joins + func selectOnly(_ selection: [SQLSelectable]) -> SQLQualifiedJoin { + return SQLQualifiedJoin( + kind: kind, + condition: condition, + relation: relation.selectOnly(selection)) + } + private func sql( _ db: Database, _ context: inout SQLGenerationContext,