diff --git a/GRDB/QueryInterface/Request/Association/BelongsToAssociation.swift b/GRDB/QueryInterface/Request/Association/BelongsToAssociation.swift index 0058d3476..a5833c7c0 100644 --- a/GRDB/QueryInterface/Request/Association/BelongsToAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/BelongsToAssociation.swift @@ -58,7 +58,7 @@ /// } /// /// See ForeignKey for more information. -public struct BelongsToAssociation: AssociationToOne { +public struct BelongsToAssociation: AssociationToOne { /// :nodoc: public typealias OriginRowDecoder = Origin @@ -74,12 +74,16 @@ public struct BelongsToAssociation: AssociationToMany { +public struct HasManyAssociation: AssociationToMany { /// :nodoc: public typealias OriginRowDecoder = Origin @@ -74,12 +74,16 @@ public struct HasManyAssociation: } 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: 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) } } diff --git a/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift b/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift index d62fb7985..e2d3eeb91 100644 --- a/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift @@ -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: AssociationToMany { +public struct HasManyThroughAssociation: AssociationToMany { /// :nodoc: public typealias OriginRowDecoder = Origin diff --git a/GRDB/QueryInterface/Request/Association/HasOneAssociation.swift b/GRDB/QueryInterface/Request/Association/HasOneAssociation.swift index 02dcdb61c..94f827e05 100644 --- a/GRDB/QueryInterface/Request/Association/HasOneAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/HasOneAssociation.swift @@ -60,7 +60,7 @@ /// } /// /// See ForeignKey for more information. -public struct HasOneAssociation: AssociationToOne { +public struct HasOneAssociation: AssociationToOne { /// :nodoc: public typealias OriginRowDecoder = Origin @@ -76,12 +76,16 @@ public struct HasOneAssociation: } 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: 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) } } diff --git a/GRDB/QueryInterface/Request/Association/HasOneThroughAssociation.swift b/GRDB/QueryInterface/Request/Association/HasOneThroughAssociation.swift index bd8affe01..7d7027496 100644 --- a/GRDB/QueryInterface/Request/Association/HasOneThroughAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/HasOneThroughAssociation.swift @@ -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: AssociationToOne { +public struct HasOneThroughAssociation: AssociationToOne { /// :nodoc: public typealias OriginRowDecoder = Origin diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index f3cded51e..68220ec58 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -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. diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index f7dd793b0..2dd137df2 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -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)) diff --git a/GRDB/QueryInterface/TableRecord+Association.swift b/GRDB/QueryInterface/TableRecord+Association.swift index 8c64ba39f..a4df01e9f 100644 --- a/GRDB/QueryInterface/TableRecord+Association.swift +++ b/GRDB/QueryInterface/TableRecord+Association.swift @@ -60,8 +60,13 @@ extension TableRecord { key: String? = nil, using foreignKey: ForeignKey? = nil) -> BelongsToAssociation + 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 + 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 + 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 diff --git a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift index c19907517..0fc790ebc 100644 --- a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift @@ -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.