mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
More cte association api experiments
This commit is contained in:
1 parent
f31dd0033c
commit
c16da693df
3 files changed
+28
-25
No files matched your search
@@ -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<Void> {
|
||||
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<T>(from: T.Type = T.self, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
|
||||
-> JoinAssociation<T, Void>
|
||||
{
|
||||
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<Destination>(
|
||||
to destination: Destination.Type,
|
||||
on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
|
||||
-> JoinAssociation<Void, Void>
|
||||
-> JoinAssociation<Void, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
JoinAssociation(
|
||||
|
||||
@@ -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<Self, Void>
|
||||
{
|
||||
JoinAssociation(
|
||||
key: .inflected(cte.tableName),
|
||||
condition: .promise(condition),
|
||||
relation: cte.relationForAll)
|
||||
}
|
||||
}
|
||||
|
||||
/// A ForeignKey helps building associations when GRDB can't infer a foreign
|
||||
|
||||
@@ -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<Int>(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()
|
||||
|
||||
Reference in new issue
Block a user