Skip to content

Commit d30b935

Browse files
authored
More == overloads. (#3)
1 parent 44c58da commit d30b935

File tree

3 files changed

+117
-91
lines changed

3 files changed

+117
-91
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 112 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ extension QueryExpression where QueryValue: QueryBindable {
1414
/// - rhs: Another expression to compare.
1515
/// - Returns: A predicate expression.
1616
public static func == (
17-
lhs: Self, rhs: some QueryExpression<QueryValue>
17+
lhs: Self,
18+
rhs: some QueryExpression<QueryValue>
1819
) -> some QueryExpression<Bool> {
1920
lhs.eq(rhs)
2021
}
@@ -34,27 +35,12 @@ extension QueryExpression where QueryValue: QueryBindable {
3435
/// - rhs: Another expression to compare.
3536
/// - Returns: A predicate expression.
3637
public static func != (
37-
lhs: Self, rhs: some QueryExpression<QueryValue>
38+
lhs: Self,
39+
rhs: some QueryExpression<QueryValue>
3840
) -> some QueryExpression<Bool> {
3941
lhs.neq(rhs)
4042
}
4143

42-
@_disfavoredOverload
43-
@_documentation(visibility: private)
44-
public static func == (
45-
lhs: Self, rhs: some QueryExpression<QueryValue?>
46-
) -> some QueryExpression<Bool> {
47-
BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS" : "=", rhs: rhs)
48-
}
49-
50-
@_disfavoredOverload
51-
@_documentation(visibility: private)
52-
public static func != (
53-
lhs: Self, rhs: some QueryExpression<QueryValue?>
54-
) -> some QueryExpression<Bool> {
55-
BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS NOT" : "<>", rhs: rhs)
56-
}
57-
5844
/// Returns a predicate expression indicating whether two query expressions are equal.
5945
///
6046
/// ```swift
@@ -119,34 +105,6 @@ private func isNull<Value>(_ expression: some QueryExpression<Value>) -> Bool {
119105
}
120106

121107
extension QueryExpression where QueryValue: QueryBindable & _OptionalProtocol {
122-
@_documentation(visibility: private)
123-
public static func == (
124-
lhs: Self, rhs: some QueryExpression<QueryValue.Wrapped>
125-
) -> some QueryExpression<Bool> {
126-
BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS" : "=", rhs: rhs)
127-
}
128-
129-
@_documentation(visibility: private)
130-
public static func != (
131-
lhs: Self, rhs: some QueryExpression<QueryValue.Wrapped>
132-
) -> some QueryExpression<Bool> {
133-
BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS NOT" : "<>", rhs: rhs)
134-
}
135-
136-
@_documentation(visibility: private)
137-
public static func == (
138-
lhs: Self, rhs: some QueryExpression<QueryValue>
139-
) -> some QueryExpression<Bool> {
140-
BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS" : "=", rhs: rhs)
141-
}
142-
143-
@_documentation(visibility: private)
144-
public static func != (
145-
lhs: Self, rhs: some QueryExpression<QueryValue>
146-
) -> some QueryExpression<Bool> {
147-
BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS NOT" : "<>", rhs: rhs)
148-
}
149-
150108
@_documentation(visibility: private)
151109
public func eq(_ other: some QueryExpression<QueryValue.Wrapped>) -> some QueryExpression<Bool> {
152110
BinaryOperator(lhs: self, operator: "=", rhs: other)
@@ -183,16 +141,6 @@ extension QueryExpression where QueryValue: QueryBindable & _OptionalProtocol {
183141
}
184142

185143
extension QueryExpression where QueryValue: QueryBindable {
186-
@_documentation(visibility: private)
187-
public static func == (lhs: Self, rhs: _Null<QueryValue>) -> some QueryExpression<Bool> {
188-
lhs.is(rhs)
189-
}
190-
191-
@_documentation(visibility: private)
192-
public static func != (lhs: Self, rhs: _Null<QueryValue>) -> some QueryExpression<Bool> {
193-
lhs.isNot(rhs)
194-
}
195-
196144
@_documentation(visibility: private)
197145
public func `is`(
198146
_ other: _Null<QueryValue>
@@ -217,6 +165,80 @@ extension _Null: ExpressibleByNilLiteral {
217165
public init(nilLiteral: ()) {}
218166
}
219167

168+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
169+
@_disfavoredOverload
170+
@_documentation(visibility: private)
171+
public func == <QueryValue>(
172+
lhs: any QueryExpression<QueryValue>,
173+
rhs: some QueryExpression<QueryValue?>
174+
) -> some QueryExpression<Bool> {
175+
BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS" : "=", rhs: rhs)
176+
}
177+
178+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
179+
@_disfavoredOverload
180+
@_documentation(visibility: private)
181+
public func != <QueryValue>(
182+
lhs: any QueryExpression<QueryValue>,
183+
rhs: some QueryExpression<QueryValue?>
184+
) -> some QueryExpression<Bool> {
185+
BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS NOT" : "<>", rhs: rhs)
186+
}
187+
188+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
189+
@_documentation(visibility: private)
190+
public func == <QueryValue: _OptionalProtocol>(
191+
lhs: any QueryExpression<QueryValue>,
192+
rhs: some QueryExpression<QueryValue.Wrapped>
193+
) -> some QueryExpression<Bool> {
194+
BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS" : "=", rhs: rhs)
195+
}
196+
197+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
198+
@_documentation(visibility: private)
199+
public func != <QueryValue: _OptionalProtocol>(
200+
lhs: any QueryExpression<QueryValue>,
201+
rhs: some QueryExpression<QueryValue.Wrapped>
202+
) -> some QueryExpression<Bool> {
203+
BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS NOT" : "<>", rhs: rhs)
204+
}
205+
206+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
207+
@_documentation(visibility: private)
208+
public func == <QueryValue: _OptionalProtocol>(
209+
lhs: any QueryExpression<QueryValue>,
210+
rhs: some QueryExpression<QueryValue>
211+
) -> some QueryExpression<Bool> {
212+
BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS" : "=", rhs: rhs)
213+
}
214+
215+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
216+
@_documentation(visibility: private)
217+
public func != <QueryValue: _OptionalProtocol>(
218+
lhs: any QueryExpression<QueryValue>,
219+
rhs: some QueryExpression<QueryValue>
220+
) -> some QueryExpression<Bool> {
221+
BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS NOT" : "<>", rhs: rhs)
222+
}
223+
224+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
225+
@_documentation(visibility: private)
226+
public func == <QueryValue: QueryBindable>(
227+
lhs: any QueryExpression<QueryValue>,
228+
rhs: _Null<QueryValue>
229+
) -> some QueryExpression<Bool> {
230+
SQLQueryExpression(lhs).is(rhs)
231+
}
232+
233+
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
234+
@_documentation(visibility: private)
235+
public func != <QueryValue: QueryBindable>(
236+
lhs: any QueryExpression<QueryValue>,
237+
rhs: _Null<QueryValue>
238+
) -> some QueryExpression<Bool> {
239+
SQLQueryExpression(lhs).isNot(rhs)
240+
}
241+
220242
extension QueryExpression where QueryValue: QueryBindable {
221243
/// Returns a predicate expression indicating whether the value of the first expression is less
222244
/// than that of the second expression.
@@ -229,7 +251,8 @@ extension QueryExpression where QueryValue: QueryBindable {
229251
/// - rhs: Another expression to compare.
230252
/// - Returns: A predicate expression.
231253
public static func < (
232-
lhs: Self, rhs: some QueryExpression<QueryValue>
254+
lhs: Self,
255+
rhs: some QueryExpression<QueryValue>
233256
) -> some QueryExpression<Bool> {
234257
lhs.lt(rhs)
235258
}
@@ -245,7 +268,8 @@ extension QueryExpression where QueryValue: QueryBindable {
245268
/// - rhs: Another expression to compare.
246269
/// - Returns: A predicate expression.
247270
public static func > (
248-
lhs: Self, rhs: some QueryExpression<QueryValue>
271+
lhs: Self,
272+
rhs: some QueryExpression<QueryValue>
249273
) -> some QueryExpression<Bool> {
250274
lhs.gt(rhs)
251275
}
@@ -261,7 +285,8 @@ extension QueryExpression where QueryValue: QueryBindable {
261285
/// - rhs: Another expression to compare.
262286
/// - Returns: A predicate expression.
263287
public static func <= (
264-
lhs: Self, rhs: some QueryExpression<QueryValue>
288+
lhs: Self,
289+
rhs: some QueryExpression<QueryValue>
265290
) -> some QueryExpression<Bool> {
266291
lhs.lte(rhs)
267292
}
@@ -277,7 +302,8 @@ extension QueryExpression where QueryValue: QueryBindable {
277302
/// - rhs: Another expression to compare.
278303
/// - Returns: A predicate expression.
279304
public static func >= (
280-
lhs: Self, rhs: some QueryExpression<QueryValue>
305+
lhs: Self,
306+
rhs: some QueryExpression<QueryValue>
281307
) -> some QueryExpression<Bool> {
282308
lhs.gte(rhs)
283309
}
@@ -338,7 +364,8 @@ extension QueryExpression where QueryValue == Bool {
338364
/// - rhs: The right-hand side of the operation.
339365
/// - Returns: A predicate expression.
340366
public static func && (
341-
lhs: Self, rhs: some QueryExpression<QueryValue>
367+
lhs: Self,
368+
rhs: some QueryExpression<QueryValue>
342369
) -> some QueryExpression<QueryValue> {
343370
lhs.and(rhs)
344371
}
@@ -353,7 +380,8 @@ extension QueryExpression where QueryValue == Bool {
353380
/// - rhs: The right-hand side of the operation.
354381
/// - Returns: A predicate expression.
355382
public static func || (
356-
lhs: Self, rhs: some QueryExpression<QueryValue>
383+
lhs: Self,
384+
rhs: some QueryExpression<QueryValue>
357385
) -> some QueryExpression<QueryValue> {
358386
lhs.or(rhs)
359387
}
@@ -415,7 +443,8 @@ extension QueryExpression where QueryValue: Numeric {
415443
/// - rhs: The second expression to add.
416444
/// - Returns: A sum expression.
417445
public static func + (
418-
lhs: Self, rhs: some QueryExpression<QueryValue>
446+
lhs: Self,
447+
rhs: some QueryExpression<QueryValue>
419448
) -> some QueryExpression<QueryValue> {
420449
BinaryOperator(lhs: lhs, operator: "+", rhs: rhs)
421450
}
@@ -427,7 +456,8 @@ extension QueryExpression where QueryValue: Numeric {
427456
/// - rhs: The second expression to subtract.
428457
/// - Returns: A difference expression.
429458
public static func - (
430-
lhs: Self, rhs: some QueryExpression<QueryValue>
459+
lhs: Self,
460+
rhs: some QueryExpression<QueryValue>
431461
) -> some QueryExpression<QueryValue> {
432462
BinaryOperator(lhs: lhs, operator: "-", rhs: rhs)
433463
}
@@ -439,7 +469,8 @@ extension QueryExpression where QueryValue: Numeric {
439469
/// - rhs: The second expression to multiply.
440470
/// - Returns: A product expression.
441471
public static func * (
442-
lhs: Self, rhs: some QueryExpression<QueryValue>
472+
lhs: Self,
473+
rhs: some QueryExpression<QueryValue>
443474
) -> some QueryExpression<QueryValue> {
444475
BinaryOperator(lhs: lhs, operator: "*", rhs: rhs)
445476
}
@@ -451,7 +482,8 @@ extension QueryExpression where QueryValue: Numeric {
451482
/// - rhs: The second expression to divide.
452483
/// - Returns: A quotient expression.
453484
public static func / (
454-
lhs: Self, rhs: some QueryExpression<QueryValue>
485+
lhs: Self,
486+
rhs: some QueryExpression<QueryValue>
455487
) -> some QueryExpression<QueryValue> {
456488
BinaryOperator(lhs: lhs, operator: "/", rhs: rhs)
457489
}
@@ -546,7 +578,8 @@ extension QueryExpression where QueryValue: BinaryInteger {
546578
/// - rhs: The value to divide `lhs` by.
547579
/// - Returns: An expression representing the remainder, or `NULL` if `rhs` is zero.
548580
public static func % (
549-
lhs: Self, rhs: some QueryExpression<QueryValue>
581+
lhs: Self,
582+
rhs: some QueryExpression<QueryValue>
550583
) -> some QueryExpression<QueryValue?> {
551584
BinaryOperator(lhs: lhs, operator: "%", rhs: rhs)
552585
}
@@ -558,7 +591,8 @@ extension QueryExpression where QueryValue: BinaryInteger {
558591
/// - rhs: Another integer expression.
559592
/// - Returns: An expression representing a bitwise AND operation on the two given expressions.
560593
public static func & (
561-
lhs: Self, rhs: some QueryExpression<QueryValue>
594+
lhs: Self,
595+
rhs: some QueryExpression<QueryValue>
562596
) -> some QueryExpression<QueryValue> {
563597
BinaryOperator(lhs: lhs, operator: "&", rhs: rhs)
564598
}
@@ -570,7 +604,8 @@ extension QueryExpression where QueryValue: BinaryInteger {
570604
/// - rhs: Another integer expression.
571605
/// - Returns: An expression representing a bitwise OR operation on the two given expressions.
572606
public static func | (
573-
lhs: Self, rhs: some QueryExpression<QueryValue>
607+
lhs: Self,
608+
rhs: some QueryExpression<QueryValue>
574609
) -> some QueryExpression<QueryValue> {
575610
BinaryOperator(lhs: lhs, operator: "|", rhs: rhs)
576611
}
@@ -583,7 +618,8 @@ extension QueryExpression where QueryValue: BinaryInteger {
583618
/// - rhs: Another integer expression.
584619
/// - Returns: An expression representing a left bitshift operation on the two given expressions.
585620
public static func << (
586-
lhs: Self, rhs: some QueryExpression<QueryValue>
621+
lhs: Self,
622+
rhs: some QueryExpression<QueryValue>
587623
) -> some QueryExpression<QueryValue> {
588624
BinaryOperator(lhs: lhs, operator: "<<", rhs: rhs)
589625
}
@@ -596,7 +632,8 @@ extension QueryExpression where QueryValue: BinaryInteger {
596632
/// - rhs: Another integer expression.
597633
/// - Returns: An expression representing a right bitshift operation on the two given expressions.
598634
public static func >> (
599-
lhs: Self, rhs: some QueryExpression<QueryValue>
635+
lhs: Self,
636+
rhs: some QueryExpression<QueryValue>
600637
) -> some QueryExpression<QueryValue> {
601638
BinaryOperator(lhs: lhs, operator: ">>", rhs: rhs)
602639
}
@@ -647,7 +684,8 @@ extension QueryExpression where QueryValue == String {
647684
/// - rhs: The second string expression.
648685
/// - Returns: An expression concatenating the first expression with the second.
649686
public static func + (
650-
lhs: Self, rhs: some QueryExpression<QueryValue>
687+
lhs: Self,
688+
rhs: some QueryExpression<QueryValue>
651689
) -> some QueryExpression<QueryValue> {
652690
BinaryOperator(lhs: lhs, operator: "||", rhs: rhs)
653691
}
@@ -762,7 +800,8 @@ extension SQLQueryExpression<String> {
762800
/// - lhs: The column to append.
763801
/// - rhs: The appended text.
764802
public static func += (
765-
lhs: inout Self, rhs: some QueryExpression<QueryValue>
803+
lhs: inout Self,
804+
rhs: some QueryExpression<QueryValue>
766805
) {
767806
lhs = Self(lhs + rhs)
768807
}

Tests/StructuredQueriesTests/SQLMacroTests.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,6 @@ extension SnapshotTests {
173173
"""
174174
}
175175
}
176-
177-
func foo() {
178-
let searchText = "get"
179-
#sql(
180-
"""
181-
SELECT \(Reminder.columns)
182-
FROM \(Reminder.self)
183-
WHERE \(Reminder.title) COLLATE NOCASE LIKE \(searchText)
184-
""",
185-
as: Reminder.self
186-
)
187-
}
188176
}
189177
}
190178

Tests/StructuredQueriesTests/UpdateTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ extension SnapshotTests {
352352
.find(1)
353353
.update {
354354
$0.dueDate = Case()
355-
.when($0.dueDate.is(nil), then: #sql("'2018-01-29 00:08:00.000'"))
356-
.else(#sql("NULL"))
355+
.when($0.dueDate == nil, then: #sql("'2018-01-29 00:08:00.000'"))
357356
}
358357

359358
assertQuery(
@@ -362,11 +361,11 @@ extension SnapshotTests {
362361
) {
363362
"""
364363
UPDATE "reminders"
365-
SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' ELSE NULL END
364+
SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' END
366365
WHERE ("reminders"."id" = 1)
367366
RETURNING "dueDate"
368367
"""
369-
} results: {
368+
}results: {
370369
"""
371370
┌─────┐
372371
│ nil │
@@ -380,11 +379,11 @@ extension SnapshotTests {
380379
) {
381380
"""
382381
UPDATE "reminders"
383-
SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' ELSE NULL END
382+
SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' END
384383
WHERE ("reminders"."id" = 1)
385384
RETURNING "dueDate"
386385
"""
387-
} results: {
386+
}results: {
388387
"""
389388
┌────────────────────────────────┐
390389
│ Date(2018-01-29T00:08:00.000Z) │

0 commit comments

Comments
 (0)