Association: remove TableRecord requirement

This commit is contained in:
Gwendal Roué committed 2020-11-30 21:50:18 +01:00
1 parent c36fc2813b
commit c776ee8d47
5 files changed
+31 -4

No files matched your search

@@ -25,7 +25,7 @@ public protocol Association: _Association, DerivableRequest {
/// // BelongsToAssociation<Book, Author>
/// 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
@@ -259,7 +259,7 @@ extension QueryInterfaceRequest: _JoinableRequest {
}
}
extension QueryInterfaceRequest: JoinableRequest where RowDecoder: TableRecord { }
extension QueryInterfaceRequest: JoinableRequest { }
extension QueryInterfaceRequest: TableRequest {
/// :nodoc:
@@ -549,7 +549,7 @@ public protocol JoinableRequest: _JoinableRequest {
/// // BelongsToAssociation<Book, Author>
/// 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,
@@ -174,6 +174,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase {
where
BAssociation: Association,
CAssociation: Association,
BAssociation.OriginRowDecoder: TableRecord,
BAssociation.OriginRowDecoder == CAssociation.OriginRowDecoder
{
let A = BAssociation.OriginRowDecoder.self
@@ -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