mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
New SQL interpolation "... \(tableOf: record) ..." (addresses #524)
This commit is contained in:
1 parent
7461342089
commit
21d2df9ade
4 files changed
+36
-10
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user