diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 2ca9e5423..a8da3a750 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -656,11 +656,6 @@ private struct SQLQualifiedJoin: Refinable { // 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() @@ -728,7 +723,7 @@ private struct SQLQualifiedJoin: Refinable { guard let primaryKey = try subRelation.primaryKeyExpression(db) else { fatalError("Not implemented: support for WITHOUT ROWID optimization") } - subRelation = subRelation.select([primaryKey]) + subRelation = subRelation.selectOnly([primaryKey]) // Subquery: ... WHERE AND let filters = try condition.expressions(db, leftAlias: leftAlias) diff --git a/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift b/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift index 7d2d39363..d026a748d 100644 --- a/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift @@ -107,23 +107,37 @@ 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) + 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 + // Not implemented: chaining a required association behind an optional association // try assertMatchSQL(db, Parent.all().including(optional: association), """ // SELECT ... // """) + 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) + 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().joining(required: association), """ -// SELECT ... -// """) - // TODO: implement - // Not implemented: associating records to a `first` or `last` to-one association + // Not implemented: chaining a required association behind an optional association // try assertMatchSQL(db, Parent.all().joining(optional: association), """ // SELECT ... // """) @@ -143,16 +157,30 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { .orderByPrimaryKey() .first .including(required: Child.hasOne(Toy.self)) - // 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.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" + """) try assertMatchSQL(db, Parent().request(for: association), """ SELECT "child".*, "toy".* FROM "child" @@ -164,39 +192,71 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { } do { - // 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 ... -// """) + 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) + 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".* + 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)) + """) } do { - // 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 ... -// """) + 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)) + """) } } } @@ -236,23 +296,41 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { .orderByPrimaryKey() .first .joining(required: Child.hasOne(Toy.self).including(optional: Toy.belongsTo(Vendor.self))) + try assertMatchSQL(db, Parent.all().including(required: association), """ + SELECT "parent".*, "child".*, "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 + // Not implemented: chaining a required association behind an optional association // try assertMatchSQL(db, Parent.all().including(optional: association), """ // SELECT ... // """) + try assertMatchSQL(db, Parent.all().joining(required: association), """ + SELECT "parent".*, "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().joining(required: association), """ -// SELECT ... -// """) - // TODO: implement - // Not implemented: associating records to a `first` or `last` to-one association + // Not implemented: chaining a required association behind an optional association // try assertMatchSQL(db, Parent.all().joining(optional: association), """ // SELECT ... // """) @@ -273,16 +351,34 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { .orderByPrimaryKey() .first .including(required: Child.hasOne(Toy.self).including(optional: Toy.belongsTo(Vendor.self))) - // 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.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" + """) try assertMatchSQL(db, Parent().request(for: association), """ SELECT "child".*, "toy".*, "vendor".* FROM "child" diff --git a/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift b/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift index 76a89810a..92d9e95b4 100644 --- a/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift @@ -40,25 +40,41 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "a".*, "c".* FROM "a" JOIN "b" ON "b"."id" = "a"."bId" - JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A.all().including(optional: association), """ SELECT "a".*, "c".* FROM "a" LEFT JOIN "b" ON "b"."id" = "a"."bId" - LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) 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" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) 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" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A().request(for: association), """ SELECT "c".* @@ -104,25 +120,41 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "a".*, "c".* FROM "a" JOIN "b" ON "b"."aId" = "a"."id" - JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A.all().including(optional: association), """ SELECT "a".*, "c".* FROM "a" LEFT JOIN "b" ON "b"."aId" = "a"."id" - LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A.all().joining(required: association), """ SELECT "a".* FROM "a" JOIN "b" ON "b"."aId" = "a"."id" - JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A.all().joining(optional: association), """ SELECT "a".* FROM "a" LEFT JOIN "b" ON "b"."aId" = "a"."id" - LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = ( + SELECT "c"."id" + FROM "c" + WHERE "c"."bId" = "b"."id" + LIMIT 1) """) try assertMatchSQL(db, A().request(for: association), """ SELECT "c".* @@ -163,34 +195,58 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { do { let association = A.c.first - // 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.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) + JOIN "c" ON "c"."id" = "b"."cId" + """) + 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) + LEFT JOIN "c" ON "c"."id" = "b"."cId" + """) try assertMatchSQL(db, A().request(for: association), """ SELECT "c".* FROM "c" - JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" WHERE ("b"."cId" = "c"."id") AND ("b"."aId" = 1) LIMIT 1) + JOIN "b" ON "b"."id" = ( + SELECT "b"."id" + FROM "b" + WHERE ("b"."cId" = "c"."id") AND ("b"."aId" = 1) + LIMIT 1) LIMIT 1 """) } @@ -227,34 +283,58 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { do { let association = A.c.first - // 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.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) + JOIN "c" ON "c"."bId" = "b"."id" + """) + 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) + LEFT JOIN "c" ON "c"."bId" = "b"."id" + """) try assertMatchSQL(db, A().request(for: association), """ SELECT "c".* FROM "c" - JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" WHERE ("b"."id" = "c"."bId") AND ("b"."aId" = 1) LIMIT 1) + JOIN "b" ON "b"."id" = ( + SELECT "b"."id" + FROM "b" + WHERE ("b"."id" = "c"."bId") AND ("b"."aId" = 1) + LIMIT 1) LIMIT 1 """) } @@ -298,21 +378,55 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { } do { - // 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 ... -// """) + let association = A.d + 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" + """) + 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" + """) + 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) + JOIN "d" ON "d"."cId" = "c"."id" + """) + 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) + LEFT JOIN "d" ON "d"."cId" = "c"."id" + """) // TODO: implement // Not implemented: loading multiple records associated to a `first` or `last` to-one association // try assertMatchSQL(db, A().request(for: association), """ @@ -357,12 +471,72 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try dbQueue.inDatabase { db in do { - // 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 "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 "toy" ON "toy"."childId" = "child"."id" + 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) + JOIN "toy" ON "toy"."childId" = "child"."id" + JOIN "pet" ON "pet"."rowid" = ( + SELECT "pet"."rowid" + FROM "pet" + WHERE "pet"."childId" = "child"."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) + JOIN "toy" ON "toy"."childId" = "child"."id" + LIMIT 1 + """) + } + do { + #warning("TODO: generate correct SQL") // let association = Parent.hasMany( // Pet.self, -// through: Parent.children.joining(required: Child.toy), -// using: Child.pets) +// 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 ... @@ -374,35 +548,6 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { // SELECT ... // """) } - 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 - // 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" - JOIN "child" "child1" ON ("child1"."id" = "pet"."childId") AND (1) - JOIN "toy" ON "toy"."childId" = "child1"."id" - JOIN "child" "child2" ON "child2"."id" = ( - SELECT "child"."id" - FROM "child" - WHERE ("child"."id" = "pet"."childId") AND (1 + 1) AND ("child"."parentId" = 1) - LIMIT 1) - LIMIT 1 - """) - } do { let association = Parent.hasMany( Pet.self, @@ -410,13 +555,10 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { using: Child.pets) .joining(required: Pet.child.filter(sql: "1").joining(required: Child.toy)) .first - // TODO: implement - // Not implemented: associating records to a `first` or `last` to-one association + #warning("TODO: generate correct SQL") // 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 ... // """)