Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Sources/StructuredQueriesCore/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ extension QueryExpression where QueryValue: _OptionalProtocol {
public func map<T>(
_ transform: (SQLQueryExpression<QueryValue.Wrapped>) -> some QueryExpression<T>
) -> some QueryExpression<T?> {
SQLQueryExpression(transform(SQLQueryExpression(queryFragment)).queryFragment)
Case(SQLQueryExpression("\(self) IS NULL"))
.when(SQLQueryExpression("1"), then: SQLQueryExpression("NULL"))
.else(SQLQueryExpression(transform(SQLQueryExpression(queryFragment)).queryFragment))
}

/// Creates a new optional expression from this one by applying an unwrapped version of this
Expand Down
54 changes: 54 additions & 0 deletions Tests/StructuredQueriesTests/MapTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Dependencies
import Foundation
import InlineSnapshotTesting
import SQLite3
import StructuredQueries
import StructuredQueriesSQLite
import StructuredQueriesTestSupport
import Testing
import _StructuredQueriesSQLite

extension SnapshotTests {
@Suite struct MapTests {
@Dependency(\.defaultDatabase) var database

@Test func mapWithDatabaseFunction() throws {
$increment.install(database.handle)
try database.execute("""
CREATE TABLE "optionalIntegers" (
"value" INTEGER
) STRICT
""")
try database.execute("""
INSERT INTO "optionalIntegers" ("value") VALUES (1), (NULL), (3)
""")

assertQuery(
OptionalInteger.select {
$0.value.map { $increment($0) }
}
) {
"""
SELECT CASE "optionalIntegers"."value" IS NULL WHEN 1 THEN NULL ELSE "increment"("optionalIntegers"."value") END
FROM "optionalIntegers"
"""
} results: {
"""
┌─────┐
│ 2 │
│ nil │
│ 4 │
└─────┘
"""
}
}
}
}

@Table struct OptionalInteger {
let value: Int?
}
@DatabaseFunction(isDeterministic: true)
private func increment(_ value: Int) -> Int {
value + 1
}
2 changes: 1 addition & 1 deletion Tests/StructuredQueriesTests/SelectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@ extension SnapshotTests {
}
assertQuery(query) {
"""
SELECT ("reminders"."priority") < (3)
SELECT CASE "reminders"."priority" IS NULL WHEN 1 THEN NULL ELSE ("reminders"."priority") < (3) END
FROM "reminders"
"""
} results: {
Expand Down