Skip to content

Commit 5696c4f

Browse files
committed
Add nullif scalar function
1 parent 5caf489 commit 5696c4f

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

Sources/StructuredQueriesCore/Documentation.docc/Articles/ScalarFunctions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Explore the full list of available functions below.
4040

4141
- ``QueryExpression/??(_:_:)``
4242
- ``QueryExpression/ifnull(_:)``
43+
- ``QueryExpression/nullif(_:)``
4344

4445
### Bytes
4546

Sources/StructuredQueriesCore/ScalarFunctions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ where QueryValue: _OptionalPromotable, QueryValue._Optionalized.Wrapped: Numeric
6767
}
6868
}
6969

70+
extension QueryExpression where QueryValue: _OptionalPromotable {
71+
/// Wraps this query expression with the `nullif` function.
72+
///
73+
/// ```swift
74+
/// Reminder
75+
/// .select { $0.title.nullif("") }
76+
/// // SELECT nullif("reminders"."title", '')
77+
/// // FROM "reminders"
78+
/// ```
79+
///
80+
/// - Parameter other: An expression to compare against this expression.
81+
/// - Returns: An optional expression of the `nullif` function wrapping this expression, which is
82+
/// `NULL` when both expressions are equal.
83+
public func nullif<Other: QueryExpression>(
84+
_ other: Other
85+
) -> some QueryExpression<QueryValue._Optionalized>
86+
where
87+
Other.QueryValue: _OptionalPromotable,
88+
Other.QueryValue._Optionalized == QueryValue._Optionalized
89+
{
90+
QueryFunction("nullif", self, other)
91+
}
92+
}
93+
7094
extension QueryExpression where QueryValue: _OptionalProtocol {
7195
/// Wraps this optional query expression with the `ifnull` function.
7296
///

Tests/StructuredQueriesTests/ScalarFunctionsTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,53 @@ extension SnapshotTests {
220220
"""
221221
}
222222
}
223+
224+
@Test func nullif() {
225+
assertQuery(Reminder.select { ($0.priority, $0.priority.nullif(Priority.high)) }) {
226+
"""
227+
SELECT "reminders"."priority", nullif("reminders"."priority", 3)
228+
FROM "reminders"
229+
"""
230+
} results: {
231+
"""
232+
┌─────────┬─────────┐
233+
│ nil │ nil │
234+
│ nil │ nil │
235+
│ .high │ nil │
236+
│ nil │ nil │
237+
│ nil │ nil │
238+
│ .high │ nil │
239+
│ .low │ .low │
240+
│ .high │ nil │
241+
│ nil │ nil │
242+
│ .medium │ .medium │
243+
└─────────┴─────────┘
244+
"""
245+
}
246+
}
247+
248+
@Test func nullifPromotesNonOptional() {
249+
assertQuery(Reminder.select { $0.title.nullif("Groceries") }) {
250+
"""
251+
SELECT nullif("reminders"."title", 'Groceries')
252+
FROM "reminders"
253+
"""
254+
} results: {
255+
"""
256+
┌────────────────────────────┐
257+
│ nil │
258+
"Haircut"
259+
"Doctor appointment"
260+
"Take a walk"
261+
"Buy concert tickets"
262+
"Pick up kids from school"
263+
"Get laundry"
264+
"Take out trash"
265+
"Call accountant"
266+
"Send weekly emails"
267+
└────────────────────────────┘
268+
"""
269+
}
270+
}
223271
}
224272
}

0 commit comments

Comments
 (0)