Skip to content

Commit 9c84335

Browse files
authored
A better implementation for 'map' (#222)
* Fix 'map' when calling database functions. * wip * wip
1 parent b78cf04 commit 9c84335

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

Package.resolved

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/StructuredQueriesCore/Optional.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ extension QueryExpression where QueryValue: _OptionalProtocol {
275275
public func map<T>(
276276
_ transform: (SQLQueryExpression<QueryValue.Wrapped>) -> some QueryExpression<T>
277277
) -> some QueryExpression<T?> {
278-
SQLQueryExpression(transform(SQLQueryExpression(queryFragment)).queryFragment)
278+
Case(SQLQueryExpression("\(self) IS NULL"))
279+
.when(SQLQueryExpression("1"), then: SQLQueryExpression("NULL"))
280+
.else(SQLQueryExpression(transform(SQLQueryExpression(queryFragment)).queryFragment))
279281
}
280282

281283
/// Creates a new optional expression from this one by applying an unwrapped version of this
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Dependencies
2+
import Foundation
3+
import InlineSnapshotTesting
4+
import SQLite3
5+
import StructuredQueries
6+
import StructuredQueriesSQLite
7+
import StructuredQueriesTestSupport
8+
import Testing
9+
import _StructuredQueriesSQLite
10+
11+
extension SnapshotTests {
12+
@Suite struct MapTests {
13+
@Dependency(\.defaultDatabase) var database
14+
15+
@Test func mapWithDatabaseFunction() throws {
16+
$increment.install(database.handle)
17+
try database.execute("""
18+
CREATE TABLE "optionalIntegers" (
19+
"value" INTEGER
20+
) STRICT
21+
""")
22+
try database.execute("""
23+
INSERT INTO "optionalIntegers" ("value") VALUES (1), (NULL), (3)
24+
""")
25+
26+
assertQuery(
27+
OptionalInteger.select {
28+
$0.value.map { $increment($0) }
29+
}
30+
) {
31+
"""
32+
SELECT CASE "optionalIntegers"."value" IS NULL WHEN 1 THEN NULL ELSE "increment"("optionalIntegers"."value") END
33+
FROM "optionalIntegers"
34+
"""
35+
} results: {
36+
"""
37+
┌─────┐
38+
│ 2 │
39+
│ nil │
40+
│ 4 │
41+
└─────┘
42+
"""
43+
}
44+
}
45+
}
46+
}
47+
48+
@Table struct OptionalInteger {
49+
let value: Int?
50+
}
51+
@DatabaseFunction(isDeterministic: true)
52+
private func increment(_ value: Int) -> Int {
53+
value + 1
54+
}

Tests/StructuredQueriesTests/SelectTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ extension SnapshotTests {
13691369
}
13701370
assertQuery(query) {
13711371
"""
1372-
SELECT ("reminders"."priority") < (3)
1372+
SELECT CASE "reminders"."priority" IS NULL WHEN 1 THEN NULL ELSE ("reminders"."priority") < (3) END
13731373
FROM "reminders"
13741374
"""
13751375
} results: {

0 commit comments

Comments
 (0)