diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index cf76931ec..7eb227e10 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -2,7 +2,9 @@ public struct CommonTableExpression { var tableName: String var request: _FetchRequest - +} + +extension CommonTableExpression { var relationForAll: SQLRelation { SQLRelation( source: .table(tableName: tableName, alias: nil), @@ -13,6 +15,19 @@ public struct CommonTableExpression { public func all() -> QueryInterfaceRequest { QueryInterfaceRequest(relation: relationForAll) } +} + +extension CommonTableExpression { + #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") + #warning("TODO: doc") + public func association(from: T.Type = T.self, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) + -> JoinAssociation + { + JoinAssociation( + key: .inflected(tableName), + condition: .promise(condition), + relation: relationForAll) + } #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") #warning("TODO: doc") @@ -32,7 +47,7 @@ public struct CommonTableExpression { public func association( to destination: Destination.Type, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) - -> JoinAssociation + -> JoinAssociation where Destination: TableRecord { JoinAssociation( diff --git a/GRDB/QueryInterface/TableRecord+Association.swift b/GRDB/QueryInterface/TableRecord+Association.swift index 99d5c2042..b92f206c2 100644 --- a/GRDB/QueryInterface/TableRecord+Association.swift +++ b/GRDB/QueryInterface/TableRecord+Association.swift @@ -357,19 +357,6 @@ extension TableRecord { return association } } - - #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") - #warning("TODO: doc") - public static func association( - to cte: CommonTableExpression, - on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) - -> JoinAssociation - { - JoinAssociation( - key: .inflected(cte.tableName), - condition: .promise(condition), - relation: cte.relationForAll) - } } /// A ForeignKey helps building associations when GRDB can't infer a foreign diff --git a/Tests/GRDBTests/CommonTableExpressionTests.swift b/Tests/GRDBTests/CommonTableExpressionTests.swift index d98be3931..30066153b 100644 --- a/Tests/GRDBTests/CommonTableExpressionTests.swift +++ b/Tests/GRDBTests/CommonTableExpressionTests.swift @@ -39,7 +39,7 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .including(optional: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] })) + .including(optional: cte.association(on: { (left, right) in left["id"] > right["id"] })) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT * FROM "t") \ SELECT "t".*, "cte".* \ @@ -54,7 +54,7 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .including(required: T.association(to: cte, on: { (_, _) in true })) + .including(required: cte.association(on: { (_, _) in true })) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT 'O''Brien') \ SELECT "t".*, "cte".* \ @@ -69,7 +69,7 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .joining(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] })) + .joining(required: cte.association(on: { (left, right) in left["id"] > right["id"] })) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT * FROM "t") \ SELECT "t".* \ @@ -84,8 +84,8 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] })) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] })) + .including(required: cte.association(on: { (left, right) in left["id"] > right["id"] })) + .including(required: cte.association(on: { (left, right) in left["id"] > right["id"] })) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT * FROM "t") \ SELECT "t".*, "cte".* \ @@ -100,8 +100,8 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] })) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] + right["id"] == 1 })) + .including(required: cte.association(on: { (left, right) in left["id"] > right["id"] })) + .including(required: cte.association(on: { (left, right) in left["id"] + right["id"] == 1 })) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT * FROM "t") \ SELECT "t".*, "cte".* \ @@ -116,8 +116,8 @@ class CommonTableExpressionTests: GRDBTestCase { .commonTableExpression(tableName: "cte") let request = T.all() .with(cte) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] }).forKey("a")) - .including(required: T.association(to: cte, on: { (left, right) in left["id"] > right["id"] }).forKey("b")) + .including(required: cte.association(on: { (left, right) in left["id"] > right["id"] }).forKey("a")) + .including(required: cte.association(on: { (left, right) in left["id"] > right["id"] }).forKey("b")) try assertEqualSQL(db, request, """ WITH "cte" AS (SELECT * FROM "t") \ SELECT "t".*, "cte1".*, "cte2".* \ @@ -132,7 +132,8 @@ class CommonTableExpressionTests: GRDBTestCase { let cte1 = T.all().commonTableExpression(tableName: "cte1") let cte2 = SQLRequest(literal: "SELECT \("O'Brien")") .commonTableExpression(tableName: "cte2") - let assoc1 = T.association(to: cte1, on: { _, _ in true }) + #warning("TODO: from? to? WTF?") + let assoc1 = cte1.association(from: T.self, on: { _, _ in true }) let assoc2 = cte1.association(to: cte2, on: { _, _ in true }) let assoc3 = cte2.association(to: T.self, on: { _, _ in true }) let request = T.all()