From 8d5e57873e6103c0f93aa5ce679fca5b73465f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 30 Apr 2018 15:33:16 +0200 Subject: [PATCH] Documentation --- .../Association/Association.swift | 25 +++++++++++++------ .../QueryInterfaceRequest+Association.swift | 12 +++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 841739b8c..3ec70811e 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -34,7 +34,7 @@ public protocol Association: SelectionRequest, FilteredRequest, OrderedRequest { /// Creates an association with the given key. /// - /// This new key impacts how rows fetched from this association + /// This new key impacts how rows fetched from the resulting association /// should be consumed: /// /// struct Player: TableRecord { @@ -120,10 +120,6 @@ extension Association { /// // 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() @@ -159,7 +155,8 @@ extension Association { /// Creates an association with the given key. /// - /// This new key helps Decodable records decode fetched rows: + /// This new key helps Decodable records decode rows fetched from the + /// resulting association: /// /// struct Player: TableRecord { /// static let team = belongsTo(Team.self) @@ -209,7 +206,7 @@ extension Association { /// let teamAlias = TableAlias(name: "custom") /// let request = Player /// .including(required: Player.team.aliased(teamAlias)) - /// .filter(sql: "custom.color = ?", arguments: ["red") + /// .filter(sql: "custom.color = ?", arguments: ["red"]) public func aliased(_ alias: TableAlias) -> Self { return mapRequest { $0.aliased(alias) } } @@ -217,6 +214,7 @@ extension Association { public typealias JoinCondition = (_ leftQualifier: SQLTableQualifier, _ rightQualifier: SQLTableQualifier) -> SQLExpression? +/// Turns a ForeignKeyRequest into a JoinCondition struct ForeignKeyJoinConditionRequest { var foreignKeyRequest: ForeignKeyRequest var originIsLeft: Bool @@ -268,6 +266,19 @@ extension Association { } extension Association where LeftAssociated: MutablePersistableRecord { + /// Support for MutablePersistableRecord.request(for:). + /// + /// For example: + /// + /// struct Team: { + /// static let players = hasMany(Book.self) + /// var players: QueryInterfaceRequest { + /// return request(for: Team.players) + /// } + /// } + /// + /// let team: Team = ... + /// let players = try team.players.fetchAll(db) // [Player] func request(from record: LeftAssociated) -> QueryInterfaceRequest { let query = request.query.qualifiedQuery // make sure query has a qualifier let associationQualifier = query.qualifier! diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index 4e7025c57..7b4fc8073 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -47,13 +47,15 @@ extension MutablePersistableRecord { /// /// For example: /// - /// struct Player: TableRecord { - /// static let team = belongsTo(Team.self) + /// struct Team: { + /// static let players = hasMany(Book.self) + /// var players: QueryInterfaceRequest { + /// return request(for: Team.players) + /// } /// } /// - /// let player: Player = ... - /// let request = player.request(for: Player.team) - /// let team = try request.fetchOne(db) // Team? + /// let team: Team = ... + /// let players = try team.players.fetchAll(db) // [Player] public func request(for association: A) -> QueryInterfaceRequest where A.LeftAssociated == Self { return association.request(from: self) }