mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Extract SQLAssociationCondition.using
This commit is contained in:
1 parent
06000abbfd
commit
a70b42cbf5
7 files changed
+67
-29
No files matched your search
@@ -33,10 +33,13 @@ extension TableRecord {
|
||||
#warning("TODO: doc")
|
||||
public static func association<Destination>(
|
||||
to cte: CommonTableExpression<Destination>,
|
||||
using columns: Column...)
|
||||
using columns: [Column] = [])
|
||||
-> JoinAssociation<Self, Destination>
|
||||
{
|
||||
association(to: cte, on: joinCondition(columns))
|
||||
JoinAssociation(
|
||||
key: .inflected(cte.tableName),
|
||||
condition: .using(columns),
|
||||
relation: cte.relationForAll)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +59,13 @@ extension CommonTableExpression {
|
||||
#warning("TODO: doc")
|
||||
public func association<Destination>(
|
||||
to cte: CommonTableExpression<Destination>,
|
||||
using columns: Column...)
|
||||
using columns: [Column] = [])
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
{
|
||||
association(to: cte, on: joinCondition(columns))
|
||||
JoinAssociation(
|
||||
key: .inflected(cte.tableName),
|
||||
condition: .using(columns),
|
||||
relation: cte.relationForAll)
|
||||
}
|
||||
|
||||
#warning("TODO: doc")
|
||||
@@ -78,11 +84,14 @@ extension CommonTableExpression {
|
||||
#warning("TODO: doc")
|
||||
public func association<Destination>(
|
||||
to destination: Destination.Type,
|
||||
using columns: Column...)
|
||||
using columns: [Column] = [])
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
association(to: Destination.self, on: joinCondition(columns))
|
||||
JoinAssociation(
|
||||
key: .inflected(Destination.databaseTableName),
|
||||
condition: .using(columns),
|
||||
relation: Destination.relationForAll)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ extension ColumnExpression {
|
||||
/// Instead, adopt the ColumnExpression protocol.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift#the-query-interface
|
||||
public struct Column: ColumnExpression {
|
||||
public struct Column: ColumnExpression, Equatable {
|
||||
/// The hidden rowID column
|
||||
public static let rowID = Column("rowid")
|
||||
|
||||
|
||||
@@ -523,6 +523,9 @@ enum SQLAssociationCondition {
|
||||
/// SELECT ... FROM author JOIN book ON author.id = book.authorId
|
||||
case foreignKey(request: SQLForeignKeyRequest, originIsLeft: Bool)
|
||||
|
||||
#warning("TODO: doc")
|
||||
case using([Column])
|
||||
|
||||
#warning("TODO: doc")
|
||||
case expression((TableAlias, TableAlias) -> SQLExpressible)
|
||||
|
||||
@@ -530,6 +533,8 @@ enum SQLAssociationCondition {
|
||||
switch self {
|
||||
case let .foreignKey(request: request, originIsLeft: originIsLeft):
|
||||
return .foreignKey(request: request, originIsLeft: !originIsLeft)
|
||||
case .using:
|
||||
return self
|
||||
case let .expression(condition):
|
||||
return .expression { condition($1, $0) }
|
||||
}
|
||||
@@ -756,6 +761,13 @@ extension SQLAssociationCondition {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
case let (.using(lhs), .using(rhs)):
|
||||
if lhs == rhs {
|
||||
return self
|
||||
} else {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
case let (.expression, .expression(rhs)):
|
||||
// Can't compare functions: the last one wins
|
||||
return .expression(rhs)
|
||||
|
||||
@@ -149,6 +149,10 @@ private func prefetch(_ db: Database, associations: [_SQLAssociation], in rows:
|
||||
// are not prefetched.
|
||||
fatalError("Not implemented: prefetch association without any foreign key")
|
||||
|
||||
case .using:
|
||||
#warning("TODD: prefetch on USING clause")
|
||||
fatalError("Not implemented")
|
||||
|
||||
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
|
||||
// Annotate prefetched rows with pivot columns, so that we can
|
||||
// group them.
|
||||
@@ -214,6 +218,10 @@ func prefetchedRegion(_ db: Database, associations: [_SQLAssociation]) throws ->
|
||||
// are not prefetched.
|
||||
fatalError("Not implemented: prefetch association without any foreign key")
|
||||
|
||||
case .using:
|
||||
#warning("TODD: prefetch region on USING clause")
|
||||
fatalError("Not implemented")
|
||||
|
||||
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
|
||||
// Filter the pivot on a `NullRow` in order to make sure all join
|
||||
// condition columns are made visible to SQLite, and present in the
|
||||
|
||||
@@ -821,22 +821,10 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
var joinExpressions: [SQLExpression]
|
||||
switch condition {
|
||||
case let .expression(condition):
|
||||
let conditionExpression = condition(leftAlias, rightAlias).sqlExpression
|
||||
#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]
|
||||
}
|
||||
joinExpressions = [condition(leftAlias, rightAlias).sqlExpression]
|
||||
|
||||
case let .using(columns):
|
||||
joinExpressions = columns.map { leftAlias[$0] == rightAlias[$0] }
|
||||
|
||||
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
|
||||
joinExpressions = try foreignKeyRequest
|
||||
|
||||
@@ -492,6 +492,10 @@ extension TableRecord where Self: EncodableRecord {
|
||||
// no public method builds an Association.
|
||||
fatalError("Not implemented: request association without any foreign key")
|
||||
|
||||
case .using:
|
||||
#warning("TODD: request on USING clause")
|
||||
fatalError("Not implemented")
|
||||
|
||||
case let .foreignKey(request: foreignKeyRequest, originIsLeft: originIsLeft):
|
||||
let destinationRelation = association
|
||||
._sqlAssociation
|
||||
|
||||
@@ -63,7 +63,7 @@ class CommonTableExpressionTests: GRDBTestCase {
|
||||
WITH "cte" AS (SELECT 'O''Brien') \
|
||||
SELECT "t".*, "cte".* \
|
||||
FROM "t" \
|
||||
JOIN "cte"
|
||||
JOIN "cte" ON 1
|
||||
""")
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ class CommonTableExpressionTests: GRDBTestCase {
|
||||
.commonTableExpression(tableName: "cte")
|
||||
let request = T.all()
|
||||
.with(cte)
|
||||
.including(required: T.association(to: cte, using: Column("id")))
|
||||
.including(required: T.association(to: cte, using: [Column("id")]))
|
||||
try assertEqualSQL(db, request, """
|
||||
WITH "cte" AS (SELECT 'O''Brien' AS id) \
|
||||
SELECT "t".*, "cte".* \
|
||||
@@ -148,6 +148,23 @@ class CommonTableExpressionTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
|
||||
// Include one CTE twice with same key and used columns
|
||||
do {
|
||||
enum CTE { }
|
||||
let cte: CommonTableExpression<CTE> = T.all()
|
||||
.commonTableExpression(tableName: "cte")
|
||||
let request = T.all()
|
||||
.with(cte)
|
||||
.including(required: T.association(to: cte, using: [Column("id")]))
|
||||
.including(required: T.association(to: cte, using: [Column("id")]))
|
||||
try assertEqualSQL(db, request, """
|
||||
WITH "cte" AS (SELECT * FROM "t") \
|
||||
SELECT "t".*, "cte".* \
|
||||
FROM "t" \
|
||||
JOIN "cte" ON "t"."id" = "cte"."id"
|
||||
""")
|
||||
}
|
||||
|
||||
// Include one CTE twice with same key but different condition (last condition wins)
|
||||
do {
|
||||
enum CTE { }
|
||||
@@ -172,14 +189,14 @@ 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: T.association(to: cte, using: [Column("id")]).forKey("a"))
|
||||
.including(required: T.association(to: cte, using: [Column("id")]).forKey("b"))
|
||||
try assertEqualSQL(db, request, """
|
||||
WITH "cte" AS (SELECT * FROM "t") \
|
||||
SELECT "t".*, "cte1".*, "cte2".* \
|
||||
FROM "t" \
|
||||
JOIN "cte" "cte1" ON "t"."id" > "cte1"."id" \
|
||||
JOIN "cte" "cte2" ON "t"."id" > "cte2"."id"
|
||||
JOIN "cte" "cte1" ON "t"."id" = "cte1"."id" \
|
||||
JOIN "cte" "cte2" ON "t"."id" = "cte2"."id"
|
||||
""")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user