diff --git a/GRDB/Core/FetchRequest.swift b/GRDB/Core/FetchRequest.swift index d17185743..bcea72d7e 100644 --- a/GRDB/Core/FetchRequest.swift +++ b/GRDB/Core/FetchRequest.swift @@ -73,23 +73,6 @@ public protocol FetchRequest: SelectStatementRequest { } extension FetchRequest { - /// Returns a request bound to type T. - /// - /// The returned request can fetch if the type T is fetchable (Row, - /// value, record). - /// - /// // Int? - /// let maxScore = Player - /// .select(max(scoreColumn)) - /// .asRequest(of: Int.self) // <-- - /// .fetchOne(db) - /// - /// - parameter type: The fetched type T - /// - returns: A typed request bound to type T. - public func asRequest(of type: T.Type) -> AnyFetchRequest { - return AnyFetchRequest(self) - } - /// Returns an adapted request. public func adapted(_ adapter: @escaping (Database) throws -> RowAdapter) -> AdaptedFetchRequest { return AdaptedFetchRequest(self, adapter) diff --git a/GRDB/Legacy/Fixits-0.109.0.swift b/GRDB/Legacy/Fixits-0.109.0.swift index 6c2034ea6..1b9dcc727 100644 --- a/GRDB/Legacy/Fixits-0.109.0.swift +++ b/GRDB/Legacy/Fixits-0.109.0.swift @@ -1,4 +1,4 @@ extension FetchRequest { - @available(*, unavailable, renamed:"asRequest(of:)") + @available(*, unavailable, message:"Use QueryInterfaceRequest.asRequest(of:), or AnyFetchRequest") public func bound(to type: T.Type) -> AnyFetchRequest { preconditionFailure() } } diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index c0c1d1883..841739b8c 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -1,5 +1,7 @@ +/// [**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. +/// record types. public protocol Association: SelectionRequest, FilteredRequest, OrderedRequest { associatedtype LeftAssociated associatedtype RightAssociated @@ -49,6 +51,7 @@ public protocol Association: SelectionRequest, FilteredRequest, OrderedRequest { /// :nodoc: var request: AssociationRequest { get } + // TODO: this one prevents "through" associations like HasOneThrough, HasManyThrough, etc. /// :nodoc: func joinCondition(_ db: Database) throws -> JoinCondition @@ -59,26 +62,97 @@ public protocol Association: SelectionRequest, FilteredRequest, OrderedRequest { extension Association { /// Creates an association with a new net of selected columns. /// - /// Any previous selection is replaced. + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.color + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// let association = Player.team.select([Column("color")]) + /// var request = Player.including(required: association) + /// + /// Any previous selection is replaced: + /// + /// // SELECT player.*, team.color + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// let association = Player.team + /// .select([Column("id")]) + /// .select([Column("color")]) + /// var request = Player.including(required: association) public func select(_ selection: [SQLSelectable]) -> Self { return mapRequest { $0.select(selection) } } /// Creates an association with the provided *predicate promise* added to /// the eventual set of already applied predicates. + /// + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId AND 1 + /// let association = Player.team.filter { db in true } + /// var request = Player.including(required: association) public func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self { return mapRequest { $0.filter(predicate) } } /// Creates an association with the provided *orderings*. /// - /// Any previous ordering is replaced. + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// // ORDER BY team.name + /// let association = Player.team.order(Column("name")) + /// var request = Player.including(required: association) + /// + /// Any previous ordering is replaced: + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// // ORDER BY team.name + /// let association = Player.team.order(Column("name")) + /// var request = Player.including(required: association) + /// + /// // SELECT * FROM player ORDER BY name + /// let association = Player.team + /// .order(Column("color")) + /// .reversed() + /// .order(Column("name")) + /// var request = Player.including(required: association) public func order(_ orderings: [SQLOrderingTerm]) -> Self { return mapRequest { $0.order(orderings) } } - /// Creates an association that reverses applied orderings. If no ordering - /// was applied, the returned request is identical. + /// Creates an association that reverses applied orderings. + /// + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// // ORDER BY team.name DESC + /// let association = Player.team.order(Column("name")).reversed() + /// var request = Player.including(required: association) + /// + /// If no ordering was applied, the returned association is identical. + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// let association = Player.team.reversed() + /// var request = Player.including(required: association) public func reversed() -> Self { return mapRequest { $0.reversed() } } @@ -95,7 +169,7 @@ extension Association { /// let player: Player /// let team: Team /// - /// static func all() -> AnyFetchRequest { + /// static func all() -> QueryInterfaceRequest { /// return Player /// .including(required: Player.team.forKey(CodingKeys.team)) /// .asRequest(of: PlayerInfo.self) @@ -107,7 +181,7 @@ extension Association { public func forKey(_ codingKey: CodingKey) -> Self { return forKey(codingKey.stringValue) } - + /// Creates an association that allows you to define unambiguous expressions /// based on the associated record. /// @@ -124,7 +198,7 @@ extension Association { /// let teamAlias = TableAlias() /// let request = Player /// .including(required: Player.team.aliased(teamAlias)) - /// .filter(teamAlias[Column("color"] == "red") + /// .filter(teamAlias[Column("color")] == "red") /// /// When you give a name to a table alias, you can reliably inject sql /// snippets in your requests: diff --git a/GRDB/QueryInterface/Association/AssociationRequest.swift b/GRDB/QueryInterface/Association/AssociationRequest.swift index 4f5b252eb..b584d20f6 100644 --- a/GRDB/QueryInterface/Association/AssociationRequest.swift +++ b/GRDB/QueryInterface/Association/AssociationRequest.swift @@ -1,3 +1,5 @@ +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// In `SELECT a.*, b.* FROM a JOIN b ON b.aid = a.id AND b.name = 'foo'`, /// the AssociationRequest is (`b.*` + `b.name = 'foo'`). public struct AssociationRequest { diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 0204305d1..aa782b4b8 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -110,11 +110,17 @@ extension QueryInterfaceRequest : SelectionRequest, FilteredRequest, Aggregating return QueryInterfaceRequest(query: query.order(orderings)) } - /// Creates a request sorted in reversed order. + /// Creates a request that reverses applied orderings. /// /// // SELECT * FROM players ORDER BY name DESC /// var request = Player.all().order(Column("name")) /// request = request.reversed() + /// + /// If no ordering was applied, the returned request is identical. + /// + /// // SELECT * FROM players + /// var request = Player.all() + /// request = request.reversed() public func reversed() -> QueryInterfaceRequest { return QueryInterfaceRequest(query: query.reversed()) } @@ -155,6 +161,23 @@ extension QueryInterfaceRequest : SelectionRequest, FilteredRequest, Aggregating } return QueryInterfaceRequest(query: query.qualified(with: &alias.qualifier)) } + + /// Creates a request bound to type Target. + /// + /// The returned request can fetch if the type Target is fetchable (Row, + /// value, record). + /// + /// // Int? + /// let maxScore = try Player + /// .select(max(scoreColumn)) + /// .asRequest(of: Int.self) // <-- + /// .fetchOne(db) + /// + /// - parameter type: The fetched type Target + /// - returns: A typed request bound to type Target. + public func asRequest(of type: Target.Type) -> QueryInterfaceRequest { + return QueryInterfaceRequest(query: query) + } } /// Conditional conformance to TableRequest when RowDecoder conforms diff --git a/GRDB/QueryInterface/RequestProtocols.swift b/GRDB/QueryInterface/RequestProtocols.swift index bf2987935..b1df8148f 100644 --- a/GRDB/QueryInterface/RequestProtocols.swift +++ b/GRDB/QueryInterface/RequestProtocols.swift @@ -241,13 +241,14 @@ public protocol OrderedRequest { /// .order([Column("name")]) func order(_ orderings: [SQLOrderingTerm]) -> Self - /// Creates a request that reverses applied orderings. If no ordering - /// was applied, the returned request is identical. + /// Creates a request that reverses applied orderings. /// /// // SELECT * FROM players ORDER BY name DESC /// var request = Player.all().order(Column("name")) /// request = request.reversed() /// + /// If no ordering was applied, the returned request is identical. + /// /// // SELECT * FROM players /// var request = Player.all() /// request = request.reversed()