diff --git a/Documentation/SQLInterpolation.md b/Documentation/SQLInterpolation.md index 4e5017347..571a95e13 100644 --- a/Documentation/SQLInterpolation.md +++ b/Documentation/SQLInterpolation.md @@ -329,6 +329,10 @@ This chapter lists all kinds of supported interpolations. // SELECT * FROM player extension Player: TableRecord { } "SELECT * FROM \(Player.self)" + + // INSERT INTO player ... + let player: Player = ... + "INSERT INTO \(tableOf: player) ..." ``` - [Expressions] and [values]: diff --git a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift index cd1baece3..1b16bed5c 100644 --- a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift @@ -9,6 +9,15 @@ extension SQLInterpolation { sql += table.databaseTableName.quotedDatabaseIdentifier } + /// Appends the table name of the record. + /// + /// // INSERT INTO player ... + /// let player: Player = ... + /// let request: SQLRequest = "INSERT INTO \(tableOf: player) ..." + public mutating func appendInterpolation(tableOf record: T) { + sql += type(of: record).databaseTableName.quotedDatabaseIdentifier + } + /// Appends the selectable SQL. /// /// // SELECT * FROM player diff --git a/Tests/GRDBTests/SQLInterpolationTests.swift b/Tests/GRDBTests/SQLInterpolationTests.swift index cf19eb098..3f83b825b 100644 --- a/Tests/GRDBTests/SQLInterpolationTests.swift +++ b/Tests/GRDBTests/SQLInterpolationTests.swift @@ -39,9 +39,11 @@ class SQLInterpolationTests: GRDBTestCase { var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) struct Player: TableRecord { } - sql.appendInterpolation(Player.self) + sql.appendInterpolation(Player.self); sql.appendLiteral("\n") + sql.appendInterpolation(tableOf: Player()) XCTAssertEqual(sql.sql, """ + "player" "player" """) XCTAssert(sql.arguments.isEmpty) diff --git a/Tests/GRDBTests/SQLLiteralTests.swift b/Tests/GRDBTests/SQLLiteralTests.swift index 48e06628d..a2bc10af4 100644 --- a/Tests/GRDBTests/SQLLiteralTests.swift +++ b/Tests/GRDBTests/SQLLiteralTests.swift @@ -157,15 +157,26 @@ extension SQLLiteralTests { func testTableInterpolation() { struct Player: TableRecord { } - let query: SQLLiteral = """ - SELECT * - FROM \(Player.self) - """ - XCTAssertEqual(query.sql, #""" - SELECT * - FROM "player" - """#) - XCTAssert(query.arguments.isEmpty) + do { + let query: SQLLiteral = """ + SELECT * + FROM \(Player.self) + """ + XCTAssertEqual(query.sql, """ + SELECT * + FROM "player" + """) + XCTAssert(query.arguments.isEmpty) + } + do { + let query: SQLLiteral = """ + INSERT INTO \(tableOf: Player()) DEFAULT VALUES + """ + XCTAssertEqual(query.sql, """ + INSERT INTO "player" DEFAULT VALUES + """) + XCTAssert(query.arguments.isEmpty) + } } func testExpressibleInterpolation() {