-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathOperators.swift
More file actions
901 lines (813 loc) · 30.4 KB
/
Copy pathOperators.swift
File metadata and controls
901 lines (813 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
extension QueryExpression where QueryValue: QueryRepresentable {
/// Returns a predicate expression indicating whether two query expressions are equal.
///
/// ```swift
/// Reminder.where { $0.title.eq("Buy milk") }
/// // SELECT … FROM "reminders" WHERE "reminders"."title" = 'Buy milk'
/// ```
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func eq(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "=", rhs: other)
}
/// Returns a predicate expression indicating whether two query expressions are not equal.
///
/// ```swift
/// Reminder.where { $0.title.neq("Buy milk") }
/// // SELECT … FROM "reminders" WHERE "reminders"."title" <> 'Buy milk'
/// ```
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func neq(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "<>", rhs: other)
}
/// Returns a predicate expression indicating whether two query expressions are equal (or are
/// equal to `NULL`).
///
/// ```swift
/// Reminder.where { $0.priority.is(nil) }
/// // SELECT … FROM "reminders" WHERE "reminders"."priority" IS NULL
/// ```
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func `is`<Other: QueryRepresentable>(
_ other: some QueryExpression<Other>
) -> some QueryExpression<Bool>
where QueryValue._Optionalized.Wrapped == Other._Optionalized.Wrapped {
BinaryOperator(lhs: self, operator: "IS", rhs: other)
}
/// Returns a predicate expression indicating whether two query expressions are not equal (or are
/// not equal to `NULL`).
///
/// ```swift
/// Reminder.where { $0.priority.isNot(nil) }
/// // SELECT … FROM "reminders" WHERE "reminders"."priority" IS NOT NULL
/// ```
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func isNot<Other: QueryRepresentable>(
_ other: some QueryExpression<QueryValue._Optionalized>
) -> some QueryExpression<Bool>
where QueryValue._Optionalized.Wrapped == Other._Optionalized.Wrapped {
BinaryOperator(lhs: self, operator: "IS NOT", rhs: other)
}
@available(*, unavailable, message: "Use 'eq' (or 'is') instead.")
public static func == (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: lhs, operator: "=", rhs: rhs)
}
@available(*, unavailable, message: "Use 'eq' (or 'is') instead.")
public static func == (
lhs: Self,
rhs: some QueryExpression<QueryValue?>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: lhs, operator: "=", rhs: rhs)
}
@available(*, unavailable, message: "Use 'eq' (or 'is') instead.")
public static func == (
lhs: Self,
rhs: some QueryExpression<QueryValue.Wrapped>
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "=", rhs: rhs)
}
@available(*, deprecated, message: "Use 'is' instead.")
public static func == (
lhs: Self,
rhs: _Null<QueryValue>
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "IS", rhs: rhs)
}
@available(*, deprecated, message: "Use 'is' instead.")
public static func == (
lhs: _Null<QueryValue>,
rhs: Self
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "IS", rhs: rhs)
}
@available(
*,
unavailable,
message: "Use 'neq' or 'isNot' instead."
)
public static func != (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: lhs, operator: "<>", rhs: rhs)
}
@available(
*,
unavailable,
message: "Use 'neq' or 'isNot' instead."
)
public static func != (
lhs: Self,
rhs: some QueryExpression<QueryValue?>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: lhs, operator: "<>", rhs: rhs)
}
@available(
*,
unavailable,
message: "Use 'neq' or 'isNot' instead."
)
public static func != (
lhs: Self,
rhs: some QueryExpression<QueryValue.Wrapped>
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "<>", rhs: rhs)
}
@available(*, deprecated, message: "Use 'isNot' instead.")
public static func != (
lhs: Self,
rhs: _Null<QueryValue>
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "<>", rhs: rhs)
}
@available(*, deprecated, message: "Use 'isNot' instead.")
public static func != (
lhs: _Null<QueryValue>,
rhs: Self
) -> some QueryExpression<Bool> where QueryValue: _OptionalProtocol {
BinaryOperator(lhs: lhs, operator: "<>", rhs: rhs)
}
}
extension QueryExpression where QueryValue: QueryRepresentable & QueryExpression {
@_documentation(visibility: private)
public func `is`(
_ other: _Null<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "IS", rhs: other)
}
@_documentation(visibility: private)
public func isNot(
_ other: _Null<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "IS NOT", rhs: other)
}
}
public struct _Null<Wrapped: QueryExpression>: QueryExpression {
public typealias QueryValue = Wrapped?
public var queryFragment: QueryFragment {
Wrapped?.none.queryFragment
}
}
extension _Null: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {}
}
// TODO: Remove this when we correctly unwrap `TableColumns` in `join` conditions.
extension QueryExpression where QueryValue: QueryRepresentable & _OptionalProtocol {
@_documentation(visibility: private)
public func eq(_ other: some QueryExpression<QueryValue.Wrapped>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "=", rhs: other)
}
@_documentation(visibility: private)
public func neq(_ other: some QueryExpression<QueryValue.Wrapped>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "<>", rhs: other)
}
@_documentation(visibility: private)
public func eq(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "=", rhs: other)
}
@_documentation(visibility: private)
public func neq(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "<>", rhs: other)
}
@_documentation(visibility: private)
public func `is`(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "IS", rhs: other)
}
@_documentation(visibility: private)
public func isNot(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "IS NOT", rhs: other)
}
}
extension QueryExpression where QueryValue: _OptionalPromotable {
/// Returns a predicate expression indicating whether the value of the first expression is less
/// than that of the second expression.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``lt(_:)``, instead.
///
/// - Parameters:
/// - lhs: An expression to compare.
/// - rhs: Another expression to compare.
/// - Returns: A predicate expression.
public static func < (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
lhs.lt(rhs)
}
/// Returns a predicate expression indicating whether the value of the first expression is greater
/// than that of the second expression.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``gt(_:)``, instead.
///
/// - Parameters:
/// - lhs: An expression to compare.
/// - rhs: Another expression to compare.
/// - Returns: A predicate expression.
public static func > (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
lhs.gt(rhs)
}
/// Returns a predicate expression indicating whether the value of the first expression is less
/// than or equal to that of the second expression.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``lte(_:)``, instead.
///
/// - Parameters:
/// - lhs: An expression to compare.
/// - rhs: Another expression to compare.
/// - Returns: A predicate expression.
public static func <= (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
lhs.lte(rhs)
}
/// Returns a predicate expression indicating whether the value of the first expression is greater
/// than or equal to that of the second expression.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``gte(_:)``, instead.
///
/// - Parameters:
/// - lhs: An expression to compare.
/// - rhs: Another expression to compare.
/// - Returns: A predicate expression.
public static func >= (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
lhs.gte(rhs)
}
/// Returns a predicate expression indicating whether the value of the first expression is less
/// than that of the second expression.
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func lt(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "<", rhs: other)
}
/// Returns a predicate expression indicating whether the value of the first expression is greater
/// than that of the second expression.
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func gt(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: ">", rhs: other)
}
/// Returns a predicate expression indicating whether the value of the first expression is less
/// than or equal to that of the second expression.
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func lte(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "<=", rhs: other)
}
/// Returns a predicate expression indicating whether the value of the first expression is greater
/// than or equal to that of the second expression.
///
/// - Parameter other: An expression to compare this one to.
/// - Returns: A predicate expression.
public func gte(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: ">=", rhs: other)
}
}
extension QueryExpression where QueryValue == Bool {
/// Returns a logical AND operation on two predicate expressions.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``and(_:)``, instead.
///
/// - Parameters:
/// - lhs: The left-hand side of the operation.
/// - rhs: The right-hand side of the operation.
/// - Returns: A predicate expression.
public static func && (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
lhs.and(rhs)
}
/// Returns a logical OR operation on two predicate expressions.
///
/// > Important: Overloaded operators can strain the Swift compiler's type checking ability.
/// > Consider using ``or(_:)``, instead.
///
/// - Parameters:
/// - lhs: The left-hand side of the operation.
/// - rhs: The right-hand side of the operation.
/// - Returns: A predicate expression.
public static func || (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
lhs.or(rhs)
}
/// Returns a logical NOT operation on a predicate expression.
///
/// - Parameter expression: The predicate expression to negate.
/// - Returns: A negated predicate expression.
public static prefix func ! (expression: Self) -> some QueryExpression<QueryValue> {
expression.not()
}
/// Returns a logical AND operation on two predicate expressions.
///
/// - Parameter other: The right-hand side of the operation to this predicate's left-hand side.
/// - Returns: A predicate expression.
public func and(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: self, operator: "AND", rhs: other)
}
/// Returns a logical OR operation on two predicate expressions.
///
/// - Parameter other: The right-hand side of the operation to this predicate's left-hand side.
/// - Returns: A predicate expression.
public func or(_ other: some QueryExpression<QueryValue>) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: self, operator: "OR", rhs: other)
}
/// Returns a logical NOT operation on this predicate expression.
///
/// - Returns: This predicate expression, negated.
public func not() -> some QueryExpression<QueryValue> {
UnaryOperator(operator: "NOT", base: self)
}
}
extension SQLQueryExpression<Bool> {
public mutating func toggle() {
self = Self(not())
}
}
extension QueryExpression where QueryValue: Numeric {
/// Returns a sum expression that adds two expressions.
///
/// - Parameters:
/// - lhs: The first expression to add.
/// - rhs: The second expression to add.
/// - Returns: A sum expression.
public static func + (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "+", rhs: rhs)
}
/// Returns a difference expression that subtracts two expressions.
///
/// - Parameters:
/// - lhs: The first expression to subtract.
/// - rhs: The second expression to subtract.
/// - Returns: A difference expression.
public static func - (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "-", rhs: rhs)
}
/// Returns a product expression that multiplies two expressions.
///
/// - Parameters:
/// - lhs: The first expression to multiply.
/// - rhs: The second expression to multiply.
/// - Returns: A product expression.
public static func * (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "*", rhs: rhs)
}
/// Returns a quotient expression that divides two expressions.
///
/// - Parameters:
/// - lhs: The first expression to divide.
/// - rhs: The second expression to divide.
/// - Returns: A quotient expression.
public static func / (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "/", rhs: rhs)
}
/// Returns the additive inverse of the specified expression.
///
/// - Parameter expression: A numeric expression.
/// - Returns: the additive inverse of this expression
public static prefix func - (expression: Self) -> some QueryExpression<QueryValue> {
UnaryOperator(operator: "-", base: expression, separator: "")
}
/// Returns the additive equivalent to the specified expression.
///
/// - Parameter expression: A numeric expression.
/// - Returns: the additive equivalent to this expression
public static prefix func + (expression: Self) -> some QueryExpression<QueryValue> {
UnaryOperator(operator: "+", base: expression, separator: "")
}
}
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
@_documentation(visibility: private)
public prefix func - <QueryValue: Numeric>(
expression: any QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
func open(_ expression: some QueryExpression<QueryValue>) -> SQLQueryExpression<QueryValue> {
SQLQueryExpression(UnaryOperator(operator: "-", base: expression, separator: ""))
}
return open(expression)
}
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
@_documentation(visibility: private)
public prefix func + <QueryValue: Numeric>(
expression: any QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
func open(_ expression: some QueryExpression<QueryValue>) -> SQLQueryExpression<QueryValue> {
SQLQueryExpression(UnaryOperator(operator: "+", base: expression, separator: ""))
}
return open(expression)
}
extension SQLQueryExpression where QueryValue: Numeric {
/// Adds to a numeric expression in an update clause.
///
/// - Parameters:
/// - lhs: The column to add to.
/// - rhs: The expression to add.
public static func += (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs + rhs)
}
/// Subtracts from a numeric expression in an update clause.
///
/// - Parameters:
/// - lhs: The column to subtract from.
/// - rhs: The expression to subtract.
public static func -= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs - rhs)
}
/// Multiplies a numeric expression in an update clause.
///
/// - Parameters:
/// - lhs: The column to multiply.
/// - rhs: The expression multiplier.
public static func *= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs * rhs)
}
/// Divides a numeric expression in an update clause.
///
/// - Parameters:
/// - lhs: The column to divide.
/// - rhs: The expression divisor.
public static func /= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs / rhs)
}
/// Negates a numeric expression in an update clause.
public mutating func negate() {
self = Self(-self)
}
}
extension QueryExpression where QueryValue: BinaryInteger {
/// Returns the remainder expression of dividing the first expression by the second.
///
/// - Parameters:
/// - lhs: The expression to divide.
/// - rhs: The value to divide `lhs` by.
/// - Returns: An expression representing the remainder, or `NULL` if `rhs` is zero.
public static func % (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue?> {
BinaryOperator(lhs: lhs, operator: "%", rhs: rhs)
}
/// Returns the expression of performing a bitwise AND operation on the two given expressions.
///
/// - Parameters:
/// - lhs: An integer expression.
/// - rhs: Another integer expression.
/// - Returns: An expression representing a bitwise AND operation on the two given expressions.
public static func & (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "&", rhs: rhs)
}
/// Returns the expression of performing a bitwise OR operation on the two given expressions.
///
/// - Parameters:
/// - lhs: An integer expression.
/// - rhs: Another integer expression.
/// - Returns: An expression representing a bitwise OR operation on the two given expressions.
public static func | (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "|", rhs: rhs)
}
/// Returns an expression representing the result of shifting an expression's binary
/// representation the specified expression of digits to the left.
///
/// - Parameters:
/// - lhs: An integer expression.
/// - rhs: Another integer expression.
/// - Returns: An expression representing a left bitshift operation on the two given expressions.
public static func << (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "<<", rhs: rhs)
}
/// Returns an expression representing the result of shifting an expression's binary
/// representation the specified expression of digits to the right.
///
/// - Parameters:
/// - lhs: An integer expression.
/// - rhs: Another integer expression.
/// - Returns: An expression representing a right bitshift operation on the two given expressions.
public static func >> (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: ">>", rhs: rhs)
}
/// Returns the inverse expression of the bits set in the argument.
///
/// - Parameter expression: An integer expression.
/// - Returns: An expression representing the inverse bits of the given expression.
public static prefix func ~ (expression: Self) -> some QueryExpression<QueryValue> {
UnaryOperator(operator: "~", base: expression, separator: "")
}
}
// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'.
@_documentation(visibility: private)
public prefix func ~ <QueryValue: BinaryInteger>(
expression: any QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
func open(_ expression: some QueryExpression<QueryValue>) -> SQLQueryExpression<QueryValue> {
SQLQueryExpression(UnaryOperator(operator: "~", base: expression, separator: ""))
}
return open(expression)
}
extension SQLQueryExpression where QueryValue: BinaryInteger {
public static func &= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs & rhs)
}
public static func |= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs | rhs)
}
public static func <<= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs << rhs)
}
public static func >>= (lhs: inout Self, rhs: some QueryExpression<QueryValue>) {
lhs = Self(lhs >> rhs)
}
}
extension QueryExpression where QueryValue == String {
/// Returns an expression that concatenates two string expressions.
///
/// - Parameters:
/// - lhs: The first string expression.
/// - rhs: The second string expression.
/// - Returns: An expression concatenating the first expression with the second.
public static func + (
lhs: Self,
rhs: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue> {
BinaryOperator(lhs: lhs, operator: "||", rhs: rhs)
}
/// Returns an expression of this expression that is compared using the given collating sequence.
///
/// - Parameter collation: A collating sequence name.
/// - Returns: An expression that is compared using the given collating sequence.
public func collate(_ collation: Collation) -> some QueryExpression<QueryValue> {
SQLQueryExpression("\(self) COLLATE \(collation)")
}
/// A predicate expression from this string expression matched against another _via_ the `GLOB`
/// operator.
///
/// ```swift
/// Asset.where { $0.path.glob("Resources/*.png") }
/// // SELECT … FROM "assets" WHERE ("assets"."path" GLOB 'Resources/*.png')
/// ```
///
/// - Parameter pattern: A string expression describing the `GLOB` pattern.
/// - Returns: A predicate expression.
public func glob(_ pattern: some StringProtocol) -> some QueryExpression<Bool> {
BinaryOperator(lhs: self, operator: "GLOB", rhs: "\(pattern)")
}
/// A predicate expression from this string expression matched against another _via_ the `LIKE`
/// operator.
///
/// ```swift
/// Reminder.where { $0.title.like("%get%") }
/// // SELECT … FROM "reminders" WHERE ("reminders"."title" LIKE '%get%')
/// ```
///
/// - Parameters
/// - pattern: A string expression describing the `LIKE` pattern.
/// - escape: An optional character for the `ESCAPE` clause.
/// - Returns: A predicate expression.
public func like(
_ pattern: some StringProtocol,
escape: Character? = nil
) -> some QueryExpression<Bool> {
LikeOperator(string: self, pattern: "\(pattern)", escape: escape)
}
}
extension SQLQueryExpression<String> {
/// Appends a string expression in an update clause.
///
/// Can be used in an `UPDATE` clause to append an existing column:
///
/// ```swift
/// Reminder.update { $0.title += " 2" }
/// // UPDATE "reminders" SET "title" = ("reminders"."title" || ` 2`)
/// ```
///
/// - Parameters:
/// - lhs: The column to append.
/// - rhs: The appended text.
public static func += (
lhs: inout Self,
rhs: some QueryExpression<QueryValue>
) {
lhs = Self(lhs + rhs)
}
/// Appends this string expression in an update clause.
///
/// An alias for ``+=(_:_:)``.
///
/// - Parameters other: The text to append.
public mutating func append(_ other: some QueryExpression<QueryValue>) {
self += other
}
/// Appends this string expression in an update clause.
///
/// An alias for ``+=(_:_:)``.
///
/// - Parameters other: The text to append.
public mutating func append(contentsOf other: some QueryExpression<QueryValue>) {
self += other
}
}
extension QueryExpression where QueryValue: QueryExpression {
/// Returns a predicate expression indicating whether the expression is in a sequence.
///
/// - Parameter expression: A sequence of expressions.
/// - Returns: A predicate expression indicating whether this expression is in the given sequence.
public func `in`<S: Sequence>(_ expression: S) -> some QueryExpression<Bool>
where S.Element: QueryExpression<QueryValue> {
BinaryOperator(lhs: self, operator: "IN", rhs: S.Expression(elements: expression))
}
/// Returns a predicate expression indicating whether the expression is not in a sequence.
///
/// - Parameter expression: A sequence of expressions.
/// - Returns: A predicate expression indicating whether this expression is not in the given
/// sequence.
public func notIn<S: Sequence>(_ expression: S) -> some QueryExpression<Bool>
where S.Element: QueryExpression<QueryValue> {
BinaryOperator(lhs: self, operator: "NOT IN", rhs: S.Expression(elements: expression))
}
/// Returns a predicate expression indicating whether the expression is in a subquery.
///
/// - Parameter query: A subquery.
/// - Returns: A predicate expression indicating whether this expression is in the given subquery.
public func `in`(_ query: some Statement<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(
lhs: self,
operator: "IN",
rhs: SQLQueryExpression("(\(query.query))", as: Void.self)
)
}
/// Returns a predicate expression indicating whether the expression is not in a subquery.
///
/// - Parameter query: A subquery.
/// - Returns: A predicate expression indicating whether this expression is not in the given
/// subquery.
public func notIn(_ query: some Statement<QueryValue>) -> some QueryExpression<Bool> {
BinaryOperator(
lhs: self,
operator: "NOT IN",
rhs: SQLQueryExpression("(\(query.query))", as: Void.self)
)
}
/// Returns a predicate expression indicating whether the expression is between a lower and upper
/// bound.
///
/// - Parameters:
/// - lowerBound: An expression representing the lower bound.
/// - upperBound: An expression representing the upper bound.
/// - Returns: A predicate expression indicating whether this expression is between the given
/// bounds.
public func between(
_ lowerBound: some QueryExpression<QueryValue>,
and upperBound: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
SQLQueryExpression("\(self) BETWEEN \(lowerBound) AND \(upperBound)")
}
}
extension Statement where QueryValue: QueryBindable {
/// Returns a predicate expression indicating whether this subquery contains the given element.
///
/// An alias for ``QueryExpression/in(_:)``, flipped.
///
/// - Parameter element: An element.
/// - Returns: A predicate expression indicating whether this expression is in the given subquery.
@available(*, deprecated, message: "Prefer 'element.in(self)' instead")
public func contains(
_ element: some QueryExpression<QueryValue>
) -> some QueryExpression<Bool> {
element.in(self)
}
}
extension PartialSelectStatement {
/// Returns a predicate expression indicating whether this subquery contains any element.
///
/// - Returns: A predicate expression indicating whether this subquery contains any element.
public func exists() -> some QueryExpression<Bool> {
SQLQueryExpression("EXISTS \(self.queryFragment)")
}
}
extension Table {
/// Returns a predicate expression indicating whether this table contains any element.
///
/// - Returns: A predicate expression indicating whether this subquery contains any element.
public static func exists() -> some QueryExpression<Bool> {
all.exists()
}
}
private struct UnaryOperator<QueryValue>: QueryExpression {
let `operator`: QueryFragment
let base: QueryFragment
let separator: QueryFragment
init(operator: QueryFragment, base: some QueryExpression, separator: QueryFragment = " ") {
self.operator = `operator`
self.base = base.queryFragment
self.separator = separator
}
var queryFragment: QueryFragment {
"\(`operator`)\(separator)(\(base))"
}
}
struct BinaryOperator<QueryValue>: QueryExpression {
let lhs: QueryFragment
let `operator`: QueryFragment
let rhs: QueryFragment
init(
lhs: some QueryExpression,
operator: QueryFragment,
rhs: some QueryExpression
) {
self.lhs = lhs.queryFragment
self.operator = `operator`
self.rhs = rhs.queryFragment
}
var queryFragment: QueryFragment {
"(\(lhs)) \(`operator`) (\(rhs))"
}
}
private struct LikeOperator<
LHS: QueryExpression<String>,
RHS: QueryExpression<String>
>: QueryExpression {
typealias QueryValue = Bool
let string: LHS
let pattern: RHS
let escape: Character?
var queryFragment: QueryFragment {
var query: QueryFragment = "(\(string.queryFragment) LIKE \(pattern.queryFragment)"
if let escape {
query.append(" ESCAPE \(bind: String(escape))")
}
query.append(")")
return query
}
}
extension Sequence where Element: QueryExpression, Element.QueryValue: QueryExpression {
fileprivate typealias Expression = _SequenceExpression<Self>
}
private struct _SequenceExpression<S: Sequence>: QueryExpression
where S.Element: QueryExpression, S.Element.QueryValue: QueryExpression {
typealias QueryValue = S
let queryFragment: QueryFragment
init(elements: S) {
queryFragment = elements.map { "(\($0.queryFragment))" }.joined(separator: ", ")
}
}