-
-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coalesce free function and Row method
- Loading branch information
Phil Mitchell
committed
Sep 30, 2024
1 parent
dc03b8a
commit d7754fa
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import XCTest | ||
import GRDB | ||
|
||
private struct Player: Codable, FetchableRecord, PersistableRecord { | ||
var id: Int64 | ||
var name: String? | ||
var nickname: String? | ||
var score: Int | ||
} | ||
|
||
class SimpleFunctionTests: GRDBTestCase { | ||
|
||
override func setup(_ dbWriter: some DatabaseWriter) throws { | ||
try dbWriter.write { db in | ||
try db.create(table: "player") { t in | ||
t.primaryKey("id", .integer) | ||
t.column("name", .text) | ||
t.column("nickname", .text) | ||
t.column("score", .integer) | ||
} | ||
|
||
try Player(id: 1, name: "Arthur", nickname: "Artie", score: 100).insert(db) | ||
try Player(id: 2, name: "Jacob", nickname: nil, score: 200).insert(db) | ||
try Player(id: 3, name: nil, nickname: nil, score: 200).insert(db) | ||
} | ||
} | ||
|
||
func testCoalesce() throws { | ||
let dbQueue = try makeDatabaseQueue() | ||
try dbQueue.read { db in | ||
do { | ||
let request = Player.annotated(with: coalesce([Column("nickname"), Column("name")])) | ||
try assertEqualSQL(db, request, """ | ||
SELECT *, COALESCE("nickname", "name") \ | ||
FROM "player" | ||
""") | ||
} | ||
do { | ||
let request = Player.annotated(with: coalesce([Column("nickname"), Column("name")]).forKey("foo")) | ||
try assertEqualSQL(db, request, """ | ||
SELECT *, COALESCE("nickname", "name") AS "foo" \ | ||
FROM "player" | ||
""") | ||
} | ||
} | ||
} | ||
|
||
func testRowCoalesce() throws { | ||
let dbQueue = try makeDatabaseQueue() | ||
try dbQueue.inDatabase { db in | ||
let request = Player.all() | ||
let rows = try Row.fetchAll(db, request) | ||
var row = rows[0] | ||
XCTAssertEqual(row.coalesce(["nickname", "name"]), "Artie") | ||
XCTAssertEqual(row.coalesce([Column("nickname"), Column("name")]), "Artie") | ||
row = rows[1] | ||
XCTAssertEqual(row.coalesce(["nickname", "name"]), "Jacob") | ||
XCTAssertEqual(row.coalesce([Column("nickname"), Column("name")]), "Jacob") | ||
row = rows[2] | ||
var result: String? = row.coalesce(["nickname", "name"]) | ||
XCTAssertNil(result) | ||
result = row.coalesce([Column("nickname"), Column("name")]) | ||
XCTAssertNil(result) | ||
} | ||
} | ||
|
||
} |