Remove a hack: merging hasMany and hasManyThrough with include(all:) is simply not supported yet

This commit is contained in:
Gwendal Roué committed 2019-05-20 19:03:43 +02:00
1 parent 6f6a8e3c3b
commit 69baff31ef
8 files changed
+46 -670

No files matched your search

+19
View File
@@ -2271,6 +2271,25 @@ let request = Author
Come [discuss](http://twitter.com/groue) for more information, or if you wish to help turning those features into reality.
**You can't use the `including(all:)` method with a [HasMany] and a [HasManyThrough] associations that shares the same base association in the same request:
```swift
// NOT IMPLEMENTED
let request = Country
.including(all: Country.passports)
.including(all: Country.citizens)
```
This code compiles, but you'll get a runtime fatal error "Not implemented: merging a direct association and an indirect one with including(all:)". Future versions of GRDB may allow such requests.
The workaround is to nest the most remote association:
```swift
// Workaround
let request = Country
.including(all: Country.passports
.including(required: Passport.citizen))
```
---
@@ -673,13 +673,6 @@ public /* TODO: internal */ struct SQLAssociation {
return result
}
/// Changes the pivot key
func forPivotKey(_ key: SQLAssociationKey) -> SQLAssociation {
var result = self
result.pivot.key = key
return result
}
/// Transforms the destination relation
func mapDestinationRelation(_ transform: (SQLRelation) -> SQLRelation) -> SQLAssociation {
var result = self
@@ -792,52 +785,7 @@ public /* TODO: internal */ struct SQLAssociation {
// static let citizens = hasMany(Citizens.self, through: passports, using: Passport.citizen)
// }
// let request = Country.including(all: Country.citizens)
//
// Also, pick a unique pivot key.
//
// Why? Consider this request:
//
// let request = Country
// .including(all: Country.passports.filter(Column("isExpired") == true))
// .including(all: Country.citizens)
//
// A unique key makes the citizens' passports distinct from the
// expired passports. This has two desirable consequences:
//
// 1. As expected (since Country.citizens is included from Country),
// it attaches prefetched citizens to countries, not
// to passports.
// 2. It loads all citizens, not only citizens who have an expired
// passport. This is debatable actually, because unlike
// prefetches, joins are merged by keys. Joins require user to
// provide explicit disambiguation keys in order to prevent
// merging. But this inconsistency really helps our
// implementation here. And it's not sure it is very wrong, from
// the user's point of view.
//
// On the other side, if we would NOT pick a unique pivot key, then
// our current implementation makes the above request equivalent to
// the one below, which attaches citizens to passports, not
// to countries:
//
// let request = Country
// .including(all: Country.passports
// .filter(Column("isExpired") == true)
// .including(all: Passport.citizens))
//
// 1. It attaches citizens to passports, not to countries (BUG).
// 2. When we fetch citizens, we generate an SQL query which does
// not contain country columns: we can not dispatch citizens
// in countries when we have to (BUG).
// 3. It fetches citizens who have an expired passport (we have
// decided it's a BUG).
//
// Conclusion: for better or for worse, let's pick a unique pivot
// key, and prevent merging of intermediate steps of
// indirect associations:
return reducedAssociation
.forPivotKey(SQLAssociationKey.fixed("grdb_\(UUID().uuidString)"))
.extendedRelation(origin, kind: .allNotPrefetched)
return reducedAssociation.extendedRelation(origin, kind: .allNotPrefetched)
}
}
+11 -6
View File
@@ -644,16 +644,21 @@ extension SQLRelation.Child.Kind {
// .including(optional: association)
return .oneOptional
case (.allPrefetched, .allPrefetched),
(.allPrefetched, .allNotPrefetched),
(.allNotPrefetched, .allPrefetched):
// Prefetches both Pivot and Destination:
case (.allPrefetched, .allPrefetched):
// Equivalent to Record.including(all: association):
//
// Record
// .including(all: association)
// .including(all: association)
return .allPrefetched
case (.allPrefetched, .allNotPrefetched),
(.allNotPrefetched, .allPrefetched):
// Record
// .including(all: associationToDestinationThroughPivot)
// .including(all: associationToPivot)
return .allPrefetched
fatalError("Not implemented: merging a direct association and an indirect one with including(all:)")
case (.allNotPrefetched, .allNotPrefetched):
// Equivalent to Record.including(all: association)
//
@@ -727,16 +727,16 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase {
let request = A
.filter(Column("cola1") != 3)
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8).forKey("cs1"), using: C.hasMany(D.self))
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds3"))
@@ -791,155 +791,6 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase {
}
}
func testIncludingAllHasManyThroughIsNotMergedWithHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
// Plain request
do {
let cs = A.hasMany(C.self)
let request = A
.including(all: cs.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs, using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
struct Record: FetchableRecord, Decodable, Equatable {
var a: A
var cs: [C]
var ds: [D]
}
// Record.fetchAll
do {
let records = try Record.fetchAll(db, request)
XCTAssertEqual(records, [
Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
]),
Record(
a: A(row: ["cola1": 2, "cola2": "a2"]),
cs: [
C(row: ["colc1": 8, "colc2": 2]),
C(row: ["colc1": 9, "colc2": 2]),
],
ds: [
D(row: ["cold1": 11, "cold2": 8, "cold3": "d2"]),
D(row: ["cold1": 12, "cold2": 8, "cold3": "d3"]),
D(row: ["cold1": 13, "cold2": 9, "cold3": "d4"]),
]),
Record(
a: A(row: ["cola1": 3, "cola2": "a3"]),
cs: [],
ds: []),
])
}
// Record.fetchOne
do {
let record = try Record.fetchOne(db, request)!
XCTAssertEqual(record, Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
]))
}
}
// Request with filters
do {
let cs1 = A.hasMany(C.self).forKey("cs1")
let cs2 = A.hasMany(C.self).forKey("cs2")
let request = A
.filter(Column("cola1") != 3)
.including(all: cs1
.filter(Column("colc1") != 8)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs1, using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: cs2
.filter(Column("colc1") != 9)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs2, using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds2"))
.orderByPrimaryKey()
struct Record: FetchableRecord, Decodable, Equatable {
var a: A
var cs1: [C]
var cs2: [C]
var ds1: [D]
var ds2: [D]
}
// Record.fetchAll
do {
let records = try Record.fetchAll(db, request)
XCTAssertEqual(records, [
Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs1: [
C(row: ["colc1": 7, "colc2": 1]),
],
cs2: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds1: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
],
ds2: []),
Record(
a: A(row: ["cola1": 2, "cola2": "a2"]),
cs1: [
C(row: ["colc1": 9, "colc2": 2]),
],
cs2: [
C(row: ["colc1": 8, "colc2": 2]),
],
ds1: [
D(row: ["cold1": 12, "cold2": 8, "cold3": "d3"]),
D(row: ["cold1": 13, "cold2": 9, "cold3": "d4"]),
],
ds2: [
D(row: ["cold1": 11, "cold2": 8, "cold3": "d2"]),
]),
])
}
// Record.fetchOne
do {
let record = try Record.fetchOne(db, request)!
XCTAssertEqual(record, Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs1: [
C(row: ["colc1": 7, "colc2": 1]),
],
cs2: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds1: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
],
ds2: []))
}
}
}
}
// TODO: make a variant with joining(optional:)
func testIncludingOptionalBelongsToIncludingAllHasMany() throws {
let dbQueue = try makeDatabaseQueue()
@@ -898,16 +898,16 @@ class AssociationPrefetchingFetchableRecordTests: GRDBTestCase {
let request = A
.filter(Column("cola1") != 3)
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8).forKey("cs1"), using: C.hasMany(D.self))
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds3"))
@@ -973,177 +973,6 @@ class AssociationPrefetchingFetchableRecordTests: GRDBTestCase {
}
}
func testIncludingAllHasManyThroughIsNotMergedWithHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
// Plain request
do {
let cs = A.hasMany(C.self)
let request = A
.including(all: cs.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs, using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
struct Record: FetchableRecord, Equatable {
var a: A
var cs: [C]
var ds: [D]
init(a: A, cs: [C], ds: [D]) {
self.a = a
self.cs = cs
self.ds = ds
}
init(row: Row) {
self.init(a: A(row: row), cs: row["cs"], ds: row["ds"])
}
}
// Record.fetchAll
do {
let records = try Record.fetchAll(db, request)
XCTAssertEqual(records, [
Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
]),
Record(
a: A(row: ["cola1": 2, "cola2": "a2"]),
cs: [
C(row: ["colc1": 8, "colc2": 2]),
C(row: ["colc1": 9, "colc2": 2]),
],
ds: [
D(row: ["cold1": 11, "cold2": 8, "cold3": "d2"]),
D(row: ["cold1": 12, "cold2": 8, "cold3": "d3"]),
D(row: ["cold1": 13, "cold2": 9, "cold3": "d4"]),
]),
Record(
a: A(row: ["cola1": 3, "cola2": "a3"]),
cs: [],
ds: []),
])
}
// Record.fetchOne
do {
let record = try Record.fetchOne(db, request)!
XCTAssertEqual(record, Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
]))
}
}
// Request with filters
do {
let cs1 = A.hasMany(C.self).forKey("cs1")
let cs2 = A.hasMany(C.self).forKey("cs2")
let request = A
.filter(Column("cola1") != 3)
.including(all: cs1
.filter(Column("colc1") != 8)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs1, using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: cs2
.filter(Column("colc1") != 9)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs2, using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds2"))
.orderByPrimaryKey()
struct Record: FetchableRecord, Equatable {
var a: A
var cs1: [C]
var cs2: [C]
var ds1: [D]
var ds2: [D]
init(a: A, cs1: [C], cs2: [C], ds1: [D], ds2: [D]) {
self.a = a
self.cs1 = cs1
self.cs2 = cs2
self.ds1 = ds1
self.ds2 = ds2
}
init(row: Row) {
self.init(a: A(row: row), cs1: row["cs1"], cs2: row["cs2"], ds1: row["ds1"], ds2: row["ds2"])
}
}
// Record.fetchAll
do {
let records = try Record.fetchAll(db, request)
XCTAssertEqual(records, [
Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs1: [
C(row: ["colc1": 7, "colc2": 1]),
],
cs2: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds1: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
],
ds2: []),
Record(
a: A(row: ["cola1": 2, "cola2": "a2"]),
cs1: [
C(row: ["colc1": 9, "colc2": 2]),
],
cs2: [
C(row: ["colc1": 8, "colc2": 2]),
],
ds1: [
D(row: ["cold1": 12, "cold2": 8, "cold3": "d3"]),
D(row: ["cold1": 13, "cold2": 9, "cold3": "d4"]),
],
ds2: [
D(row: ["cold1": 11, "cold2": 8, "cold3": "d2"]),
]),
])
}
// Record.fetchOne
do {
let record = try Record.fetchOne(db, request)!
XCTAssertEqual(record, Record(
a: A(row: ["cola1": 1, "cola2": "a1"]),
cs1: [
C(row: ["colc1": 7, "colc2": 1]),
],
cs2: [
C(row: ["colc1": 7, "colc2": 1]),
],
ds1: [
D(row: ["cold1": 10, "cold2": 7, "cold3": "d1"]),
],
ds2: []))
}
}
}
}
// TODO: make a variant with joining(optional:)
func testIncludingOptionalBelongsToIncludingAllHasMany() throws {
let dbQueue = try makeDatabaseQueue()
@@ -220,14 +220,14 @@ class AssociationPrefetchingObservationTests: GRDBTestCase {
let request = A
.filter(Column("cola1") != 3)
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8).forKey("cs1"), using: C.hasMany(D.self))
.forKey("ds1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.forKey("ds2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.forKey("ds3"))
@@ -236,44 +236,6 @@ class AssociationPrefetchingObservationTests: GRDBTestCase {
}
}
func testIncludingAllHasManyThroughIsNotMergedWithHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
// Plain request
do {
let cs = A.hasMany(C.self)
let request = A
.including(all: cs)
.including(all: A
.hasMany(D.self, through: cs, using: C.hasMany(D.self)))
try XCTAssertEqual(request.databaseRegion(db).description, "a(cola1,cola2),c(colc1,colc2),d(cold1,cold2,cold3)")
}
// Request with filters
do {
let cs1 = A.hasMany(C.self).forKey("cs1")
let cs2 = A.hasMany(C.self).forKey("cs2")
let request = A
.filter(Column("cola1") != 3)
.including(all: cs1
.filter(Column("colc1") != 8))
.including(all: A
.hasMany(D.self, through: cs1, using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.forKey("ds1"))
.including(all: cs2
.filter(Column("colc1") != 9))
.including(all: A
.hasMany(D.self, through: cs2, using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.forKey("ds2"))
try XCTAssertEqual(request.databaseRegion(db).description, "a(cola1,cola2),c(colc1,colc2),d(cold1,cold2,cold3)")
}
}
}
// TODO: make a variant with joining(optional:)
func testIncludingOptionalBelongsToIncludingAllHasMany() throws {
let dbQueue = try makeDatabaseQueue()
@@ -656,16 +656,16 @@ class AssociationPrefetchingRowTests: GRDBTestCase {
let request = A
.filter(Column("cola1") != 3)
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8).forKey("cs1"), using: C.hasMany(D.self))
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds3"))
@@ -779,146 +779,6 @@ class AssociationPrefetchingRowTests: GRDBTestCase {
}
}
func testIncludingAllHasManyThroughIsNotMergedWithHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
// Plain request
do {
let cs = A.hasMany(C.self)
let request = A
.including(all: cs.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs, using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
// Row.fetchAll
do {
let rows = try Row.fetchAll(db, request)
XCTAssertEqual(rows.count, 3)
XCTAssertEqual(rows[0].description, "[cola1:1 cola2:\"a1\"]")
XCTAssertEqual(rows[0].debugDescription, """
▿ [cola1:1 cola2:"a1"]
+ cs: 1 row
+ ds: 1 row
""")
XCTAssertEqual(rows[0].unscoped, ["cola1": 1, "cola2": "a1"])
XCTAssertEqual(rows[0].prefetchedRows.keys, ["cs", "ds"])
XCTAssertEqual(rows[0].prefetchedRows["cs"]!.count, 1)
XCTAssertEqual(rows[0].prefetchedRows["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(rows[0].prefetchedRows["ds"]!.count, 1)
XCTAssertEqual(rows[0].prefetchedRows["ds"]![0], ["cold1": 10, "cold2": 7, "cold3": "d1", "grdb_colc2": 1])
XCTAssertEqual(rows[1].unscoped, ["cola1": 2, "cola2": "a2"])
XCTAssertEqual(rows[1].prefetchedRows.keys, ["cs", "ds"])
XCTAssertEqual(rows[1].prefetchedRows["cs"]!.count, 2)
XCTAssertEqual(rows[1].prefetchedRows["cs"]![0], ["colc1": 8, "colc2": 2, "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["cs"]![1], ["colc1": 9, "colc2": 2, "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds"]!.count, 3)
XCTAssertEqual(rows[1].prefetchedRows["ds"]![0], ["cold1": 11, "cold2": 8, "cold3": "d2", "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds"]![1], ["cold1": 12, "cold2": 8, "cold3": "d3", "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds"]![2], ["cold1": 13, "cold2": 9, "cold3": "d4", "grdb_colc2": 2])
XCTAssertEqual(rows[2].unscoped, ["cola1": 3, "cola2": "a3"])
XCTAssertEqual(rows[2].prefetchedRows.keys, ["cs", "ds"])
XCTAssertEqual(rows[2].prefetchedRows["cs"]!.count, 0)
XCTAssertEqual(rows[2].prefetchedRows["ds"]!.count, 0)
}
// Row.fetchOne
do {
let row = try Row.fetchOne(db, request)!
XCTAssertEqual(row.unscoped, ["cola1": 1, "cola2": "a1"])
XCTAssertEqual(row.prefetchedRows.keys, ["cs", "ds"])
XCTAssertEqual(row.prefetchedRows["cs"]!.count, 1)
XCTAssertEqual(row.prefetchedRows["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(row.prefetchedRows["ds"]!.count, 1)
XCTAssertEqual(row.prefetchedRows["ds"]![0], ["cold1": 10, "cold2": 7, "cold3": "d1", "grdb_colc2": 1])
}
}
// Request with filters
do {
let cs1 = A.hasMany(C.self).forKey("cs1")
let cs2 = A.hasMany(C.self).forKey("cs2")
let request = A
.filter(Column("cola1") != 3)
.including(all: cs1
.filter(Column("colc1") != 8)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs1, using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: cs2
.filter(Column("colc1") != 9)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs2, using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds2"))
.orderByPrimaryKey()
// Row.fetchAll
do {
let rows = try Row.fetchAll(db, request)
XCTAssertEqual(rows.count, 2)
XCTAssertEqual(rows[0].description, "[cola1:1 cola2:\"a1\"]")
XCTAssertEqual(rows[0].debugDescription, """
▿ [cola1:1 cola2:"a1"]
+ cs1: 1 row
+ cs2: 1 row
+ ds1: 1 row
+ ds2: 0 row
""")
XCTAssertEqual(rows[0].unscoped, ["cola1": 1, "cola2": "a1"])
XCTAssertEqual(rows[0].prefetchedRows.keys, ["cs1", "cs2", "ds1", "ds2"])
XCTAssertEqual(rows[0].prefetchedRows["cs1"]!.count, 1)
XCTAssertEqual(rows[0].prefetchedRows["cs1"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(rows[0].prefetchedRows["cs2"]!.count, 1)
XCTAssertEqual(rows[0].prefetchedRows["cs2"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(rows[0].prefetchedRows["ds1"]!.count, 1)
XCTAssertEqual(rows[0].prefetchedRows["ds1"]![0], ["cold1": 10, "cold2": 7, "cold3": "d1", "grdb_colc2": 1])
XCTAssertEqual(rows[0].prefetchedRows["ds2"]!.count, 0)
XCTAssertEqual(rows[1].unscoped, ["cola1": 2, "cola2": "a2"])
XCTAssertEqual(rows[1].prefetchedRows.keys, ["cs1", "cs2", "ds1", "ds2"])
XCTAssertEqual(rows[1].prefetchedRows["cs1"]!.count, 1)
XCTAssertEqual(rows[1].prefetchedRows["cs1"]![0], ["colc1": 9, "colc2": 2, "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["cs2"]!.count, 1)
XCTAssertEqual(rows[1].prefetchedRows["cs2"]![0], ["colc1": 8, "colc2": 2, "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds1"]!.count, 2)
XCTAssertEqual(rows[1].prefetchedRows["ds1"]![0], ["cold1": 12, "cold2": 8, "cold3": "d3", "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds1"]![1], ["cold1": 13, "cold2": 9, "cold3": "d4", "grdb_colc2": 2])
XCTAssertEqual(rows[1].prefetchedRows["ds2"]!.count, 1)
XCTAssertEqual(rows[1].prefetchedRows["ds2"]![0], ["cold1": 11, "cold2": 8, "cold3": "d2", "grdb_colc2": 2])
}
// Row.fetchOne
do {
let row = try Row.fetchOne(db, request)!
XCTAssertEqual(row.unscoped, ["cola1": 1, "cola2": "a1"])
XCTAssertEqual(row.prefetchedRows.keys, ["cs1", "cs2", "ds1", "ds2"])
XCTAssertEqual(row.prefetchedRows["cs1"]!.count, 1)
XCTAssertEqual(row.prefetchedRows["cs1"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(row.prefetchedRows["cs2"]!.count, 1)
XCTAssertEqual(row.prefetchedRows["cs2"]![0], ["colc1": 7, "colc2": 1, "grdb_colc2": 1])
XCTAssertEqual(row.prefetchedRows["ds1"]!.count, 1)
XCTAssertEqual(row.prefetchedRows["ds1"]![0], ["cold1": 10, "cold2": 7, "cold3": "d1", "grdb_colc2": 1])
XCTAssertEqual(row.prefetchedRows["ds2"]!.count, 0)
}
}
}
}
// TODO: make a variant with joining(optional:)
func testIncludingOptionalBelongsToIncludingAllHasMany() throws {
let dbQueue = try makeDatabaseQueue()
@@ -465,16 +465,16 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
let request = A
.filter(Column("cola1") != 3)
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).filter(Column("colc1") == 8).forKey("cs1"), using: C.hasMany(D.self))
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.hasMany(D.self, through: A.hasMany(C.self).forKey("cs2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds3"))
@@ -514,104 +514,6 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
}
}
func testIncludingAllHasManyThroughIsNotMergedWithHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
// Plain request
do {
let cs = A.hasMany(C.self)
let request = A
.including(all: cs.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs, using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * FROM "a" ORDER BY "cola1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE ("colc2" IN (1, 2, 3)) \
ORDER BY "colc1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3))) \
ORDER BY "d"."cold1"
"""])
}
// Request with filters
do {
let cs1 = A.hasMany(C.self).forKey("cs1")
let cs2 = A.hasMany(C.self).forKey("cs2")
let request = A
.filter(Column("cola1") != 3)
.including(all: cs1
.filter(Column("colc1") != 8)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs1, using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("ds1"))
.including(all: cs2
.filter(Column("colc1") != 9)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: cs2, using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("ds2"))
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * \
FROM "a" \
WHERE ("cola1" <> 3) \
ORDER BY "cola1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" <> 8) AND ("colc2" IN (1, 2))) \
ORDER BY "colc1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2))) \
WHERE ("d"."cold1" <> 11) ORDER BY "d"."cold1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" <> 9) AND ("colc2" IN (1, 2))) \
ORDER BY "colc1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2))) \
WHERE ("d"."cold1" = 11) ORDER BY "d"."cold1"
"""])
}
}
}
// TODO: make a variant with joining(optional:)
func testIncludingOptionalBelongsToIncludingAllHasMany() throws {
let dbQueue = try makeDatabaseQueue()