From 01916760637b185bc4f5e1284601a4cb6c780fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 1 Dec 2020 09:17:05 +0100 Subject: [PATCH] Using clause for CTE associations --- .../Request/CommonTableExpression.swift | 9 ++++++ .../SQLGeneration/SQLQueryGenerator.swift | 11 ++++++- .../CommonTableExpressionTests.swift | 32 ++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index 7eb227e10..f2d838918 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -29,6 +29,15 @@ extension CommonTableExpression { relation: relationForAll) } + #warning("TODO: doc") + public func association(from: T.Type = T.self, using columns: Column...) + -> JoinAssociation + { + association(from: T.self, on: { (left, right) -> SQLExpressible in + columns.map { left[$0] == right[$0] }.joined(operator: .and) + }) + } + #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") #warning("TODO: doc") public func association( diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 99d6f19d9..176185334 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -827,9 +827,18 @@ private struct SQLQualifiedJoin: Refinable { switch condition { case let .promise(condition): let conditionExpression = condition(leftAlias, rightAlias).sqlExpression - if let dbValue = conditionExpression as? DatabaseValue, dbValue == true.databaseValue { + #warning("TODO: we need a expression.isTrue() method") + if let dbValue = conditionExpression as? DatabaseValue, + dbValue == true.databaseValue + { // ON 1 <=> no ON clause joinExpressions = [] + } else if let expr = conditionExpression as? _SQLExpressionAssociativeBinary, + expr.op == .and, + expr.expressions.isEmpty + { + // ON (nothing) <=> no ON clause + joinExpressions = [] } else { joinExpressions = [conditionExpression] } diff --git a/Tests/GRDBTests/CommonTableExpressionTests.swift b/Tests/GRDBTests/CommonTableExpressionTests.swift index 30066153b..588f3123a 100644 --- a/Tests/GRDBTests/CommonTableExpressionTests.swift +++ b/Tests/GRDBTests/CommonTableExpressionTests.swift @@ -48,7 +48,7 @@ class CommonTableExpressionTests: GRDBTestCase { """) } - // Include SQL request as a CTE + // Include SQL request as a CTE (true ON clause) do { let cte = SQLRequest(literal: "SELECT \("O'Brien")") .commonTableExpression(tableName: "cte") @@ -63,6 +63,36 @@ class CommonTableExpressionTests: GRDBTestCase { """) } + // Include SQL request as a CTE (USING clause) + do { + let cte = SQLRequest(literal: "SELECT \("O'Brien") AS id") + .commonTableExpression(tableName: "cte") + let request = T.all() + .with(cte) + .including(required: cte.association(using: Column("id"))) + try assertEqualSQL(db, request, """ + WITH "cte" AS (SELECT 'O''Brien' AS id) \ + SELECT "t".*, "cte".* \ + FROM "t" \ + JOIN "cte" ON "t"."id" = "cte"."id" + """) + } + + // Include SQL request as a CTE (empty ON clause) + do { + let cte = SQLRequest(literal: "SELECT \("O'Brien")") + .commonTableExpression(tableName: "cte") + let request = T.all() + .with(cte) + .including(required: cte.association()) + try assertEqualSQL(db, request, """ + WITH "cte" AS (SELECT 'O''Brien') \ + SELECT "t".*, "cte".* \ + FROM "t" \ + JOIN "cte" + """) + } + // Join query interface request as a CTE do { let cte = T.all()