extension QueryInterfaceRequest where RowDecoder: TableRecord { func joining(_ joinOperator: AssociationJoinOperator, _ association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { let join = AssociationJoin( joinOperator: joinOperator, joinCondition: association.joinCondition, query: association.request.query) return QueryInterfaceRequest(query: query.appendingJoin(join, forKey: association.key)) } // MARK: - Associations /// 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 joining(.optional, association) } /// 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 joining(.required, association) } /// 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 joining(.optional, association.select([])) } /// 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 joining(.required, association.select([])) } // MARK: - Association Aggregates /// TODO public func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { // SELECT author.*, COUNT(book.rowID) AS bookCount // LEFT JOIN book ON ... // GROUP BY author.id let tableAlias = TableAlias() let selectable: SQLSelectable if let alias = aggregate.alias { selectable = tableAlias[aggregate.expression].aliased(alias) } else { selectable = tableAlias[aggregate.expression] } return aggregate.aggregatedRequest(appendingSelection([selectable]), tableAlias) } /// TODO public func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest { // SELECT author.* // LEFT JOIN book ON ... // GROUP BY author.id // HAVING COUNT(book.rowID) > 0 let tableAlias = TableAlias() return aggregate.aggregatedRequest(having(tableAlias[aggregate.expression]), tableAlias) } } extension MutablePersistableRecord { /// Creates a request that fetches the associated record(s). /// /// For example: /// /// struct Team: { /// static let players = hasMany(Player.self) /// var players: QueryInterfaceRequest { /// return request(for: Team.players) /// } /// } /// /// let team: Team = ... /// let players = try team.players.fetchAll(db) // [Player] public func request(for association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return association.request(from: self) } } extension TableRecord { // MARK: - Associations /// 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 static func including(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return all().including(optional: association) } /// 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 static func including(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return all().including(required: association) } /// 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 static func joining(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return all().joining(optional: association) } /// 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 static func joining(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return all().joining(required: association) } // MARK: - Association Aggregates /// TODO public static func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { return all().annotated(with: aggregate) } /// TODO public static func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest { return all().having(aggregate) } }