Decouple associations from TableRecord

Associations is generic for type safety, not for defining the association (just as QueryInterfaceRequest)
This commit is contained in:
Gwendal Roué committed 2021-01-23 09:48:18 +01:00
1 parent bc5ba1fda5
commit 8bf9e5e30f
9 files changed
+62 -26

No files matched your search

@@ -58,7 +58,7 @@
/// }
///
/// See ForeignKey for more information.
public struct BelongsToAssociation<Origin: TableRecord, Destination: TableRecord>: AssociationToOne {
public struct BelongsToAssociation<Origin, Destination>: AssociationToOne {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -74,12 +74,16 @@ public struct BelongsToAssociation<Origin: TableRecord, Destination: TableRecord
}
init(
from originTable: String,
to destinationRelation: SQLRelation,
key: String?,
using foreignKey: ForeignKey?)
{
let destinationTable = destinationRelation.source.tableName
let foreignKeyRequest = SQLForeignKeyRequest(
originTable: Origin.databaseTableName,
destinationTable: Destination.databaseTableName,
originTable: originTable,
destinationTable: destinationTable,
foreignKey: foreignKey)
let condition = SQLAssociationCondition.foreignKey(
@@ -90,13 +94,13 @@ public struct BelongsToAssociation<Origin: TableRecord, Destination: TableRecord
if let key = key {
associationKey = .fixedSingular(key)
} else {
associationKey = .inflected(Destination.databaseTableName)
associationKey = .inflected(destinationTable)
}
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
relation: destinationRelation,
cardinality: .toOne)
}
}
@@ -58,7 +58,7 @@
/// }
///
/// See ForeignKey for more information.
public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>: AssociationToMany {
public struct HasManyAssociation<Origin, Destination>: AssociationToMany {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -74,12 +74,16 @@ public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>:
}
init(
from originTable: String,
to destinationRelation: SQLRelation,
key: String?,
using foreignKey: ForeignKey?)
{
let destinationTable = destinationRelation.source.tableName
let foreignKeyRequest = SQLForeignKeyRequest(
originTable: Destination.databaseTableName,
destinationTable: Origin.databaseTableName,
originTable: destinationTable,
destinationTable: originTable,
foreignKey: foreignKey)
let condition = SQLAssociationCondition.foreignKey(
@@ -90,13 +94,13 @@ public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>:
if let key = key {
associationKey = .fixedPlural(key)
} else {
associationKey = .inflected(Destination.databaseTableName)
associationKey = .inflected(destinationTable)
}
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
relation: destinationRelation,
cardinality: .toMany)
}
}
@@ -41,7 +41,7 @@
/// two other associations: the `through:` and `using:` arguments. Those
/// associations can be any other association (BelongsTo, HasMany,
/// HasManyThrough, etc).
public struct HasManyThroughAssociation<Origin: TableRecord, Destination: TableRecord>: AssociationToMany {
public struct HasManyThroughAssociation<Origin, Destination>: AssociationToMany {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -60,7 +60,7 @@
/// }
///
/// See ForeignKey for more information.
public struct HasOneAssociation<Origin: TableRecord, Destination: TableRecord>: AssociationToOne {
public struct HasOneAssociation<Origin, Destination>: AssociationToOne {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -76,12 +76,16 @@ public struct HasOneAssociation<Origin: TableRecord, Destination: TableRecord>:
}
init(
from originTable: String,
to destinationRelation: SQLRelation,
key: String?,
using foreignKey: ForeignKey?)
{
let destinationTable = destinationRelation.source.tableName
let foreignKeyRequest = SQLForeignKeyRequest(
originTable: Destination.databaseTableName,
destinationTable: Origin.databaseTableName,
originTable: destinationTable,
destinationTable: originTable,
foreignKey: foreignKey)
let condition = SQLAssociationCondition.foreignKey(
@@ -92,13 +96,13 @@ public struct HasOneAssociation<Origin: TableRecord, Destination: TableRecord>:
if let key = key {
associationKey = .fixedSingular(key)
} else {
associationKey = .inflected(Destination.databaseTableName)
associationKey = .inflected(destinationTable)
}
_sqlAssociation = _SQLAssociation(
key: associationKey,
condition: condition,
relation: Destination.relationForAll,
relation: destinationRelation,
cardinality: .toOne)
}
}
@@ -21,7 +21,7 @@
/// two other associations: the `through:` and `using:` arguments. Those
/// associations can be any other association to one (BelongsTo, HasOne,
/// HasOneThrough).
public struct HasOneThroughAssociation<Origin: TableRecord, Destination: TableRecord>: AssociationToOne {
public struct HasOneThroughAssociation<Origin, Destination>: AssociationToOne {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -113,9 +113,7 @@ extension CommonTableExpression {
extension CommonTableExpression {
var relationForAll: SQLRelation {
SQLRelation(
source: SQLSource(tableName: tableName, alias: nil),
selectionPromise: DatabasePromise(value: [AllColumns(cte: cte)]))
.all(fromTable: tableName, selection: { _ in [AllColumns(cte: cte)] })
}
/// Creates a request for all rows of the common table expression.
+13
View File
@@ -186,6 +186,19 @@ struct SQLRelation {
}
}
extension SQLRelation {
/// Convenience factory methods which selects all rows from a table.
static func all(
fromTable tableName: String,
selection: @escaping (Database) -> [SQLSelectable] = { _ in [AllColumns()] })
-> Self
{
SQLRelation(
source: SQLSource(tableName: tableName, alias: nil),
selectionPromise: DatabasePromise(selection))
}
}
extension SQLRelation: Refinable {
func select(_ selection: @escaping (Database) throws -> [SQLSelectable]) -> Self {
with(\.selectionPromise, DatabasePromise(selection))
@@ -60,8 +60,13 @@ extension TableRecord {
key: String? = nil,
using foreignKey: ForeignKey? = nil)
-> BelongsToAssociation<Self, Destination>
where Destination: TableRecord
{
BelongsToAssociation(key: key, using: foreignKey)
BelongsToAssociation(
from: databaseTableName,
to: Destination.relationForAll,
key: key,
using: foreignKey)
}
/// Creates a "Has many" association between Self and the destination type,
@@ -125,8 +130,13 @@ extension TableRecord {
key: String? = nil,
using foreignKey: ForeignKey? = nil)
-> HasManyAssociation<Self, Destination>
where Destination: TableRecord
{
HasManyAssociation(key: key, using: foreignKey)
HasManyAssociation(
from: databaseTableName,
to: Destination.relationForAll,
key: key,
using: foreignKey)
}
/// Creates a "Has Many Through" association between Self and the
@@ -272,8 +282,13 @@ extension TableRecord {
key: String? = nil,
using foreignKey: ForeignKey? = nil)
-> HasOneAssociation<Self, Destination>
where Destination: TableRecord
{
HasOneAssociation(key: key, using: foreignKey)
HasOneAssociation(
from: databaseTableName,
to: Destination.relationForAll,
key: key,
using: foreignKey)
}
/// Creates a "Has One Through" association between Self and the
@@ -3,9 +3,7 @@ extension TableRecord {
// MARK: Request Derivation
static var relationForAll: SQLRelation {
SQLRelation(
source: SQLSource(tableName: databaseTableName, alias: nil),
selectionPromise: DatabasePromise(value: databaseSelection))
.all(fromTable: databaseTableName, selection: { _ in databaseSelection })
}
/// Creates a request which fetches all records.