mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Back to basics
This commit is contained in:
1 parent
c966eb3ecf
commit
c73fb0dd9b
3 files changed
+229
-450
No files matched your search
@@ -611,6 +611,18 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
enum Kind: String {
|
||||
case leftJoin = "LEFT JOIN"
|
||||
case innerJoin = "JOIN"
|
||||
|
||||
init?(_ kind: SQLRelation.Child.Kind) {
|
||||
switch kind {
|
||||
case .oneRequired:
|
||||
self = .innerJoin
|
||||
case .oneOptional:
|
||||
self = .leftJoin
|
||||
case .allPrefetched, .allNotPrefetched:
|
||||
// This relation child is not fetched with an SQL join.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Target {
|
||||
@@ -624,42 +636,44 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
var target: Target
|
||||
|
||||
init?(_ child: SQLRelation.Child) {
|
||||
switch child.kind {
|
||||
case .oneRequired:
|
||||
kind = .innerJoin
|
||||
case .oneOptional:
|
||||
kind = .leftJoin
|
||||
case .allPrefetched, .allNotPrefetched:
|
||||
// This relation child is not fetched with an SQL join.
|
||||
guard let kind = Kind(child.kind) else {
|
||||
return nil
|
||||
}
|
||||
self.kind = kind
|
||||
self.condition = child.condition
|
||||
|
||||
condition = child.condition
|
||||
// Remove non-joined grand children
|
||||
let relation = child.relation.filteringChildren { Kind($0.kind) != nil }
|
||||
|
||||
if child.relation.firstOnly {
|
||||
// Filters and order are handled in a subRelation.
|
||||
// We only keep children that need to be exposed in the outer
|
||||
// query (those with a non-empty selection).
|
||||
#warning("TODO outerRelation 1")
|
||||
// TODO: we may have a problem if we remove children used for
|
||||
// ordering or filtering with aliases.
|
||||
#warning("TODO outerRelation 2")
|
||||
// TODO: keep children that have children with selection
|
||||
#warning("TODO target")
|
||||
// TODO: in the target remove to-many children
|
||||
let outerRelation = child.relation
|
||||
if relation.firstOnly {
|
||||
// Split relation into an "outer" relation exposed to the rest of
|
||||
// the query, and a "subRelation", private to the join, limited
|
||||
// to a single row:
|
||||
//
|
||||
// v--- outer relation
|
||||
// JOIN child ON id = (SELECT id FROM child ... LIMIT 1)
|
||||
// ^--- subRelation
|
||||
//
|
||||
// Outer relation has no filter or ordering: those are handled
|
||||
// by the subRelation.
|
||||
|
||||
guard relation.children.isEmpty else {
|
||||
// TODO: handle children
|
||||
fatalError("Not implemented: associating records to a `first` or `last` to-one association")
|
||||
}
|
||||
|
||||
let outerRelation = relation
|
||||
.with(\.firstOnly, false)
|
||||
.unfiltered()
|
||||
.unordered()
|
||||
.filteringChildren { $0.relation.selection.isEmpty == false }
|
||||
|
||||
let subRelation = child.relation
|
||||
let subRelation = relation
|
||||
|
||||
relation = SQLQualifiedRelation(outerRelation)
|
||||
target = .subRelation(subRelation)
|
||||
self.relation = SQLQualifiedRelation(outerRelation)
|
||||
self.target = .subRelation(subRelation)
|
||||
} else {
|
||||
relation = SQLQualifiedRelation(child.relation)
|
||||
target = .all
|
||||
self.relation = SQLQualifiedRelation(child.relation)
|
||||
self.target = .all
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,50 +107,26 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase {
|
||||
.orderByPrimaryKey()
|
||||
.first
|
||||
.joining(required: Child.hasOne(Toy.self))
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "child".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().including(optional: association), """
|
||||
SELECT "parent".*, "child".*
|
||||
FROM "parent"
|
||||
LEFT JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(optional: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
LEFT JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "child".*
|
||||
FROM "child"
|
||||
@@ -167,28 +143,16 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase {
|
||||
.orderByPrimaryKey()
|
||||
.first
|
||||
.including(required: Child.hasOne(Toy.self))
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "child".*, "toy".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id" LIMIT 1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*, "toy".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id" LIMIT 1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "child".*, "toy".*
|
||||
FROM "child"
|
||||
@@ -200,69 +164,39 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
let alias = TableAlias()
|
||||
let association = Parent
|
||||
.hasMany(Child.self)
|
||||
.orderByPrimaryKey()
|
||||
.filter(Column("id") == alias[Column("id")] + 1)
|
||||
.first
|
||||
.joining(required: Child.hasOne(Toy.self).filter(Column("id") == alias[Column("id")] * 2))
|
||||
try assertMatchSQL(db, Parent.all().aliased(alias).including(required: association), """
|
||||
SELECT "parent".*, "child".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1))
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().aliased(alias).joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1))
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// let alias = TableAlias()
|
||||
// let association = Parent
|
||||
// .hasMany(Child.self)
|
||||
// .orderByPrimaryKey()
|
||||
// .filter(Column("id") == alias[Column("id")] + 1)
|
||||
// .first
|
||||
// .joining(required: Child.hasOne(Toy.self).filter(Column("id") == alias[Column("id")] * 2))
|
||||
// try assertMatchSQL(db, Parent.all().aliased(alias).including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, Parent.all().aliased(alias).joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
}
|
||||
|
||||
do {
|
||||
let alias = TableAlias()
|
||||
let association = Parent
|
||||
.hasMany(Child.self)
|
||||
.orderByPrimaryKey()
|
||||
.filter(Column("id") == alias[Column("id")] + 1)
|
||||
.first
|
||||
.including(required: Child.hasOne(Toy.self).filter(Column("id") == alias[Column("id")] * 2))
|
||||
try assertMatchSQL(db, Parent.all().aliased(alias).including(required: association), """
|
||||
SELECT "parent".*, "child".*, "toy".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1))
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().aliased(alias).joining(required: association), """
|
||||
SELECT "parent".*, "toy".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1))
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2))
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// let alias = TableAlias()
|
||||
// let association = Parent
|
||||
// .hasMany(Child.self)
|
||||
// .orderByPrimaryKey()
|
||||
// .filter(Column("id") == alias[Column("id")] + 1)
|
||||
// .first
|
||||
// .including(required: Child.hasOne(Toy.self).filter(Column("id") == alias[Column("id")] * 2))
|
||||
// try assertMatchSQL(db, Parent.all().aliased(alias).including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, Parent.all().aliased(alias).joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,55 +236,26 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase {
|
||||
.orderByPrimaryKey()
|
||||
.first
|
||||
.joining(required: Child.hasOne(Toy.self).including(optional: Toy.belongsTo(Vendor.self)))
|
||||
XCTFail("TODO: fix SQL")
|
||||
// TODO: vendor is included and should be present in the selection
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "child".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().including(optional: association), """
|
||||
SELECT "parent".*, "child".*
|
||||
FROM "parent"
|
||||
LEFT JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id" LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(optional: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
LEFT JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "child".*, "vendor".*
|
||||
FROM "child"
|
||||
@@ -368,35 +273,16 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase {
|
||||
.orderByPrimaryKey()
|
||||
.first
|
||||
.including(required: Child.hasOne(Toy.self).including(optional: Toy.belongsTo(Vendor.self)))
|
||||
XCTFail("TODO: check SQL")
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "child".*, "toy".*, "vendor".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*, "toy".*, "vendor".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
ORDER BY "child"."id"
|
||||
LIMIT 1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
LEFT JOIN "vendor" ON "vendor"."id" = "toy"."vendorId"
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "child".*, "toy".*, "vendor".*
|
||||
FROM "child"
|
||||
|
||||
@@ -163,28 +163,30 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
let association = A.c.first
|
||||
try assertMatchSQL(db, A.all().including(required: association), """
|
||||
SELECT "a".*, "c".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
JOIN "c" ON "c"."id" = "b"."cId"
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
SELECT "a".*, "c".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
LEFT JOIN "c" ON "c"."id" = "b"."cId"
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, A.all().including(required: association), """
|
||||
// SELECT "a".*, "c".*
|
||||
// FROM "a"
|
||||
// JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// JOIN "c" ON "c"."id" = "b"."cId"
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
// SELECT "a".*, "c".*
|
||||
// FROM "a"
|
||||
// LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// LEFT JOIN "c" ON "c"."id" = "b"."cId"
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
// SELECT "a".*
|
||||
// FROM "a"
|
||||
// JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
// SELECT "a".*
|
||||
// FROM "a"
|
||||
// LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// """)
|
||||
try assertMatchSQL(db, A().request(for: association), """
|
||||
SELECT "c".*
|
||||
FROM "c"
|
||||
@@ -225,28 +227,30 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
let association = A.c.first
|
||||
try assertMatchSQL(db, A.all().including(required: association), """
|
||||
SELECT "a".*, "c".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
JOIN "c" ON "c"."bId" = "b"."id"
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
SELECT "a".*, "c".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
LEFT JOIN "c" ON "c"."bId" = "b"."id"
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, A.all().including(required: association), """
|
||||
// SELECT "a".*, "c".*
|
||||
// FROM "a"
|
||||
// JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// JOIN "c" ON "c"."bId" = "b"."id"
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
// SELECT "a".*, "c".*
|
||||
// FROM "a"
|
||||
// LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// LEFT JOIN "c" ON "c"."bId" = "b"."id"
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
// SELECT "a".*
|
||||
// FROM "a"
|
||||
// JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
// SELECT "a".*
|
||||
// FROM "a"
|
||||
// LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."bId" = "b"."id" WHERE "b"."aId" = "a"."id" LIMIT 1)
|
||||
// """)
|
||||
try assertMatchSQL(db, A().request(for: association), """
|
||||
SELECT "c".*
|
||||
FROM "c"
|
||||
@@ -294,58 +298,21 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
let association = A.d
|
||||
XCTFail("TODO: fix generated SQL")
|
||||
// TODO: remove the d from the subquery
|
||||
try assertMatchSQL(db, A.all().including(required: association), """
|
||||
SELECT "a".*, "d".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = "a"."bId"
|
||||
JOIN "c" ON "c"."id" = (
|
||||
SELECT "c"."id"
|
||||
FROM "c"
|
||||
JOIN "d" ON "d"."cId" = "c"."id"
|
||||
WHERE "c"."bId" = "b"."id"
|
||||
LIMIT 1)
|
||||
JOIN "d" ON "d"."cId" = "c"."id"
|
||||
""")
|
||||
// TODO: remove the d from the subquery
|
||||
try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
SELECT "a".*, "d".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = "a"."bId"
|
||||
LEFT JOIN "c" ON "c"."id" = (
|
||||
SELECT "c"."id"
|
||||
FROM "c"
|
||||
LEFT JOIN "d" ON "d"."cId" = "c"."id"
|
||||
WHERE "c"."bId" = "b"."id"
|
||||
LIMIT 1)
|
||||
LEFT JOIN "d" ON "d"."cId" = "c"."id"
|
||||
""")
|
||||
// TODO: remove the d from the subquery
|
||||
// TODO: include the d in the outer query
|
||||
try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
JOIN "b" ON "b"."id" = "a"."bId"
|
||||
JOIN "c" ON "c"."id" = (
|
||||
SELECT "c"."id"
|
||||
FROM "c"
|
||||
JOIN "d" ON "d"."cId" = "c"."id"
|
||||
WHERE "c"."bId" = "b"."id" LIMIT 1)
|
||||
""")
|
||||
// TODO: remove the d from the subquery
|
||||
// TODO: include the d in the outer query
|
||||
try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
SELECT "a".*
|
||||
FROM "a"
|
||||
LEFT JOIN "b" ON "b"."id" = "a"."bId"
|
||||
LEFT JOIN "c" ON "c"."id" = (
|
||||
SELECT "c"."id"
|
||||
FROM "c"
|
||||
LEFT JOIN "d" ON "d"."cId" = "c"."id"
|
||||
WHERE "c"."bId" = "b"."id" LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// let association = A.d
|
||||
// try assertMatchSQL(db, A.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().including(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, A.all().joining(optional: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: loading multiple records associated to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, A().request(for: association), """
|
||||
@@ -390,51 +357,22 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
do {
|
||||
let association = Parent.hasMany(
|
||||
Pet.self,
|
||||
through: Parent.children.joining(required: Child.toy),
|
||||
using: Child.pets)
|
||||
.first
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "pet".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet" WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
LIMIT 1)
|
||||
JOIN "pet" ON "pet"."rowid" = (SELECT "pet"."rowid" FROM "pet" WHERE "pet"."childId" = "child"."id" LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet" WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE "child"."parentId" = "parent"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "pet".*
|
||||
FROM "pet"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE ("child"."id" = "pet"."childId") AND ("child"."parentId" = 1)
|
||||
LIMIT 1)
|
||||
LIMIT 1
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// let association = Parent.hasMany(
|
||||
// Pet.self,
|
||||
// through: Parent.children.joining(required: Child.toy),
|
||||
// using: Child.pets)
|
||||
// .first
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// try assertMatchSQL(db, Parent().request(for: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
}
|
||||
do {
|
||||
let association = Parent.hasMany(
|
||||
@@ -442,45 +380,16 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
through: Parent.children.filter(sql: "1 + 1"),
|
||||
using: Child.pets.joining(required: Pet.child.filter(sql: "1").joining(required: Child.toy)))
|
||||
.first
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "pet".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE ("child"."parentId" = "parent"."id") AND (1 + 1)
|
||||
LIMIT 1)
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE ("child"."parentId" = "parent"."id") AND (1 + 1)
|
||||
LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "pet".*
|
||||
FROM "pet"
|
||||
@@ -501,46 +410,16 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
|
||||
using: Child.pets)
|
||||
.joining(required: Pet.child.filter(sql: "1").joining(required: Child.toy))
|
||||
.first
|
||||
try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
SELECT "parent".*, "pet".*
|
||||
FROM "parent"
|
||||
JOIN "child" ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE ("child"."parentId" = "parent"."id") AND (1 + 1)
|
||||
LIMIT 1)
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
""")
|
||||
try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
SELECT "parent".*
|
||||
FROM "parent"
|
||||
JOIN "child"
|
||||
ON "child"."id" = (
|
||||
SELECT "child"."id"
|
||||
FROM "child"
|
||||
JOIN "pet" ON "pet"."rowid" = (
|
||||
SELECT "pet"."rowid"
|
||||
FROM "pet"
|
||||
JOIN "child" ON ("child"."id" = "pet"."childId") AND (1)
|
||||
JOIN "toy" ON "toy"."childId" = "child"."id"
|
||||
WHERE "pet"."childId" = "child"."id"
|
||||
LIMIT 1)
|
||||
WHERE ("child"."parentId" = "parent"."id") AND (1 + 1)
|
||||
LIMIT 1)
|
||||
""")
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().including(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
// TODO: implement
|
||||
// Not implemented: associating records to a `first` or `last` to-one association
|
||||
// try assertMatchSQL(db, Parent.all().joining(required: association), """
|
||||
// SELECT ...
|
||||
// """)
|
||||
try assertMatchSQL(db, Parent().request(for: association), """
|
||||
SELECT "pet".*
|
||||
FROM "pet"
|
||||
|
||||
Reference in new issue
Block a user