diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index eefd8827c..a3f9b3b60 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -203,10 +203,12 @@ extension SQLRelation: Refinable { select { _ in expressions.map { .expression($0) } } } - /// Removes all selections from chidren + /// Sets the selection, removes all selections from chidren, and clears the + /// `isDistinct` flag. func selectOnly(_ selection: [SQLSelection]) -> Self { self .select(selection) + .with(\.isDistinct, false) .map(\.children) { children in children.mapValues { child in child.map(\.relation) { $0.selectOnly([]) } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 3ab6480c4..20c854d6b 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -524,8 +524,7 @@ private struct SQLQualifiedRelation { /// The selection from source, not including selection of joined relations private var sourceSelectionPromise: DatabasePromise<[SQLSelection]> - #warning("TODO: decide if selectOnly() should reset this flag") - let isDistinct: Bool + var isDistinct: Bool /// The full selection, including selection of joined relations var selectionPromise: DatabasePromise<[SQLSelection]> { @@ -651,14 +650,20 @@ private struct SQLQualifiedRelation { return (adapter: adapter, endIndex: endIndex) } - /// Removes all selections from joins + /// Sets the selection, removes all selections from joins, and clears the + /// `isDistinct` flag. func selectOnly(_ selection: [SQLSelection]) -> Self { let sourceSelectionPromise = DatabasePromise(value: selection.map { $0.qualified(with: source.alias) }) return self .with(\.sourceSelectionPromise, sourceSelectionPromise) - .map(\.joins, { $0.mapValues { $0.selectOnly([]) } }) + .with(\.isDistinct, false) + .map(\.joins, { joins in + joins.mapValues { join in + join.map(\.relation) { $0.selectOnly([]) } + } + }) } } @@ -720,11 +725,6 @@ private struct SQLQualifiedJoin: Refinable { try sql(context, leftAlias: leftAlias, allowingInnerJoin: true) } - /// Removes all selections from joins - func selectOnly(_ selection: [SQLSelection]) -> SQLQualifiedJoin { - map(\.relation) { $0.selectOnly(selection) } - } - private func sql( _ context: SQLGenerationContext, leftAlias: TableAlias,