mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Tests for Table Associations
This commit is contained in:
1 parent
9def48f4f6
commit
32bbee9c8a
3 files changed
+533
-55
No files matched your search
@@ -917,7 +917,7 @@ extension Table {
|
||||
_ destination: Destination.Type,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasOneAssociation<Self, Destination>
|
||||
-> HasOneAssociation<RowDecoder, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
HasOneAssociation(
|
||||
@@ -1071,7 +1071,7 @@ extension Table {
|
||||
-> HasManyThroughAssociation<RowDecoder, Target.RowDecoder>
|
||||
where Pivot: Association,
|
||||
Target: Association,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.OriginRowDecoder == RowDecoder,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
let association = HasManyThroughAssociation<RowDecoder, Target.RowDecoder>(
|
||||
@@ -1104,7 +1104,7 @@ extension Table {
|
||||
-> HasOneThroughAssociation<RowDecoder, Target.RowDecoder>
|
||||
where Pivot: AssociationToOne,
|
||||
Target: AssociationToOne,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.OriginRowDecoder == RowDecoder,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
let association = HasOneThroughAssociation<RowDecoder, Target.RowDecoder>(
|
||||
@@ -1117,3 +1117,95 @@ extension Table {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Joining Methods
|
||||
|
||||
extension Table {
|
||||
/// Creates a request that prefetches an association.
|
||||
public func including<A: AssociationToMany>(all association: A)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
where A.OriginRowDecoder == RowDecoder
|
||||
{
|
||||
all().including(all: association)
|
||||
}
|
||||
|
||||
/// Creates a request that includes an association. The columns of the
|
||||
/// associated record are selected. The returned association does not
|
||||
/// require that the associated database table contains a matching row.
|
||||
public func including<A: Association>(optional association: A)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
where A.OriginRowDecoder == RowDecoder
|
||||
{
|
||||
all().including(optional: association)
|
||||
}
|
||||
|
||||
/// Creates a request that includes an association. The columns of the
|
||||
/// associated record are selected. The returned association requires
|
||||
/// that the associated database table contains a matching row.
|
||||
public func including<A: Association>(required association: A)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
where A.OriginRowDecoder == RowDecoder
|
||||
{
|
||||
all().including(required: association)
|
||||
}
|
||||
|
||||
/// Creates a request that includes an association. The columns of the
|
||||
/// associated record are not selected. The returned association does not
|
||||
/// require that the associated database table contains a matching row.
|
||||
public func joining<A: Association>(optional association: A)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
where A.OriginRowDecoder == RowDecoder
|
||||
{
|
||||
all().joining(optional: association)
|
||||
}
|
||||
|
||||
/// Creates a request that includes an association. The columns of the
|
||||
/// associated record are not selected. The returned association requires
|
||||
/// that the associated database table contains a matching row.
|
||||
public func joining<A: Association>(required association: A)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
where A.OriginRowDecoder == RowDecoder
|
||||
{
|
||||
all().joining(required: association)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Association Aggregates
|
||||
|
||||
extension Table {
|
||||
/// Creates a request with *aggregates* appended to the selection.
|
||||
///
|
||||
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
|
||||
/// // FROM player LEFT JOIN book ...
|
||||
/// let table = Table<Player>("player")
|
||||
/// let request = table.annotated(with: Player.books.count)
|
||||
public func annotated(with aggregates: AssociationAggregate<RowDecoder>...) -> QueryInterfaceRequest<RowDecoder> {
|
||||
all().annotated(with: aggregates)
|
||||
}
|
||||
|
||||
/// Creates a request with *aggregates* appended to the selection.
|
||||
///
|
||||
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
|
||||
/// // FROM player LEFT JOIN book ...
|
||||
/// let table = Table<Player>("player")
|
||||
/// let request = table.annotated(with: [Player.books.count])
|
||||
public func annotated(with aggregates: [AssociationAggregate<RowDecoder>]) -> QueryInterfaceRequest<RowDecoder> {
|
||||
all().annotated(with: aggregates)
|
||||
}
|
||||
|
||||
/// Creates a request with the provided aggregate *predicate*.
|
||||
///
|
||||
/// // SELECT player.*
|
||||
/// // FROM player LEFT JOIN book ...
|
||||
/// // HAVING COUNT(DISTINCT book.id) = 0
|
||||
/// let table = Table<Player>("player")
|
||||
/// var request = table.all()
|
||||
/// request = request.having(Player.books.isEmpty)
|
||||
///
|
||||
/// The selection defaults to all columns. This default can be changed for
|
||||
/// all requests by the `TableRecord.databaseSelection` property, or
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public func having(_ predicate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
all().having(predicate)
|
||||
}
|
||||
}
|
||||
@@ -629,7 +629,7 @@ extension TableRecord {
|
||||
///
|
||||
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
|
||||
/// // FROM player LEFT JOIN book ...
|
||||
/// var request = Player.annotated(with: Player.books.count)
|
||||
/// let request = Player.annotated(with: Player.books.count)
|
||||
public static func annotated(with aggregates: AssociationAggregate<Self>...) -> QueryInterfaceRequest<Self> {
|
||||
all().annotated(with: aggregates)
|
||||
}
|
||||
@@ -638,7 +638,7 @@ extension TableRecord {
|
||||
///
|
||||
/// // SELECT player.*, COUNT(DISTINCT book.id) AS bookCount
|
||||
/// // FROM player LEFT JOIN book ...
|
||||
/// var request = Player.annotated(with: [Player.books.count])
|
||||
/// let request = Player.annotated(with: [Player.books.count])
|
||||
public static func annotated(with aggregates: [AssociationAggregate<Self>]) -> QueryInterfaceRequest<Self> {
|
||||
all().annotated(with: aggregates)
|
||||
}
|
||||
|
||||
@@ -33,23 +33,23 @@ class TableTests: GRDBTestCase {
|
||||
do {
|
||||
let t = Table("player")
|
||||
try assertEqualSQL(db, t.all(), """
|
||||
SELECT * FROM "player"
|
||||
""")
|
||||
SELECT * FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.none(), """
|
||||
SELECT * FROM "player" WHERE 0
|
||||
""")
|
||||
SELECT * FROM "player" WHERE 0
|
||||
""")
|
||||
try assertEqualSQL(db, t.select(Column("id"), Column("name")), """
|
||||
SELECT "id", "name" FROM "player"
|
||||
""")
|
||||
SELECT "id", "name" FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.select([Column("id"), Column("name")]), """
|
||||
SELECT "id", "name" FROM "player"
|
||||
""")
|
||||
SELECT "id", "name" FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.select(sql: "id, ?", arguments: ["O'Brien"]), """
|
||||
SELECT id, 'O''Brien' FROM "player"
|
||||
""")
|
||||
SELECT id, 'O''Brien' FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.select(literal: "id, \("O'Brien")"), """
|
||||
SELECT id, 'O''Brien' FROM "player"
|
||||
""")
|
||||
SELECT id, 'O''Brien' FROM "player"
|
||||
""")
|
||||
try XCTAssertEqual(t.select(Column("id"), as: Int64.self).fetchOne(db), 1)
|
||||
try XCTAssertEqual(t.select(Column("id")).fetchOne(db), 1)
|
||||
try XCTAssertEqual(t.select([Column("name")], as: String.self).fetchOne(db), "Alice")
|
||||
@@ -59,59 +59,59 @@ class TableTests: GRDBTestCase {
|
||||
try XCTAssertEqual(t.select(literal: "name", as: String.self).fetchOne(db), "Alice")
|
||||
try XCTAssertEqual(t.select(literal: "name").fetchOne(db), "Alice")
|
||||
try assertEqualSQL(db, t.annotated(with: Column.rowID), """
|
||||
SELECT *, "rowid" FROM "player"
|
||||
""")
|
||||
SELECT *, "rowid" FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.annotated(with: [Column.rowID]), """
|
||||
SELECT *, "rowid" FROM "player"
|
||||
""")
|
||||
SELECT *, "rowid" FROM "player"
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(Column("id") > 10), """
|
||||
SELECT * FROM "player" WHERE "id" > 10
|
||||
""")
|
||||
SELECT * FROM "player" WHERE "id" > 10
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(key: 1), """
|
||||
SELECT * FROM "player" WHERE "id" = 1
|
||||
""")
|
||||
SELECT * FROM "player" WHERE "id" = 1
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(keys: [1, 2, 3]), """
|
||||
SELECT * FROM "player" WHERE "id" IN (1, 2, 3)
|
||||
""")
|
||||
SELECT * FROM "player" WHERE "id" IN (1, 2, 3)
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(key: ["id": 1]), """
|
||||
SELECT * FROM "player" WHERE "id" = 1
|
||||
""")
|
||||
SELECT * FROM "player" WHERE "id" = 1
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(keys: [["id": 1], ["id": 2]]), """
|
||||
SELECT * FROM "player" WHERE ("id" = 1) OR ("id" = 2)
|
||||
""")
|
||||
SELECT * FROM "player" WHERE ("id" = 1) OR ("id" = 2)
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(sql: "name = ?", arguments: ["O'Brien"]), """
|
||||
SELECT * FROM "player" WHERE name = 'O''Brien'
|
||||
""")
|
||||
SELECT * FROM "player" WHERE name = 'O''Brien'
|
||||
""")
|
||||
try assertEqualSQL(db, t.filter(literal: "name = \("O'Brien")"), """
|
||||
SELECT * FROM "player" WHERE name = 'O''Brien'
|
||||
""")
|
||||
SELECT * FROM "player" WHERE name = 'O''Brien'
|
||||
""")
|
||||
try assertEqualSQL(db, t.order(Column("id"), Column("name").desc), """
|
||||
SELECT * FROM "player" ORDER BY "id", "name" DESC
|
||||
""")
|
||||
SELECT * FROM "player" ORDER BY "id", "name" DESC
|
||||
""")
|
||||
try assertEqualSQL(db, t.order([Column("id"), Column("name").desc]), """
|
||||
SELECT * FROM "player" ORDER BY "id", "name" DESC
|
||||
""")
|
||||
SELECT * FROM "player" ORDER BY "id", "name" DESC
|
||||
""")
|
||||
try assertEqualSQL(db, t.orderByPrimaryKey(), """
|
||||
SELECT * FROM "player" ORDER BY "id"
|
||||
""")
|
||||
SELECT * FROM "player" ORDER BY "id"
|
||||
""")
|
||||
try assertEqualSQL(db, t.order(sql: "IFNULL(name, ?)", arguments: ["O'Brien"]), """
|
||||
SELECT * FROM "player" ORDER BY IFNULL(name, 'O''Brien')
|
||||
""")
|
||||
SELECT * FROM "player" ORDER BY IFNULL(name, 'O''Brien')
|
||||
""")
|
||||
try assertEqualSQL(db, t.order(literal: "IFNULL(name, \("O'Brien"))"), """
|
||||
SELECT * FROM "player" ORDER BY IFNULL(name, 'O''Brien')
|
||||
""")
|
||||
SELECT * FROM "player" ORDER BY IFNULL(name, 'O''Brien')
|
||||
""")
|
||||
try assertEqualSQL(db, t.limit(1), """
|
||||
SELECT * FROM "player" LIMIT 1
|
||||
""")
|
||||
SELECT * FROM "player" LIMIT 1
|
||||
""")
|
||||
try assertEqualSQL(db, t.limit(1, offset: 3), """
|
||||
SELECT * FROM "player" LIMIT 1 OFFSET 3
|
||||
""")
|
||||
SELECT * FROM "player" LIMIT 1 OFFSET 3
|
||||
""")
|
||||
try assertEqualSQL(db, t.aliased(TableAlias(name: "p")), """
|
||||
SELECT "p".* FROM "player" "p"
|
||||
""")
|
||||
SELECT "p".* FROM "player" "p"
|
||||
""")
|
||||
try assertEqualSQL(db, t.with(CommonTableExpression(named: "cte", literal: "SELECT \("O'Brien")")), """
|
||||
WITH "cte" AS (SELECT 'O''Brien') SELECT * FROM "player"
|
||||
""")
|
||||
WITH "cte" AS (SELECT 'O''Brien') SELECT * FROM "player"
|
||||
""")
|
||||
}
|
||||
|
||||
if #available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *) {
|
||||
@@ -228,7 +228,7 @@ class TableTests: GRDBTestCase {
|
||||
}
|
||||
try db.execute(sql: "INSERT INTO player VALUES ('Alice')")
|
||||
try db.execute(sql: "INSERT INTO player VALUES (NULL)")
|
||||
|
||||
|
||||
let t = Table<Value?>("player")
|
||||
try XCTAssertEqual(t.fetchCursor(db).next(), Value(rawValue: "Alice"))
|
||||
try XCTAssertEqual(t.fetchAll(db), [Value(rawValue: "Alice"), nil])
|
||||
@@ -286,7 +286,7 @@ class TableTests: GRDBTestCase {
|
||||
}
|
||||
try db.execute(sql: "INSERT INTO player VALUES ('Alice')")
|
||||
try db.execute(sql: "INSERT INTO player VALUES (NULL)")
|
||||
|
||||
|
||||
let t = Table<String?>("player")
|
||||
try XCTAssertEqual(t.fetchCursor(db).next(), "Alice")
|
||||
try XCTAssertEqual(t.fetchAll(db), ["Alice", nil])
|
||||
@@ -310,4 +310,390 @@ class TableTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_belongsTo_Table() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
let player = Table("player")
|
||||
let association = player.belongsTo(Table("team"))
|
||||
try assertEqualSQL(db, player.including(optional: association), """
|
||||
SELECT "player".*, "team".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.including(required: association), """
|
||||
SELECT "player".*, "team".* \
|
||||
FROM "player" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.joining(optional: association), """
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.joining(required: association), """
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_belongsTo_TableRecord() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
struct Team: TableRecord { }
|
||||
let player = Table("player")
|
||||
let association = player.belongsTo(Team.self)
|
||||
try assertEqualSQL(db, player.including(optional: association), """
|
||||
SELECT "player".*, "team".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.including(required: association), """
|
||||
SELECT "player".*, "team".* \
|
||||
FROM "player" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.joining(optional: association), """
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, player.joining(required: association), """
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasOne_Table() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
let team = Table("team")
|
||||
let association = team.hasOne(Table("player"))
|
||||
try assertEqualSQL(db, team.including(optional: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.including(required: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(optional: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(required: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasOne_TableRecord() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
struct Player: TableRecord { }
|
||||
let team = Table("team")
|
||||
let association = team.hasOne(Player.self)
|
||||
try assertEqualSQL(db, team.including(optional: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.including(required: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(optional: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(required: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasMany_Table() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
let team = Table("team")
|
||||
let association = team.hasMany(Table("player"))
|
||||
try assertEqualSQL(db, team.including(optional: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.including(required: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(optional: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(required: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
_ = team.including(all: association) // TODO: test
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasMany_TableRecord() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
struct Player: TableRecord { }
|
||||
let team = Table("team")
|
||||
let association = team.hasMany(Player.self)
|
||||
try assertEqualSQL(db, team.including(optional: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.including(required: association), """
|
||||
SELECT "team".*, "player".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(optional: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(required: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id"
|
||||
""")
|
||||
_ = team.including(all: association) // TODO: test
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_to_CommonTableExpression() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
let player = Table("player")
|
||||
let cte = CommonTableExpression(named: "teamBis", request: Table("team").all())
|
||||
let association = player.association(to: cte, on: { $0["teamID"] == $1["id"] })
|
||||
try assertEqualSQL(db, player.with(cte).including(optional: association), """
|
||||
WITH "teamBis" AS (SELECT * FROM "team") \
|
||||
SELECT "player".*, "teamBis".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "teamBis" ON "player"."teamID" = "teamBis"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, player.with(cte).including(required: association), """
|
||||
WITH "teamBis" AS (SELECT * FROM "team") \
|
||||
SELECT "player".*, "teamBis".* \
|
||||
FROM "player" \
|
||||
JOIN "teamBis" ON "player"."teamID" = "teamBis"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, player.with(cte).joining(optional: association), """
|
||||
WITH "teamBis" AS (SELECT * FROM "team") \
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
LEFT JOIN "teamBis" ON "player"."teamID" = "teamBis"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, player.with(cte).joining(required: association), """
|
||||
WITH "teamBis" AS (SELECT * FROM "team") \
|
||||
SELECT "player".* \
|
||||
FROM "player" \
|
||||
JOIN "teamBis" ON "player"."teamID" = "teamBis"."id"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasOneThrough() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
try db.create(table: "award") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("playerID", .integer).references("player")
|
||||
}
|
||||
|
||||
let team = Table("team")
|
||||
let player = Table("player")
|
||||
let award = Table("award")
|
||||
let association = award.hasOne(
|
||||
Row.self,
|
||||
through: award.belongsTo(player),
|
||||
using: player.belongsTo(team))
|
||||
try assertEqualSQL(db, award.including(optional: association), """
|
||||
SELECT "award".*, "team".* \
|
||||
FROM "award" \
|
||||
LEFT JOIN "player" ON "player"."id" = "award"."playerID" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, award.including(required: association), """
|
||||
SELECT "award".*, "team".* \
|
||||
FROM "award" \
|
||||
JOIN "player" ON "player"."id" = "award"."playerID" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, award.joining(optional: association), """
|
||||
SELECT "award".* \
|
||||
FROM "award" \
|
||||
LEFT JOIN "player" ON "player"."id" = "award"."playerID" \
|
||||
LEFT JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
try assertEqualSQL(db, award.joining(required: association), """
|
||||
SELECT "award".* \
|
||||
FROM "award" \
|
||||
JOIN "player" ON "player"."id" = "award"."playerID" \
|
||||
JOIN "team" ON "team"."id" = "player"."teamID"
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_hasManyThrough() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
try db.create(table: "award") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("playerID", .integer).references("player")
|
||||
}
|
||||
|
||||
let team = Table("team")
|
||||
let player = Table("player")
|
||||
let award = Table("award")
|
||||
let association = team.hasMany(
|
||||
Row.self,
|
||||
through: team.hasMany(player),
|
||||
using: player.hasMany(award))
|
||||
try assertEqualSQL(db, team.including(optional: association), """
|
||||
SELECT "team".*, "award".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
LEFT JOIN "award" ON "award"."playerID" = "player"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.including(required: association), """
|
||||
SELECT "team".*, "award".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
JOIN "award" ON "award"."playerID" = "player"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(optional: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
LEFT JOIN "award" ON "award"."playerID" = "player"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.joining(required: association), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
JOIN "award" ON "award"."playerID" = "player"."id"
|
||||
""")
|
||||
_ = team.including(all: association) // TODO: test
|
||||
}
|
||||
}
|
||||
|
||||
func test_association_aggregates() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "team") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("teamID", .integer).references("team")
|
||||
}
|
||||
|
||||
let team = Table("team")
|
||||
let association = team.hasMany(Table("player"))
|
||||
try assertEqualSQL(db, team.annotated(with: association.count, association.max(Column("id"))), """
|
||||
SELECT "team".*, COUNT(DISTINCT "player"."id") AS "playerCount", MAX("player"."id") AS "maxPlayerId" \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
GROUP BY "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.annotated(with: [association.count, association.max(Column("id"))]), """
|
||||
SELECT "team".*, COUNT(DISTINCT "player"."id") AS "playerCount", MAX("player"."id") AS "maxPlayerId" \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
GROUP BY "team"."id"
|
||||
""")
|
||||
try assertEqualSQL(db, team.having(association.isEmpty), """
|
||||
SELECT "team".* \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON "player"."teamID" = "team"."id" \
|
||||
GROUP BY "team"."id" \
|
||||
HAVING COUNT(DISTINCT "player"."id") = 0
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user