diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 193758695..df04dca45 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -114,6 +114,23 @@ extension Association { return mapDestinationRelation { $0.select(selection) } } + /// Creates an association which appends *selection*. + /// + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.color, team.name + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// let association = Player.team + /// .select([Column("color")]) + /// .annotated(with: [Column("name")]) + /// var request = Player.including(required: association) + public func annotated(with selection: [SQLSelectable]) -> Self { + return mapDestinationRelation { $0.annotated(with: selection) } + } + /// Creates an association with the provided *predicate promise* added to /// the eventual set of already applied predicates. /// diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index bdd126f8f..8378516ce 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -179,7 +179,7 @@ extension TableRecord { public static func annotated(with aggregates: [AssociationAggregate]) -> QueryInterfaceRequest { return all().annotated(with: aggregates) } - + /// Creates a request with the provided aggregate *predicate*. /// /// // SELECT player.* diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 00afbc9bd..bf979321b 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -513,6 +513,26 @@ extension TableRecord { return all().select(literal: sqlLiteral, as: type) } + /// Creates a request which appends *selection*. + /// + /// // SELECT id, email, name FROM player + /// le request = Player + /// .select([Column("id"), Column("email")]) + /// .annotated(with: [Column("name")]) + public static func annotated(with selection: [SQLSelectable]) -> QueryInterfaceRequest { + return all().annotated(with: selection) + } + + /// Creates a request which appends *selection*. + /// + /// // SELECT id, email, name FROM player + /// le request = Player + /// .select([Column("id"), Column("email")]) + /// .annotated(with: Column("name")) + public static func annotated(with selection: SQLSelectable...) -> QueryInterfaceRequest { + return all().annotated(with: selection) + } + /// Creates a request with the provided *predicate*. /// /// // SELECT * FROM player WHERE email = 'arthur@example.com' diff --git a/GRDB/QueryInterface/RequestProtocols.swift b/GRDB/QueryInterface/RequestProtocols.swift index df70b30cc..02d3f1d93 100644 --- a/GRDB/QueryInterface/RequestProtocols.swift +++ b/GRDB/QueryInterface/RequestProtocols.swift @@ -19,6 +19,15 @@ public protocol SelectionRequest { /// .select([Column("id")]) /// .select([Column("email")]) func select(_ selection: [SQLSelectable]) -> Self + + /// Creates a request which appends *selection*. + /// + /// // SELECT id, email, name FROM player + /// var request = Player.all() + /// request = request + /// .select([Column("id"), Column("email")]) + /// .annotated(with: [Column("name")]) + func annotated(with selection: [SQLSelectable]) -> Self } /// :nodoc: @@ -84,6 +93,17 @@ extension SelectionRequest { // NOT TESTED return select(SQLSelectionLiteral(literal: sqlLiteral)) } + + /// Creates a request which appends *selection*. + /// + /// // SELECT id, email, name FROM player + /// var request = Player.all() + /// request = request + /// .select([Column("id"), Column("email")]) + /// .annotated(with: Column("name")) + public func annotated(with selection: SQLSelectable...) -> Self { + return annotated(with: selection) + } } // MARK: - FilteredRequest diff --git a/Tests/GRDBTests/QueryInterfaceRequestTests.swift b/Tests/GRDBTests/QueryInterfaceRequestTests.swift index 2ff0b7c92..23e669651 100644 --- a/Tests/GRDBTests/QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/QueryInterfaceRequestTests.swift @@ -241,7 +241,43 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual(row["agePlusOne"] as Int, 43) } } - + + func testAnnotated() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + let request = Reader.annotated(with: [Col.id - 1]) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT *, (\"id\" - 1) FROM \"readers\"") + } + do { + let request = Reader.annotated(with: Col.id - 1, Col.id + 1) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT *, (\"id\" - 1), (\"id\" + 1) FROM \"readers\"") + } + do { + let request = tableRequest.annotated(with: [Col.id - 1]) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT *, (\"id\" - 1) FROM \"readers\"") + } + do { + let request = tableRequest.annotated(with: Col.id - 1, Col.id + 1) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT *, (\"id\" - 1), (\"id\" + 1) FROM \"readers\"") + } + do { + let request = tableRequest.select(Col.name).annotated(with: [Col.id - 1]) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT \"name\", (\"id\" - 1) FROM \"readers\"") + } + do { + let request = tableRequest.select(Col.name).annotated(with: Col.id - 1, Col.id + 1) + _ = try Row.fetchAll(db, request) + XCTAssertEqual(lastSQLQuery, "SELECT \"name\", (\"id\" - 1), (\"id\" + 1) FROM \"readers\"") + } + } + } + func testMultipleSelect() throws { let dbQueue = try makeDatabaseQueue() XCTAssertEqual(