diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index f9666c953..69815ffa3 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -83,13 +83,6 @@ public /* TODO: internal */ protocol AssociationImpl { var joinCondition: JoinCondition { get } } -extension AssociationImpl { - /// Returns a request joined with self. - func joinedRequest(_ request: QueryInterfaceRequest, joinOperator: JoinOperator) -> QueryInterfaceRequest { - return request.mapQuery { $0.mapRelation { joinedRelation($0, joinOperator: joinOperator) } } - } -} - extension Association { private func mapImpl(_ transform: (Impl) throws -> Impl) rethrows -> Self { return try Self.init(_impl: transform(_impl)) diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index 46072f51e..d381f4af1 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -4,34 +4,34 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { /// Creates a request that includes an association. The columns of the /// 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._impl.joinedRequest(self, joinOperator: .optional) + public func including(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + return mapQuery { $0.mapRelation { association._impl.joinedRelation($0, 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._impl.joinedRequest(self, joinOperator: .required) + public func including(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + return mapQuery { $0.mapRelation { association._impl.joinedRelation($0, 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([])._impl.joinedRequest(self, joinOperator: .optional) + public func joining(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + return mapQuery { $0.mapRelation { association.select([])._impl.joinedRelation($0, 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([])._impl.joinedRequest(self, joinOperator: .required) + public func joining(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + return mapQuery { $0.mapRelation { association.select([])._impl.joinedRelation($0, joinOperator: .required) } } } // MARK: - Association Aggregates - private func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { + private func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { let (request, expression) = aggregate.prepare(self) if let alias = aggregate.alias { return request.annotated(with: [expression.aliased(alias)]) @@ -46,7 +46,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { /// // FROM player LEFT JOIN book ... /// var request = Player.all() /// request = request.annotated(with: Player.books.count) - public func annotated(with aggregates: AssociationAggregate...) -> QueryInterfaceRequest { + public func annotated(with aggregates: AssociationAggregate...) -> QueryInterfaceRequest { return annotated(with: aggregates) } @@ -56,7 +56,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { /// // FROM player LEFT JOIN book ... /// var request = Player.all() /// request = request.annotated(with: [Player.books.count]) - public func annotated(with aggregates: [AssociationAggregate]) -> QueryInterfaceRequest { + public func annotated(with aggregates: [AssociationAggregate]) -> QueryInterfaceRequest { return aggregates.reduce(self) { request, aggregate in request.annotated(with: aggregate) } @@ -70,7 +70,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { /// // HAVING COUNT(DISTINCT book.rowid) = 0 /// var request = Player.all() /// request = request.having(Player.books.isEmpty) - public func having(_ predicate: AssociationAggregate) -> QueryInterfaceRequest { + public func having(_ predicate: AssociationAggregate) -> QueryInterfaceRequest { let (request, expression) = predicate.prepare(self) return request.having(expression) }