New SQL interpolation "... \(tableOf: record) ..." (addresses #524)

This commit is contained in:
Gwendal Roué committed 2019-05-22 08:53:50 +02:00
1 parent 7461342089
commit 21d2df9ade
4 files changed
+36 -10

No files matched your search

+4
View File
@@ -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]:
@@ -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<Player> = "INSERT INTO \(tableOf: player) ..."
public mutating func appendInterpolation<T: TableRecord>(tableOf record: T) {
sql += type(of: record).databaseTableName.quotedDatabaseIdentifier
}
/// Appends the selectable SQL.
///
/// // SELECT * FROM player
+3 -1
View File
@@ -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)
+20 -9
View File
@@ -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() {