From c776ee8d479a5b6fa7ea08be6fe760e10bde0cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 30 Nov 2020 20:07:08 +0100 Subject: [PATCH] Association: remove TableRecord requirement --- .../Request/Association/Association.swift | 27 +++++++++++++++++-- .../Request/QueryInterfaceRequest.swift | 2 +- .../Request/RequestProtocols.swift | 3 ++- .../AssociationHasManyThroughSQLTests.swift | 1 + .../AssociationHasOneThroughSQLTests.swift | 2 ++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/GRDB/QueryInterface/Request/Association/Association.swift b/GRDB/QueryInterface/Request/Association/Association.swift index f500389b1..f584c790d 100644 --- a/GRDB/QueryInterface/Request/Association/Association.swift +++ b/GRDB/QueryInterface/Request/Association/Association.swift @@ -25,7 +25,7 @@ public protocol Association: _Association, DerivableRequest { /// // BelongsToAssociation /// static let author = belongsTo(Author.self) /// } - associatedtype OriginRowDecoder: TableRecord + associatedtype OriginRowDecoder /// Creates an association with the given key. /// @@ -292,7 +292,30 @@ extension Association { // TableRequest extension Association { /// :nodoc: - public var databaseTableName: String { RowDecoder.databaseTableName } + public var databaseTableName: String { + switch _sqlAssociation.destination.relation.source { + case .table(tableName: let tableName, alias: _): + // Use case: + // + // let request = Player.all() + // request.filter(key: ...) + // request.filter(keys: ...) + // request.orderByPrimaryKey() + return tableName + case .subquery: + // The only current use case for SQLSource.query is the + // "trivial count query" (see SQLQuery.countQuery): + // + // // SELECT COUNT(*) FROM (SELECT * FROM player LIMIT 10) + // let request = Player.limit(10) + // let count = try request.fetchCount(db) + // + // This query is currently never wrapped in a QueryInterfaceRequest + // So this fatal error can not currently happen. + fatalError("Request is not based on a database table") + } + } + } // MARK: - AssociationToOne diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index af65a3e4d..a173516de 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -259,7 +259,7 @@ extension QueryInterfaceRequest: _JoinableRequest { } } -extension QueryInterfaceRequest: JoinableRequest where RowDecoder: TableRecord { } +extension QueryInterfaceRequest: JoinableRequest { } extension QueryInterfaceRequest: TableRequest { /// :nodoc: diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index e898a673d..6727618c7 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -549,7 +549,7 @@ public protocol JoinableRequest: _JoinableRequest { /// // BelongsToAssociation /// static let author = belongsTo(Author.self) /// } - associatedtype RowDecoder: TableRecord + associatedtype RowDecoder } extension JoinableRequest { @@ -586,6 +586,7 @@ extension JoinableRequest { _joining(required: association._sqlAssociation) } + #warning("TODO: Do we need to be able to build a real association? With a default key? With a foreign key?") #warning("TODO: doc") public func including( optional cte: CommonTableExpression, diff --git a/Tests/GRDBTests/AssociationHasManyThroughSQLTests.swift b/Tests/GRDBTests/AssociationHasManyThroughSQLTests.swift index 499319438..3fc66af7b 100644 --- a/Tests/GRDBTests/AssociationHasManyThroughSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasManyThroughSQLTests.swift @@ -174,6 +174,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase { where BAssociation: Association, CAssociation: Association, + BAssociation.OriginRowDecoder: TableRecord, BAssociation.OriginRowDecoder == CAssociation.OriginRowDecoder { let A = BAssociation.OriginRowDecoder.self diff --git a/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift b/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift index 9a444d26a..f5d71cc8c 100644 --- a/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift @@ -176,6 +176,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase { where BAssociation: AssociationToOne, CAssociation: AssociationToOne, + BAssociation.OriginRowDecoder: TableRecord, BAssociation.OriginRowDecoder == CAssociation.OriginRowDecoder { let A = BAssociation.OriginRowDecoder.self @@ -784,6 +785,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase { CAssociation: AssociationToOne, DCAssociation: AssociationToOne, DBAssociation: AssociationToOne, + BAssociation.OriginRowDecoder: TableRecord, BAssociation.OriginRowDecoder == CAssociation.OriginRowDecoder, BAssociation.OriginRowDecoder == DCAssociation.OriginRowDecoder, BAssociation.OriginRowDecoder == DBAssociation.OriginRowDecoder