From 4483f037a4df6ae3a0fba6b8e47e829600f852fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 17 Apr 2020 21:17:47 +0200 Subject: [PATCH] Cleanup --- .../SQLGeneration/SQLQueryGenerator.swift | 12 +++++------- Tests/GRDBTests/AssociationHasOneSQLTests.swift | 3 --- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 478ef5c31..8a2e1c22d 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -494,17 +494,17 @@ private struct SQLQualifiedRelation { } if child.firstOnly { + // Filters and order are handled in a subquery, + // and only keep children with a non-empty selection let relation = child.relation - // Filters and order are handled in a subquery .unfiltered() .unordered() - // Only keep children with a non-empty selection .filteringChildren { $0.relation.selection.isEmpty == false } return SQLQualifiedJoin( kind: kind, condition: child.condition, relation: SQLQualifiedRelation(relation), - target: .firstOnly(child.relation)) + target: .firstInSubRelation(child.relation)) } else { return SQLQualifiedJoin( kind: kind, @@ -643,14 +643,13 @@ private struct SQLQualifiedJoin: Refinable { enum Target { case all - case firstOnly(SQLRelation) + case firstInSubRelation(SQLRelation) } var kind: Kind var condition: SQLAssociationCondition var relation: SQLQualifiedRelation var target: Target -// var firstOnly: Bool func sql(_ db: Database, _ context: inout SQLGenerationContext, leftAlias: TableAlias) throws -> String { try sql(db, &context, leftAlias: leftAlias, allowingInnerJoin: true) @@ -694,8 +693,7 @@ private struct SQLQualifiedJoin: Refinable { if filters.isEmpty == false { sql += " ON \(filters.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false))" } - case let .firstOnly(subRelation): - #warning("TODO: use child.condition") + case let .firstInSubRelation(subRelation): let subAlias = TableAlias() let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: subAlias) let subRelation = subRelation diff --git a/Tests/GRDBTests/AssociationHasOneSQLTests.swift b/Tests/GRDBTests/AssociationHasOneSQLTests.swift index a00ef40b0..2f9e7f40e 100644 --- a/Tests/GRDBTests/AssociationHasOneSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasOneSQLTests.swift @@ -832,7 +832,6 @@ class AssociationHasOneSQLTests: GRDBTestCase { LIMIT 1 """) } - // TODO: test that first/last can be used with filtering with further association } } @@ -948,7 +947,6 @@ class AssociationHasOneSQLTests: GRDBTestCase { ORDER BY "child"."id" LIMIT 1) \ JOIN "toy" ON "toy"."childId" = "child"."id" """) - // TODO: remove toy from selection, since this is a request for child try assertEqualSQL(db, Parent().request(for: association), """ SELECT "child".*, "toy".* \ FROM "child" \ @@ -1105,7 +1103,6 @@ class AssociationHasOneSQLTests: GRDBTestCase { LIMIT 1 """) } - // TODO: test that first/last can be used with filtering with further association } } }