Failing tests for HasManyThrough.first

This commit is contained in:
Gwendal Roué committed 2020-04-18 16:40:22 +02:00
1 parent 860faf19f7
commit f97088d63a
4 files changed
+422 -2

No files matched your search

+11 -1
View File
@@ -193,6 +193,10 @@
564C2451244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C244F244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift */; };
564C2452244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C244F244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift */; };
564C2453244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C244F244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift */; };
564C2459244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564C245A244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564C245B244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564C245C244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564CE43121AA901800652B19 /* ValueObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE43021AA901800652B19 /* ValueObserver.swift */; };
564CE43221AA901800652B19 /* ValueObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE43021AA901800652B19 /* ValueObserver.swift */; };
564CE43321AA901800652B19 /* ValueObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE43021AA901800652B19 /* ValueObserver.swift */; };
@@ -1249,6 +1253,7 @@
5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversion.swift; sourceTree = "<group>"; };
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
564C244F244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyFirstSQLTests.swift; sourceTree = "<group>"; };
564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughFirstSQLTests.swift; sourceTree = "<group>"; };
564CE43021AA901800652B19 /* ValueObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValueObserver.swift; sourceTree = "<group>"; };
564CE4E821B2E06F00652B19 /* ValueObservationMapTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationMapTests.swift; sourceTree = "<group>"; };
564CE59621B7A8B500652B19 /* RemoveDuplicates.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoveDuplicates.swift; sourceTree = "<group>"; };
@@ -1836,10 +1841,11 @@
5653EAC920944B4D00F46237 /* AssociationBelongsToSQLTests.swift */,
5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */,
5653EACF20944B4E00F46237 /* AssociationChainSQLTests.swift */,
561CFA9B2376EC86000C8BAA /* AssociationHasManyOrderingTests.swift */,
564C244F244A44B4003D7B05 /* AssociationHasManyFirstSQLTests.swift */,
561CFA9B2376EC86000C8BAA /* AssociationHasManyOrderingTests.swift */,
56057C4E2291B16900A7CB10 /* AssociationHasManyRowScopeTests.swift */,
56959632222D056D002CB7C9 /* AssociationHasManySQLTests.swift */,
564C2458244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */,
561CFA912376E546000C8BAA /* AssociationHasManyThroughOrderingTests.swift */,
56057C612291C7C600A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
56959615222C456C002CB7C9 /* AssociationHasManyThroughSQLTests.swift */,
@@ -2856,6 +2862,7 @@
5695310A1C9067DC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift in Sources */,
5695310F1C9067DC00CF1A2B /* StatementCrashTests.swift in Sources */,
5695310B1C9067DC00CF1A2B /* GRDBCrashTestCase.swift in Sources */,
564C245A244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
560A37AF1C90A8D800949E71 /* DatabasePoolCrashTests.swift in Sources */,
56553C101C3E906C00522B5C /* GRDBTestCase.swift in Sources */,
);
@@ -3066,6 +3073,7 @@
56EA869F1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */,
56677C16241D14450050755D /* FailableTestCase.swift in Sources */,
5615B288222B17C000061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
564C245B244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
567F45AC1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */,
5682D722239582AA004B58C4 /* DatabaseSuspensionTests.swift in Sources */,
5657AB621D108BA9006283EF /* FoundationNSURLTests.swift in Sources */,
@@ -3273,6 +3281,7 @@
56176C591EACCCC7000F3F2B /* FTS5CustomTokenizerTests.swift in Sources */,
56677C15241D14450050755D /* FailableTestCase.swift in Sources */,
5615B289222B17C000061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
564C2459244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
56D4968A1D81316E008276D7 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
5682D721239582AA004B58C4 /* DatabaseSuspensionTests.swift in Sources */,
56D496601D81304E008276D7 /* FoundationNSUUIDTests.swift in Sources */,
@@ -3609,6 +3618,7 @@
AAA4DD4D230F262000C74B15 /* DatabasePoolReadOnlyTests.swift in Sources */,
56677C17241D14450050755D /* FailableTestCase.swift in Sources */,
AAA4DD4E230F262000C74B15 /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
564C245C244A466A003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
AAA4DD4F230F262000C74B15 /* TruncateOptimizationTests.swift in Sources */,
5682D723239582AA004B58C4 /* DatabaseSuspensionTests.swift in Sources */,
AAA4DD50230F262000C74B15 /* FoundationNSURLTests.swift in Sources */,
+6 -1
View File
@@ -98,7 +98,12 @@ public /* TODO: internal */ struct SQLAssociation {
}
func associationForFirst() -> Self {
SQLAssociation(steps: steps.map { $0.with(\.firstOnly, true) })
SQLAssociation(steps: steps.map { step in
switch step.cardinality {
case .toMany: return step.with(\.firstOnly, true)
case .toOne: return step
}
})
}
/// Given an origin alias and rows, returns the destination of the
+6
View File
@@ -109,6 +109,8 @@
564B3D73239BDBD6007BF308 /* DatabaseSuspensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564B3D70239BDBD6007BF308 /* DatabaseSuspensionTests.swift */; };
564C2456244A44F8003D7B05 /* AssociationHasManyFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2454244A44F7003D7B05 /* AssociationHasManyFirstSQLTests.swift */; };
564C2457244A44F8003D7B05 /* AssociationHasManyFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C2454244A44F7003D7B05 /* AssociationHasManyFirstSQLTests.swift */; };
564C245E244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C245D244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564C245F244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564C245D244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */; };
564CE43C21AA955B00652B19 /* ValueObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE43B21AA955B00652B19 /* ValueObserver.swift */; };
564CE43D21AA955B00652B19 /* ValueObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE43B21AA955B00652B19 /* ValueObserver.swift */; };
564CE4E321B2E05400652B19 /* ValueObservationMapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564CE4E221B2E05400652B19 /* ValueObservationMapTests.swift */; };
@@ -790,6 +792,7 @@
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
564B3D70239BDBD6007BF308 /* DatabaseSuspensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSuspensionTests.swift; sourceTree = "<group>"; };
564C2454244A44F7003D7B05 /* AssociationHasManyFirstSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyFirstSQLTests.swift; sourceTree = "<group>"; };
564C245D244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughFirstSQLTests.swift; sourceTree = "<group>"; };
564CE43B21AA955B00652B19 /* ValueObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObserver.swift; sourceTree = "<group>"; };
564CE4E221B2E05400652B19 /* ValueObservationMapTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationMapTests.swift; sourceTree = "<group>"; };
564CE5B521B8FBEA00652B19 /* DatabaseRegionObservation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRegionObservation.swift; sourceTree = "<group>"; };
@@ -1346,6 +1349,7 @@
561CFA9F2376EF4F000C8BAA /* AssociationHasManyOrderingTests.swift */,
56057C572291B18E00A7CB10 /* AssociationHasManyRowScopeTests.swift */,
56959635222D058B002CB7C9 /* AssociationHasManySQLTests.swift */,
564C245D244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift */,
561CFAA32376EF59000C8BAA /* AssociationHasManyThroughOrderingTests.swift */,
56057C642291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift */,
5695961E222C4589002CB7C9 /* AssociationHasManyThroughSQLTests.swift */,
@@ -2345,6 +2349,7 @@
F3BA80AF1CFB2FB1003DC1BA /* DatabaseCollationTests.swift in Sources */,
56CC9235201E009100CB597E /* DropWhileCursorTests.swift in Sources */,
567E4209242AB3CB00CAAD2C /* FailableTestCase.swift in Sources */,
564C245F244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
56D507651F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift in Sources */,
5615B286222B17BA00061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
5657AB651D108BA9006283EF /* FoundationNSURLTests.swift in Sources */,
@@ -2681,6 +2686,7 @@
56B964DB1DA5216B0002DA19 /* FTS5RecordTests.swift in Sources */,
56CC9234201E009000CB597E /* DropWhileCursorTests.swift in Sources */,
567E420A242AB3CB00CAAD2C /* FailableTestCase.swift in Sources */,
564C245E244A468C003D7B05 /* AssociationHasManyThroughFirstSQLTests.swift in Sources */,
56D507611F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift in Sources */,
5615B285222B17BA00061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
F3BA80B01CFB2FB2003DC1BA /* DatabaseCollationTests.swift in Sources */,
@@ -0,0 +1,399 @@
import XCTest
import GRDB
/// Test SQL generation
class AssociationHasManyThroughFirstSQLTests: GRDBTestCase {
func testBelongsToHasMany() throws {
struct A: TableRecord, EncodableRecord {
static let b = belongsTo(B.self)
static let c = hasMany(C.self, through: b, using: B.c)
func encode(to container: inout PersistenceContainer) {
container["bId"] = 1
}
}
struct B: TableRecord {
static let c = hasMany(C.self)
}
struct C: TableRecord {
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "b") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "a") { t in
t.autoIncrementedPrimaryKey("id")
t.column("bId").references("b")
}
try db.create(table: "c") { t in
t.autoIncrementedPrimaryKey("id")
t.column("bId").references("b")
}
do {
let association = A.c.first
try assertEqualSQL(db, A.all().including(required: association), """
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON "b"."id" = "a"."bId" \
JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().including(optional: association), """
SELECT "a".*, "c".* \
FROM "a" \
LEFT JOIN "b" ON "b"."id" = "a"."bId" \
LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(required: association), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON "b"."id" = "a"."bId" \
JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(optional: association), """
SELECT "a".* \
FROM "a" \
LEFT JOIN "b" ON "b"."id" = "a"."bId" \
LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A().request(for: association), """
SELECT "c".* \
FROM "c" \
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1) \
LIMIT 1
""")
}
}
}
func testHasOneHasMany() throws {
struct A: TableRecord, EncodableRecord {
static let b = hasOne(B.self)
static let c = hasMany(C.self, through: b, using: B.c)
func encode(to container: inout PersistenceContainer) {
container["id"] = 1
}
}
struct B: TableRecord {
static let c = hasMany(C.self)
}
struct C: TableRecord {
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "a") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "b") { t in
t.autoIncrementedPrimaryKey("id")
t.column("aId").references("a")
}
try db.create(table: "c") { t in
t.autoIncrementedPrimaryKey("id")
t.column("bId").references("b")
}
do {
let association = A.c.first
try assertEqualSQL(db, A.all().including(required: association), """
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON "b"."aId" = "a"."id" \
JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().including(optional: association), """
SELECT "a".*, "c".* \
FROM "a" \
LEFT JOIN "b" ON "b"."aId" = "a"."id" \
LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(required: association), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON "b"."aId" = "a"."id" \
JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(optional: association), """
SELECT "a".* \
FROM "a" \
LEFT JOIN "b" ON "b"."aId" = "a"."id" \
LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1)
""")
try assertEqualSQL(db, A().request(for: association), """
SELECT "c".* \
FROM "c" \
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1) LIMIT 1
""")
}
}
}
func testHasManyBelongsTo() throws {
struct A: TableRecord, EncodableRecord {
static let b = hasMany(B.self)
static let c = hasMany(C.self, through: b, using: B.c)
func encode(to container: inout PersistenceContainer) {
container["id"] = 1
}
}
struct B: TableRecord {
static let c = belongsTo(C.self)
}
struct C: TableRecord {
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "a") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "c") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "b") { t in
t.autoIncrementedPrimaryKey("id")
t.column("cId").references("c")
t.column("aId").references("a")
}
do {
let association = A.c.first
try assertEqualSQL(db, A.all().including(required: association), """
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) \
JOIN "c" ON "c"."id" = "b"."cId"
""")
try assertEqualSQL(db, A.all().including(optional: association), """
SELECT "a".*, "c".* \
FROM "a" \
LEFT JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" 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 assertEqualSQL(db, A.all().joining(required: association), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(optional: association), """
SELECT "a".* \
FROM "a" \
LEFT JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1)
""")
// TODO: is is possible?
// TODO: test request(for: hasManyThrough(hasMany.first, hasMany) (all comments of the last message in a chat)
try assertEqualSQL(db, A().request(for: association), """
SELECT "c".* FROM "c" JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1) LIMIT 1
""")
}
}
}
func testHasManyHasOne() throws {
struct A: TableRecord, EncodableRecord {
static let b = hasMany(B.self)
static let c = hasMany(C.self, through: b, using: B.c)
func encode(to container: inout PersistenceContainer) {
container["id"] = 1
}
}
struct B: TableRecord {
static let c = hasOne(C.self)
}
struct C: TableRecord {
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "a") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "b") { t in
t.autoIncrementedPrimaryKey("id")
t.column("aId").references("a")
}
try db.create(table: "c") { t in
t.autoIncrementedPrimaryKey("id")
t.column("bId").references("b")
}
do {
let association = A.c.first
try assertEqualSQL(db, A.all().including(required: association), """
SELECT "parent".*, "child".* \
FROM "parent" \
JOIN "child" ON "child"."rowid" = (\
SELECT "child"."rowid" \
FROM "child" \
WHERE "child"."parentId" = "parent"."id" \
ORDER BY "child"."id" \
LIMIT 1)
""")
try assertEqualSQL(db, A.all().including(optional: association), """
SELECT "parent".*, "child".* \
FROM "parent" \
LEFT JOIN "child" ON "child"."rowid" = (\
SELECT "child"."rowid" \
FROM "child" \
WHERE "child"."parentId" = "parent"."id" \
ORDER BY "child"."id" \
LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(required: association), """
SELECT "parent".* \
FROM "parent" \
JOIN "child" ON "child"."rowid" = (\
SELECT "child"."rowid" \
FROM "child" \
WHERE "child"."parentId" = "parent"."id" \
ORDER BY "child"."id" \
LIMIT 1)
""")
try assertEqualSQL(db, A.all().joining(optional: association), """
SELECT "parent".* \
FROM "parent" \
LEFT JOIN "child" ON "child"."rowid" = (\
SELECT "child"."rowid" \
FROM "child" \
WHERE "child"."parentId" = "parent"."id" \
ORDER BY "child"."id" \
LIMIT 1)
""")
try assertEqualSQL(db, A().request(for: association), """
SELECT * FROM "child" \
WHERE "parentId" = 1 \
ORDER BY "id" \
LIMIT 1
""")
}
}
}
func testAssociationFilteredByOtherAssociation() throws {
struct Pet: TableRecord {
static let child = belongsTo(Child.self)
}
struct Toy: TableRecord { }
struct Child: TableRecord {
static let toy = hasOne(Toy.self)
static let pets = hasMany(Pet.self)
}
struct Parent: TableRecord, EncodableRecord {
static let children = hasMany(Child.self)
func encode(to container: inout PersistenceContainer) {
container["id"] = 1
}
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "parent") { t in
t.autoIncrementedPrimaryKey("id")
}
try db.create(table: "child") { t in
t.column("parentId", .integer).references("parent")
}
try db.create(table: "toy") { t in
t.column("childId", .integer).references("child")
}
try db.create(table: "pet") { t in
t.column("childId", .integer).references("child")
}
}
try dbQueue.inDatabase { db in
do {
let association = Parent.hasMany(
Pet.self,
through: Parent.children.joining(required: Child.toy),
using: Child.pets)
.first
try assertEqualSQL(db, Parent.all().including(required: association), """
SELECT "parent".*, "pet".* \
FROM "parent" \
JOIN "child" ON "child"."parentId" = "parent"."id" \
JOIN "toy" ON "toy"."childId" = "child"."rowid" \
JOIN "pet" ON "pet"."childId" = "child"."rowid"
""")
try assertEqualSQL(db, Parent.all().joining(required: association), """
SELECT "parent".* \
FROM "parent" \
JOIN "child" ON "child"."parentId" = "parent"."id" \
JOIN "toy" ON "toy"."childId" = "child"."rowid" \
JOIN "pet" ON "pet"."childId" = "child"."rowid"
""")
try assertEqualSQL(db, Parent().request(for: association), """
SELECT "pet".* \
FROM "pet" \
JOIN "child" ON ("child"."rowid" = "pet"."childId") AND ("child"."parentId" = 1) \
JOIN "toy" ON "toy"."childId" = "child"."rowid"
""")
}
do {
let association = Parent.hasMany(
Pet.self,
through: Parent.children.filter(sql: "1 + 1"),
using: Child.pets.joining(required: Pet.child.filter(sql: "1").joining(required: Child.toy)))
.first
try assertEqualSQL(db, Parent.all().including(required: association), """
SELECT "parent".*, "pet".* \
FROM "parent" \
JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \
JOIN "pet" ON "pet"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child2"."rowid"
""")
try assertEqualSQL(db, Parent.all().joining(required: association), """
SELECT "parent".* \
FROM "parent" \
JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \
JOIN "pet" ON "pet"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child2"."rowid"
""")
try assertEqualSQL(db, Parent().request(for: association), """
SELECT "pet".* \
FROM "pet" \
JOIN "child" "child1" ON ("child1"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1)
""")
}
do {
let association = Parent.hasMany(
Pet.self,
through: Parent.children.filter(sql: "1 + 1"),
using: Child.pets)
.joining(required: Pet.child.filter(sql: "1").joining(required: Child.toy))
.first
try assertEqualSQL(db, Parent.all().including(required: association), """
SELECT "parent".*, "pet".* \
FROM "parent" \
JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \
JOIN "pet" ON "pet"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child2"."rowid"
""")
try assertEqualSQL(db, Parent.all().joining(required: association), """
SELECT "parent".* \
FROM "parent" \
JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \
JOIN "pet" ON "pet"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child2"."rowid"
""")
try assertEqualSQL(db, Parent().request(for: association), """
SELECT "pet".* \
FROM "pet" \
JOIN "child" "child1" ON ("child1"."rowid" = "pet"."childId") AND (1) \
JOIN "toy" ON "toy"."childId" = "child1"."rowid" \
JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1)
""")
}
}
}
}