diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 2ea26c9d0..0d3781238 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -1,9 +1,60 @@ /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// -/// The protocol for associations implementation details. +/// The base protocol for all associations that define a connection between two +/// record types. +public protocol Association: DerivableRequest { + // OriginRowDecoder and RowDecoder provide type safety: + // + // Book.including(required: Book.author) // compiles + // Fruit.including(required: Book.author) // does not compile + + /// The record type at the origin of the association. + /// + /// In the `belongsTo` association below, it is Book: + /// + /// struct Book: TableRecord { + /// // BelongsToAssociation + /// static let author = belongsTo(Author.self) + /// } + associatedtype OriginRowDecoder + + /// The associated record type. + /// + /// In the `belongsTo` association below, it is Author: + /// + /// struct Book: TableRecord { + /// // BelongsToAssociation + /// static let author = belongsTo(Author.self) + /// } + associatedtype RowDecoder + + // Association is a protocol, not a struct. + // This is because we want associations to be richly typed. + // Yet, we don't want to pollute user code with implementation details of + // associations. So let's hide all this stuff behind the _impl property: + + /// :nodoc: + associatedtype Impl: AssociationImpl + + /// :nodoc: + var _impl: Impl { get } + + /// :nodoc: + init(_impl: Impl) +} + +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// +/// The protocol for implementation details of associations. /// /// :nodoc: -public protocol _Association { +public /* TODO: internal */ protocol AssociationImpl { + /// The association key + var key: String { get } + + /// Creates an association with the given key. + func forKey(_ key: String) -> Self + /// Returns an association whose relation is transformed by the /// given closure. /// @@ -32,38 +83,25 @@ public protocol _Association { var joinCondition: JoinCondition { get } } -extension _Association { +extension AssociationImpl { /// Returns a request joined with self. func joinedRequest(_ request: QueryInterfaceRequest, joinOperator: JoinOperator) -> QueryInterfaceRequest { return request.mapQuery { $0.mapRelation { joinedRelation($0, joinOperator: joinOperator) } } } } -/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) -/// -/// The base protocol for all associations that define a connection between two -/// record types. -public protocol Association: _Association, DerivableRequest { - /// The record type at the origin of the association. - /// - /// In the `belongsTo` association below, it is Book: - /// - /// struct Book: TableRecord { - /// // BelongsToAssociation - /// static let author = belongsTo(Author.self) - /// } - associatedtype OriginRowDecoder - - /// The associated record type. - /// - /// In the `belongsTo` association below, it is Author: - /// - /// struct Book: TableRecord { - /// // BelongsToAssociation - /// static let author = belongsTo(Author.self) - /// } - associatedtype RowDecoder +extension Association { + private func mapImpl(_ transform: (Impl) throws -> Impl) rethrows -> Self { + return try Self.init(_impl: transform(_impl)) + } + private func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> Self { + return mapImpl { $0.mapRelation(transform) } + } +} + +extension Association { + /// The association key defines how rows fetched from this association /// should be consumed. /// @@ -88,26 +126,10 @@ public protocol Association: _Association, DerivableRequest { /// for row in Row.fetchAll(db, request) { /// let team: Team = row["custom"] /// } - var key: String { get } + var key: String { + return _impl.key + } - /// Creates an association with the given key. - /// - /// This new key impacts how rows fetched from the resulting association - /// should be consumed: - /// - /// struct Player: TableRecord { - /// static let team = belongsTo(Team.self) - /// } - /// - /// // Consume rows: - /// let request = Player.including(required: Player.team.forKey("custom")) - /// for row in Row.fetchAll(db, request) { - /// let team: Team = row["custom"] - /// } - func forKey(_ key: String) -> Self -} - -extension Association { /// Creates an association which selects *selection*. /// /// struct Player: TableRecord { @@ -201,6 +223,24 @@ extension Association { return mapRelation { $0.reversed() } } + /// Creates an association with the given key. + /// + /// This new key impacts how rows fetched from the resulting association + /// should be consumed: + /// + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // Consume rows: + /// let request = Player.including(required: Player.team.forKey("custom")) + /// for row in Row.fetchAll(db, request) { + /// let team: Team = row["custom"] + /// } + public func forKey(_ key: String) -> Self { + return mapImpl { $0.forKey(key) } + } + /// Creates an association with the given key. /// /// This new key helps Decodable records decode rows fetched from the @@ -265,28 +305,28 @@ extension Association { /// associated record are selected. The returned association does not /// require that the associated database table contains a matching row. public func including(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder { - return mapRelation { association.joinedRelation($0, joinOperator: .optional) } + return mapRelation { association._impl.joinedRelation($0, joinOperator: .optional) } } /// Creates an association that includes another one. The columns of the /// associated record are selected. The returned association requires /// that the associated database table contains a matching row. public func including(required association: A) -> Self where A.OriginRowDecoder == RowDecoder { - return mapRelation { association.joinedRelation($0, joinOperator: .required) } + return mapRelation { association._impl.joinedRelation($0, joinOperator: .required) } } /// Creates an association that joins another one. 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(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder { - return mapRelation { association.select([]).joinedRelation($0, joinOperator: .optional) } + return mapRelation { association.select([])._impl.joinedRelation($0, joinOperator: .optional) } } /// Creates an association that joins another one. 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(required association: A) -> Self where A.OriginRowDecoder == RowDecoder { - return mapRelation { association.select([]).joinedRelation($0, joinOperator: .required) } + return mapRelation { association.select([])._impl.joinedRelation($0, joinOperator: .required) } } } @@ -314,13 +354,13 @@ extension Association where OriginRowDecoder: MutablePersistableRecord { // Turn the association query into a query interface request: // JOIN association -> SELECT FROM association - return QueryInterfaceRequest(query: SQLSelectQuery(relation: relation)) + return QueryInterfaceRequest(query: SQLSelectQuery(relation: _impl.relation)) // Turn the JOIN condition into a regular WHERE condition .filter { db in // Build a join condition: `association.recordId = record.id` // We still need to replace `record.id` with the actual record id. - guard let joinExpression = try self.joinCondition.sqlExpression(db, leftAlias: recordAlias, rightAlias: associationAlias) else { + guard let joinExpression = try self._impl.joinCondition.sqlExpression(db, leftAlias: recordAlias, rightAlias: associationAlias) else { fatalError("Can't request from record without join condition") } diff --git a/GRDB/QueryInterface/Association/BelongsToAssociation.swift b/GRDB/QueryInterface/Association/BelongsToAssociation.swift index 08336152a..3c7269368 100644 --- a/GRDB/QueryInterface/Association/BelongsToAssociation.swift +++ b/GRDB/QueryInterface/Association/BelongsToAssociation.swift @@ -65,28 +65,33 @@ public struct BelongsToAssociation: Association { /// :nodoc: public typealias RowDecoder = Destination + /// :nodoc: + public var _impl: BelongsToAssociationImpl + + /// :nodoc: + public init(_impl: BelongsToAssociationImpl) { + self._impl = _impl + } +} + +/// :nodoc: +public /* TODO: internal */ struct BelongsToAssociationImpl: AssociationImpl { public var key: String + public let joinCondition: JoinCondition + public var relation: SQLRelation - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ let joinCondition: JoinCondition - - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ var relation: SQLRelation - - public func forKey(_ key: String) -> BelongsToAssociation { + public func forKey(_ key: String) -> BelongsToAssociationImpl { var assoc = self assoc.key = key return assoc } - /// :nodoc: - public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> BelongsToAssociation { + public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> BelongsToAssociationImpl { var assoc = self assoc.relation = transform(relation) return assoc } - /// :nodoc: public func joinedRelation(_ relation: SQLRelation, joinOperator: JoinOperator) -> SQLRelation { let join = Join( joinOperator: joinOperator, @@ -175,9 +180,9 @@ extension TableRecord { foreignKeyRequest: foreignKeyRequest, originIsLeft: true) - return BelongsToAssociation( + return BelongsToAssociation(_impl: BelongsToAssociationImpl( key: key ?? Destination.databaseTableName, joinCondition: joinCondition, - relation: Destination.all().relation) + relation: Destination.all().relation)) } } diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index d83832087..f2a1d47b8 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -65,28 +65,33 @@ public struct HasManyAssociation: Association { /// :nodoc: public typealias RowDecoder = Destination + /// :nodoc: + public var _impl: HasManyAssociationImpl + + /// :nodoc: + public init(_impl: HasManyAssociationImpl) { + self._impl = _impl + } +} + +/// :nodoc: +public /* TODO: internal */ struct HasManyAssociationImpl: AssociationImpl { public var key: String + public let joinCondition: JoinCondition + public var relation: SQLRelation - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ let joinCondition: JoinCondition - - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ var relation: SQLRelation - - public func forKey(_ key: String) -> HasManyAssociation { + public func forKey(_ key: String) -> HasManyAssociationImpl { var assoc = self assoc.key = key return assoc } - /// :nodoc: - public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> HasManyAssociation { + public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> HasManyAssociationImpl { var assoc = self assoc.relation = transform(relation) return assoc } - /// :nodoc: public func joinedRelation(_ relation: SQLRelation, joinOperator: JoinOperator) -> SQLRelation { let join = Join( joinOperator: joinOperator, @@ -264,9 +269,9 @@ extension TableRecord { foreignKeyRequest: foreignKeyRequest, originIsLeft: false) - return HasManyAssociation( + return HasManyAssociation(_impl: HasManyAssociationImpl( key: key ?? Destination.databaseTableName, joinCondition: joinCondition, - relation: Destination.all().relation) + relation: Destination.all().relation)) } } diff --git a/GRDB/QueryInterface/Association/HasOneAssociation.swift b/GRDB/QueryInterface/Association/HasOneAssociation.swift index b003262bf..358b86de5 100644 --- a/GRDB/QueryInterface/Association/HasOneAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneAssociation.swift @@ -60,35 +60,40 @@ /// } /// /// See ForeignKey for more information. -public struct HasOneAssociation : Association { +public struct HasOneAssociation: Association { /// :nodoc: public typealias OriginRowDecoder = Origin /// :nodoc: public typealias RowDecoder = Destination + /// :nodoc: + public var _impl: HasOneAssociationImpl + + /// :nodoc: + public init(_impl: HasOneAssociationImpl) { + self._impl = _impl + } +} + +/// :nodoc: +public /* TODO: internal */ struct HasOneAssociationImpl: AssociationImpl { public var key: String + public let joinCondition: JoinCondition + public var relation: SQLRelation - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ let joinCondition: JoinCondition - - /// :nodoc: - public /* TODO: make internal when no longer required by Association */ var relation: SQLRelation - - public func forKey(_ key: String) -> HasOneAssociation { + public func forKey(_ key: String) -> HasOneAssociationImpl { var assoc = self assoc.key = key return assoc } - /// :nodoc: - public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> HasOneAssociation { + public func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> HasOneAssociationImpl { var assoc = self assoc.relation = transform(relation) return assoc } - /// :nodoc: public func joinedRelation(_ relation: SQLRelation, joinOperator: JoinOperator) -> SQLRelation { let join = Join( joinOperator: joinOperator, @@ -177,9 +182,9 @@ extension TableRecord { foreignKeyRequest: foreignKeyRequest, originIsLeft: false) - return HasOneAssociation( + return HasOneAssociation(_impl: HasOneAssociationImpl( key: key ?? Destination.databaseTableName, joinCondition: joinCondition, - relation: Destination.all().relation) + relation: Destination.all().relation)) } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index df7c4dcbc..46072f51e 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -5,28 +5,28 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { /// associated record are selected. The returned association does not /// require that the associated database table contains a matching row. public func including(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { - return association.joinedRequest(self, joinOperator: .optional) + return association._impl.joinedRequest(self, joinOperator: .optional) } /// 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(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { - return association.joinedRequest(self, joinOperator: .required) + return association._impl.joinedRequest(self, joinOperator: .required) } /// 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(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { - return association.select([]).joinedRequest(self, joinOperator: .optional) + return association.select([])._impl.joinedRequest(self, joinOperator: .optional) } /// 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(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { - return association.select([]).joinedRequest(self, joinOperator: .required) + return association.select([])._impl.joinedRequest(self, joinOperator: .required) } // MARK: - Association Aggregates diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index da7ce435b..83b2aa15f 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -5,7 +5,7 @@ /// > A set of tuples sharing the same attributes; a set of columns and rows. /// /// :nodoc: -public /* TODO: make internal when possible */ struct SQLRelation { +public /* TODO: internal */ struct SQLRelation { var source: SQLSource var selection: [SQLSelectable] var filterPromise: DatabasePromise diff --git a/GRDB/QueryInterface/SQLSelectQuery.swift b/GRDB/QueryInterface/SQLSelectQuery.swift index 0e71c0f94..21e15f808 100644 --- a/GRDB/QueryInterface/SQLSelectQuery.swift +++ b/GRDB/QueryInterface/SQLSelectQuery.swift @@ -359,7 +359,7 @@ extension SQLSelectQuery { /// A.including(optional: A.b.including(required: B.c)) /// /// :nodoc: -public /* TODO: make internal when possible */ enum JoinOperator { +public /* TODO: internal */ enum JoinOperator { case required, optional } @@ -390,7 +390,7 @@ public /* TODO: make internal when possible */ enum JoinOperator { /// .including(optional: Book.author.filter(condition2)) /// /// :nodoc: -public /* TODO: make internal when possible */ struct JoinCondition: Equatable { +public /* TODO: internal */ struct JoinCondition: Equatable { /// Definition of a foreign key var foreignKeyRequest: ForeignKeyRequest