diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index 2f6d9b6ff..ac3051c33 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -704,7 +704,36 @@ extension JoinableRequest { } extension JoinableRequest where Self: SelectionRequest { -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the current selection. + /// + /// // SELECT player.*, team.color + /// // FROM player LEFT JOIN team ... + /// let teamColor = Player.team.select(Column("color")) + /// let request = Player.all().annotated(withOptional: teamColor) + /// + /// This method performs the same SQL request as `including(optional:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String? + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(optional:)`: + /// + /// let teamAlias = TableAlias() + /// let request = Player.all() + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(optional: Player.team.aliased(teamAlias)) public func annotated(withOptional association: A) -> Self where A.OriginRowDecoder == RowDecoder { let alias = TableAlias() let selection = association._sqlAssociation.destination.relation.selectionPromise @@ -717,7 +746,36 @@ extension JoinableRequest where Self: SelectionRequest { }) } -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the current selection. + /// + /// // SELECT player.*, team.color + /// // FROM player JOIN team ... + /// let teamColor = Player.team.select(Column("color")) + /// let request = Player.all().annotated(withRequired: teamColor) + /// + /// This method performs the same SQL request as `including(required:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(required:)`: + /// + /// let teamAlias = TableAlias() + /// let request = Player.all() + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(required: Player.team.aliased(teamAlias)) public func annotated(withRequired association: A) -> Self where A.OriginRowDecoder == RowDecoder { let selection = association._sqlAssociation.destination.relation.selectionPromise let alias = TableAlias() diff --git a/GRDB/QueryInterface/SQL/Table.swift b/GRDB/QueryInterface/SQL/Table.swift index 14e180d8c..722937b6a 100644 --- a/GRDB/QueryInterface/SQL/Table.swift +++ b/GRDB/QueryInterface/SQL/Table.swift @@ -1187,7 +1187,40 @@ extension Table { all().joining(required: association) } -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the columns of the table. + /// + /// let playerTable = Table("player") + /// let teamTable = Table("team") + /// let playerTeam = playerTable.belongsTo(teamTable) + /// + /// // SELECT player.*, team.color + /// // FROM player LEFT JOIN team ... + /// let teamColor = playerTeam.select(Column("color") + /// let request = playerTable.annotated(withOptional: teamColor)) + /// + /// This method performs the same SQL request as `including(optional:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String? + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(optional:)`: + /// + /// let teamAlias = TableAlias() + /// let request = playerTable + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(optional: playerTeam.aliased(teamAlias)) public func annotated(withOptional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder @@ -1195,7 +1228,40 @@ extension Table { all().annotated(withOptional: association) } -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the columns of the table. + /// + /// let playerTable = Table("player") + /// let teamTable = Table("team") + /// let playerTeam = playerTable.belongsTo(teamTable) + /// + /// // SELECT player.*, team.color + /// // FROM player JOIN team ... + /// let teamColor = playerTeam.select(Column("color") + /// let request = playerTable.annotated(withRequired: teamColor)) + /// + /// This method performs the same SQL request as `including(required:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(required:)`: + /// + /// let teamAlias = TableAlias() + /// let request = playerTable + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(required: playerTeam.aliased(teamAlias)) public func annotated(withRequired association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder diff --git a/GRDB/QueryInterface/TableRecord+Association.swift b/GRDB/QueryInterface/TableRecord+Association.swift index e3b367a1a..98b49fa7d 100644 --- a/GRDB/QueryInterface/TableRecord+Association.swift +++ b/GRDB/QueryInterface/TableRecord+Association.swift @@ -621,7 +621,36 @@ extension TableRecord { all().joining(required: association) } -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the selection. + /// + /// // SELECT player.*, team.color + /// // FROM player LEFT JOIN team ... + /// let teamColor = Player.team.select(Column("color")) + /// let request = Player.annotated(withOptional: teamColor) + /// + /// This method performs the same SQL request as `including(optional:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String? + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(optional:)`: + /// + /// let teamAlias = TableAlias() + /// let request = Player + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(optional: Player.team.aliased(teamAlias)) public static func annotated(withOptional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self @@ -629,7 +658,36 @@ extension TableRecord { all().annotated(withOptional: association) } -#warning("TODO: document") + /// Creates a request which appends *columns of an associated record* to + /// the selection. + /// + /// // SELECT player.*, team.color + /// // FROM player JOIN team ... + /// let teamColor = Player.team.select(Column("color")) + /// let request = Player.annotated(withRequired: teamColor) + /// + /// This method performs the same SQL request as `including(required:)`. + /// The difference is in the shape of Decodable records that decode such + /// a request: the associated columns can be decoded at the same level as + /// the main record: + /// + /// struct PlayerWithTeamColor: FetchableRecord, Decodable { + /// var player: Player + /// var color: String + /// } + /// let players = try dbQueue.read { db in + /// try request + /// .asRequest(of: PlayerWithTeamColor.self) + /// .fetchAll(db) + /// } + /// + /// Note: this is a convenience method. You can build the same request with + /// `TableAlias`, `annotated(with:)`, and `joining(required:)`: + /// + /// let teamAlias = TableAlias() + /// let request = Player + /// .annotated(with: teamAlias[Column("color")]) + /// .joining(required: Player.team.aliased(teamAlias)) public static func annotated(withRequired association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == Self