-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathQueryConstraint.swift
887 lines (802 loc) · 36.7 KB
/
QueryConstraint.swift
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
//
// QueryConstraint.swift
// ParseSwift
//
// Created by Corey Baker on 12/9/21.
// Copyright © 2021 Parse Community. All rights reserved.
//
import Foundation
/// Used to constrain a query.
public struct QueryConstraint: Encodable, Hashable {
enum Comparator: String, CodingKey, Encodable {
case lessThan = "$lt"
case lessThanOrEqualTo = "$lte"
case greaterThan = "$gt"
case greaterThanOrEqualTo = "$gte"
case equalTo = "$eq"
case notEqualTo = "$ne"
case containedIn = "$in"
case notContainedIn = "$nin"
case containedBy = "$containedBy"
case exists = "$exists"
case select = "$select"
case dontSelect = "$dontSelect"
case all = "$all"
case regex = "$regex"
case inQuery = "$inQuery"
case notInQuery = "$notInQuery"
case nearSphere = "$nearSphere"
case or = "$or" //swiftlint:disable:this identifier_name
case and = "$and"
case nor = "$nor"
case relatedTo = "$relatedTo"
case within = "$within"
case geoWithin = "$geoWithin"
case geoIntersects = "$geoIntersects"
case maxDistance = "$maxDistance"
case centerSphere = "$centerSphere"
case box = "$box"
case polygon = "$polygon"
case point = "$point"
case text = "$text"
case search = "$search"
case term = "$term"
case regexOptions = "$options"
case relativeTime = "$relativeTime"
case score = "$score"
}
var key: String
var value: Encodable?
var comparator: Comparator?
var isNull: Bool = false
public func encode(to encoder: Encoder) throws {
if isNull {
var container = encoder.singleValueContainer()
try container.encodeNil()
} else if let value = value as? Date {
// Parse uses special case for date
try value.parseRepresentation.encode(to: encoder)
} else {
try value?.encode(to: encoder)
}
}
public static func == (lhs: QueryConstraint, rhs: QueryConstraint) -> Bool {
guard lhs.key == rhs.key,
lhs.comparator == rhs.comparator,
lhs.isNull == rhs.isNull else {
return false
}
guard let lhsValue = lhs.value,
let rhsValue = rhs.value else {
guard lhs.value == nil,
rhs.value == nil else {
return false
}
return true
}
return lhsValue.isEqual(rhsValue)
}
public func hash(into hasher: inout Hasher) {
do {
let encodedData = try ParseCoding.jsonEncoder().encode(self)
hasher.combine(encodedData)
} catch {
hasher.combine(0)
}
}
}
/**
Add a constraint that requires that a key is greater than a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func > <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: value, comparator: .greaterThan)
}
/**
Add a constraint that requires that a key is greater than or equal to a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func >= <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: value, comparator: .greaterThanOrEqualTo)
}
/**
Add a constraint that requires that a key is less than a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func < <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: value, comparator: .lessThan)
}
/**
Add a constraint that requires that a key is less than or equal to a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func <= <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: value, comparator: .lessThanOrEqualTo)
}
/**
Add a constraint that requires that a key is equal to a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
- warning: See `equalTo` for more information.
Behavior changes based on `ParseSwift.configuration.isUsingEqualQueryConstraint`
where isUsingEqualQueryConstraint == true is known not to work for LiveQuery on
Parse Servers <= 5.0.0.
*/
public func == <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
equalTo(key: key, value: value)
}
/**
Add a constraint that requires that a key is equal to a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- parameter usingEqComparator: Set to **true** to use **$eq** comparater,
allowing for multiple `QueryConstraint`'s to be used on a single **key**.
Setting to *false* may override any `QueryConstraint`'s on the same **key**.
Defaults to `ParseSwift.configuration.isUsingEqualQueryConstraint`.
- returns: The same instance of `QueryConstraint` as the receiver.
- warning: `usingEqComparator == true` is known not to work for LiveQueries
on Parse Servers <= 5.0.0.
*/
public func equalTo <T>(key: String,
value: T,
//swiftlint:disable:next line_length
usingEqComparator: Bool = ParseSwift.configuration.isUsingEqualQueryConstraint) -> QueryConstraint where T: Encodable {
if !usingEqComparator {
return QueryConstraint(key: key, value: value)
} else {
return QueryConstraint(key: key, value: value, comparator: .equalTo)
}
}
/**
Add a constraint that requires that a key is equal to a `ParseObject`.
- parameter key: The key that the value is stored in.
- parameter value: The `ParseObject` to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
- throws: An error of type `ParseError`.
- warning: See `equalTo` for more information.
Behavior changes based on `ParseSwift.configuration.isUsingEqualQueryConstraint`
where isUsingEqualQueryConstraint == true is known not to work for LiveQuery on
Parse Servers <= 5.0.0.
*/
public func == <T>(key: String, value: T) throws -> QueryConstraint where T: ParseObject {
try equalTo(key: key, value: value)
}
/**
Add a constraint that requires that a key is equal to a `ParseObject`.
- parameter key: The key that the value is stored in.
- parameter value: The `ParseObject` to compare.
- parameter usingEqComparator: Set to **true** to use **$eq** comparater,
allowing for multiple `QueryConstraint`'s to be used on a single **key**.
Setting to *false* may override any `QueryConstraint`'s on the same **key**.
Defaults to `ParseSwift.configuration.isUsingEqualQueryConstraint`.
- returns: The same instance of `QueryConstraint` as the receiver.
- throws: An error of type `ParseError`.
- warning: `usingEqComparator == true` is known not to work for LiveQueries
on Parse Servers <= 5.0.0.
*/
public func equalTo <T>(key: String,
value: T,
//swiftlint:disable:next line_length
usingEqComparator: Bool = ParseSwift.configuration.isUsingEqualQueryConstraint) throws -> QueryConstraint where T: ParseObject {
if !usingEqComparator {
return try QueryConstraint(key: key, value: value.toPointer())
} else {
return try QueryConstraint(key: key, value: value.toPointer(), comparator: .equalTo)
}
}
/**
Add a constraint that requires that a key is not equal to a value.
- parameter key: The key that the value is stored in.
- parameter value: The value to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func != <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: value, comparator: .notEqualTo)
}
/**
Add a constraint that requires that a key is not equal to a `ParseObject`.
- parameter key: The key that the value is stored in.
- parameter value: The `ParseObject` to compare.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func != <T>(key: String, value: T) throws -> QueryConstraint where T: ParseObject {
try QueryConstraint(key: key, value: value.toPointer(), comparator: .notEqualTo)
}
internal struct InQuery<T>: Encodable where T: ParseObject {
let query: Query<T>
var className: String {
return T.className
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(className, forKey: .className)
try container.encode(query.where, forKey: .where)
}
enum CodingKeys: String, CodingKey {
case `where`, className
}
}
internal struct OrAndQuery<T>: Encodable where T: ParseObject {
let query: Query<T>
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(query.where)
}
enum CodingKeys: String, CodingKey {
case `where`
}
}
internal struct QuerySelect<T>: Encodable where T: ParseObject {
let query: InQuery<T>
let key: String
}
/**
Returns a `Query` that is the `or` of the passed in queries.
- parameter queries: The list of queries to `or` together.
- returns: An instance of `QueryConstraint`'s that are the `or` of the passed in queries.
*/
public func or <T>(queries: [Query<T>]) -> QueryConstraint where T: ParseObject {
let orQueries = queries.map { OrAndQuery(query: $0) }
return QueryConstraint(key: QueryConstraint.Comparator.or.rawValue, value: orQueries)
}
/**
Returns a `Query` that is the `or` of the passed in queries.
- parameter queries: The variadic amount of queries to `or` together.
- returns: An instance of `QueryConstraint`'s that are the `or` of the passed in queries.
*/
public func or <T>(queries: Query<T>...) -> QueryConstraint where T: ParseObject {
or(queries: queries)
}
/**
Returns a `Query` that is the `nor` of the passed in queries.
- parameter queries: The list of queries to `nor` together.
- returns: An instance of `QueryConstraint`'s that are the `nor` of the passed in queries.
*/
public func nor <T>(queries: [Query<T>]) -> QueryConstraint where T: ParseObject {
let orQueries = queries.map { OrAndQuery(query: $0) }
return QueryConstraint(key: QueryConstraint.Comparator.nor.rawValue, value: orQueries)
}
/**
Returns a `Query` that is the `nor` of the passed in queries.
- parameter queries: The variadic amount of queries to `nor` together.
- returns: An instance of `QueryConstraint`'s that are the `nor` of the passed in queries.
*/
public func nor <T>(queries: Query<T>...) -> QueryConstraint where T: ParseObject {
nor(queries: queries)
}
/**
Constructs a Query that is the `and` of the passed in queries.
For example:
var compoundQueryConstraints = and(queries: [query1, query2, query3])
will create a compoundQuery that is an and of the query1, query2, and query3.
- parameter queries: The list of queries to `and` together.
- returns: An instance of `QueryConstraint`'s that are the `and` of the passed in queries.
*/
public func and <T>(queries: [Query<T>]) -> QueryConstraint where T: ParseObject {
let andQueries = queries.map { OrAndQuery(query: $0) }
return QueryConstraint(key: QueryConstraint.Comparator.and.rawValue, value: andQueries)
}
/**
Constructs a Query that is the `and` of the passed in queries.
For example:
var compoundQueryConstraints = and(queries: query1, query2, query3)
will create a compoundQuery that is an and of the query1, query2, and query3.
- parameter queries: The variadic amount of queries to `and` together.
- returns: An instance of `QueryConstraint`'s that are the `and` of the passed in queries.
*/
public func and <T>(queries: Query<T>...) -> QueryConstraint where T: ParseObject {
and(queries: queries)
}
/**
Add a constraint that requires that a key's value matches a `Query` constraint.
- warning: This only works where the key's values are `ParseObject`s or arrays of `ParseObject`s.
- parameter key: The key that the value is stored in.
- parameter query: The query the value should match.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func == <T>(key: String, value: Query<T>) -> QueryConstraint {
QueryConstraint(key: key, value: InQuery(query: value), comparator: .inQuery)
}
/**
Add a constraint that requires that a key's value do not match a `Query` constraint.
- warning: This only works where the key's values are `ParseObject`s or arrays of `ParseObject`s.
- parameter key: The key that the value is stored in.
- parameter query: The query the value should not match.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func != <T>(key: String, query: Query<T>) -> QueryConstraint {
QueryConstraint(key: key, value: InQuery(query: query), comparator: .notInQuery)
}
/**
Adds a constraint that requires that a key's value matches a value in another key
in objects returned by a sub query.
- parameter key: The key that contains the value that is being matched.
- parameter queryKey: The key in objects in the returned by the sub query whose value should match.
- parameter query: The query to run.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func matchesKeyInQuery <T>(key: String, queryKey: String, query: Query<T>) -> QueryConstraint {
let select = QuerySelect(query: InQuery(query: query), key: queryKey)
return QueryConstraint(key: key, value: select, comparator: .select)
}
/**
Adds a constraint that requires that a key's value *not* match a value in another key
in objects returned by a sub query.
- parameter key: The key that contains the value that is being excluded.
- parameter queryKey: The key in objects returned by the sub query whose value should match.
- parameter query: The query to run.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func doesNotMatchKeyInQuery <T>(key: String, queryKey: String, query: Query<T>) -> QueryConstraint {
let select = QuerySelect(query: InQuery(query: query), key: queryKey)
return QueryConstraint(key: key, value: select, comparator: .dontSelect)
}
/**
Add a constraint to the query that requires a particular key's object
to be contained in the provided array.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containedIn <T>(key: String, array: [T]) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: array, comparator: .containedIn)
}
/**
Add a constraint to the query that requires a particular key's object
to be contained in the provided array of `ParseObjects`.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containedIn <T>(key: String, array: [T]) throws -> QueryConstraint where T: ParseObject {
let pointers = try array.map { try $0.toPointer() }
return containedIn(key: key, array: pointers)
}
/**
Add a constraint to the query that requires a particular key's object
not be contained in the provided array.
- parameter key: The key to be constrained.
- parameter array: The list of values the key's object should not be.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func notContainedIn <T>(key: String, array: [T]) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: array, comparator: .notContainedIn)
}
/**
Add a constraint to the query that requires a particular key's object
not be contained in the provided array of `ParseObject` pointers.
- parameter key: The key to be constrained.
- parameter array: The list of values the key's object should not be.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func notContainedIn <T>(key: String, array: [T]) throws -> QueryConstraint where T: ParseObject {
let pointers = try array.map { try $0.toPointer() }
return notContainedIn(key: key, array: pointers)
}
/**
Add a constraint to the query that requires a particular key's array
contains every element of the provided array.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containsAll <T>(key: String, array: [T]) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: array, comparator: .all)
}
/**
Add a constraint to the query that requires a particular key's array
contains every element of the provided array of `ParseObject's.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containsAll <T>(key: String, array: [T]) throws -> QueryConstraint where T: ParseObject {
let pointers = try array.map { try $0.toPointer() }
return containsAll(key: key, array: pointers)
}
/**
Add a constraint to the query that requires a particular key's object
to be contained by the provided array. Get objects where all array elements match.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containedBy <T>(key: String, array: [T]) -> QueryConstraint where T: Encodable {
QueryConstraint(key: key, value: array, comparator: .containedBy)
}
/**
Add a constraint to the query that requires a particular key's object
to be contained by the provided array of `ParseObject`'s.
Get objects where all array elements match.
- parameter key: The key to be constrained.
- parameter array: The possible values for the key's object.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func containedBy <T>(key: String, array: [T]) throws -> QueryConstraint where T: ParseObject {
let pointers = try array.map { try $0.toPointer() }
return containedBy(key: key, array: pointers)
}
/**
Add a constraint to the query that requires a particular key's time is related to a specified time.
For example:
let queryRelative = GameScore.query(relative("createdAt" < "12 days ago"))
will create a relative query where `createdAt` is less than 12 days ago.
- parameter constraint: The key to be constrained. Should be a Date field. The value is a
reference time, e.g. "12 days ago". Currently only comparators supported are: <, <=, >, and >=.
- returns: The same instance of `QueryConstraint` as the receiver.
- warning: Requires Parse Server 2.6.5+ for MongoDB and Parse Server 5.1.0+ for PostgreSQL.
*/
public func relative(_ constraint: QueryConstraint) -> QueryConstraint {
QueryConstraint(key: constraint.key,
value: [QueryConstraint.Comparator.relativeTime.rawValue: AnyEncodable(constraint.value)],
comparator: constraint.comparator)
}
/**
Add a constraint to the query that requires a particular key's coordinates (specified via `ParseGeoPoint`)
be near a reference point. Distance is calculated based on angular distance on a sphere. Results will be sorted
by distance from reference point.
- parameter key: The key to be constrained.
- parameter geoPoint: The reference point represented as a `ParseGeoPoint`.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func near(key: String, geoPoint: ParseGeoPoint) -> QueryConstraint {
QueryConstraint(key: key, value: geoPoint, comparator: .nearSphere)
}
/**
Add a constraint to the query that requires a particular key's coordinates (specified via `ParseGeoPoint`) be near
a reference point and within the maximum distance specified (in radians). Distance is calculated based on
angular distance on a sphere. Results will be sorted by distance (nearest to farthest) from the reference point.
- parameter key: The key to be constrained.
- parameter geoPoint: The reference point as a `ParseGeoPoint`.
- parameter distance: Maximum distance in radians.
- parameter sorted: `true` if results should be sorted by distance ascending, `false` is no sorting is required.
Defaults to true.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinRadians(key: String,
geoPoint: ParseGeoPoint,
distance: Double,
sorted: Bool = true) -> [QueryConstraint] {
if sorted {
var constraints = [QueryConstraint(key: key, value: geoPoint, comparator: .nearSphere)]
constraints.append(.init(key: key, value: distance, comparator: .maxDistance))
return constraints
} else {
var constraints = [QueryConstraint(key: key, value: geoPoint, comparator: .centerSphere)]
constraints.append(.init(key: key, value: distance, comparator: .geoWithin))
return constraints
}
}
/**
Add a constraint to the query that requires a particular key's coordinates (specified via `ParseGeoPoint`)
be near a reference point and within the maximum distance specified (in miles). Distance is calculated based
on a spherical coordinate system. Results will be sorted by distance (nearest to farthest) from the reference point.
- parameter key: The key to be constrained.
- parameter geoPoint: The reference point represented as a `ParseGeoPoint`.
- parameter distance: Maximum distance in miles.
- parameter sorted: `true` if results should be sorted by distance ascending, `false` is no sorting is required.
Defaults to true.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinMiles(key: String,
geoPoint: ParseGeoPoint,
distance: Double,
sorted: Bool = true) -> [QueryConstraint] {
withinRadians(key: key,
geoPoint: geoPoint,
distance: (distance / ParseGeoPoint.earthRadiusMiles),
sorted: sorted)
}
/**
Add a constraint to the query that requires a particular key's coordinates (specified via `ParseGeoPoint`)
be near a reference point and within the maximum distance specified (in kilometers). Distance is calculated based
on a spherical coordinate system. Results will be sorted by distance (nearest to farthest) from the reference point.
- parameter key: The key to be constrained.
- parameter geoPoint: The reference point represented as a `ParseGeoPoint`.
- parameter distance: Maximum distance in kilometers.
- parameter sorted: `true` if results should be sorted by distance ascending, `false` is no sorting is required.
Defaults to true.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinKilometers(key: String,
geoPoint: ParseGeoPoint,
distance: Double,
sorted: Bool = true) -> [QueryConstraint] {
withinRadians(key: key,
geoPoint: geoPoint,
distance: (distance / ParseGeoPoint.earthRadiusKilometers),
sorted: sorted)
}
/**
Add a constraint to the query that requires a particular key's coordinates (specified via `ParseGeoPoint`) be
contained within a given rectangular geographic bounding box.
- parameter key: The key to be constrained.
- parameter fromSouthWest: The lower-left inclusive corner of the box.
- parameter toNortheast: The upper-right inclusive corner of the box.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinGeoBox(key: String, fromSouthWest southwest: ParseGeoPoint,
toNortheast northeast: ParseGeoPoint) -> QueryConstraint {
let array = [southwest, northeast]
let dictionary = [QueryConstraint.Comparator.box.rawValue: array]
return .init(key: key, value: dictionary, comparator: .within)
}
/**
Add a constraint to the query that requires a particular key's
coordinates be contained within and on the bounds of a given polygon
Supports closed and open (last point is connected to first) paths.
Polygon must have at least 3 points.
- parameter key: The key to be constrained.
- parameter points: The polygon points as an Array of `ParseGeoPoint`'s.
- warning: Requires Parse Server 2.5.0+.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinPolygon(key: String, points: [ParseGeoPoint]) -> QueryConstraint {
let polygon = points.flatMap { [[$0.latitude, $0.longitude]]}
let dictionary = [QueryConstraint.Comparator.polygon.rawValue: polygon]
return .init(key: key, value: dictionary, comparator: .geoWithin)
}
/**
Add a constraint to the query that requires a particular key's
coordinates be contained within and on the bounds of a given polygon
Supports closed and open (last point is connected to first) paths.
- parameter key: The key to be constrained.
- parameter polygon: The `ParsePolygon`.
- warning: Requires Parse Server 2.5.0+.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func withinPolygon(key: String, polygon: ParsePolygon) -> QueryConstraint {
let polygon = polygon.coordinates.flatMap { [[$0.latitude, $0.longitude]]}
let dictionary = [QueryConstraint.Comparator.polygon.rawValue: polygon]
return .init(key: key, value: dictionary, comparator: .geoWithin)
}
/**
Add a constraint to the query that requires a particular key's
coordinates contains a `ParseGeoPoint`.
- parameter key: The key of the `ParsePolygon`.
- parameter point: The `ParseGeoPoint` to check for containment.
- warning: Requires Parse Server 2.6.0+.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func polygonContains(key: String, point: ParseGeoPoint) -> QueryConstraint {
let dictionary = [QueryConstraint.Comparator.point.rawValue: point]
return .init(key: key, value: dictionary, comparator: .geoIntersects)
}
/**
Add a constraint for finding string values that contain a provided
string using Full Text Search.
- parameter key: The key to be constrained.
- parameter text: The substring that the value must contain.
- returns: The resulting `QueryConstraint`.
- note: In order to sort you must use `Query.sortByTextScore()`.
Your `ParseObject` should conform to `ParseQueryScorable` to retrieve
the weight/rank via the "score" property of your `ParseObject`.
- warning: This may be slow for large datasets. Requires Parse Server > 2.5.0.
*/
public func matchesText(key: String, text: String) -> QueryConstraint {
let dictionary = [QueryConstraint.Comparator.search.rawValue: [QueryConstraint.Comparator.term.rawValue: text]]
return .init(key: key, value: dictionary, comparator: .text)
}
/**
Options used to constrain a text search.
*/
public enum ParseTextOption: String {
/// The language that determines the list of stop words for the search and the rules for the stemmer and tokenizer.
/// Must be of type `String`.
case language = "$language"
/// A boolean flag to enable or disable case sensitive search.
case caseSensitive = "$caseSensitive"
/// A boolean flag to enable or disable diacritic sensitive search.
case diacriticSensitive = "$diacriticSensitive"
internal func buildSearch(_ text: String,
options: [Self: Encodable]) throws -> [String: Encodable] {
var dictionary: [String: Encodable] = [QueryConstraint.Comparator.term.rawValue: text]
for (key, value) in options {
switch key {
case .language:
guard (value as? String) != nil else {
throw ParseError(code: .unknownError,
message: "Text option \(key) has to be a String")
}
dictionary[key.rawValue] = value
case .caseSensitive, .diacriticSensitive:
guard (value as? Bool) != nil else {
throw ParseError(code: .unknownError,
message: "Text option \(key) has to be a Bool")
}
dictionary[key.rawValue] = value
}
}
return dictionary
}
}
/**
Add a constraint for finding string values that contain a provided
string using Full Text Search.
- parameter key: The key to be constrained.
- parameter text: The substring that the value must contain.
- parameter options: The dictionary of options to constrain the search.
The key is of type `TextOption` and must have a respective value.
- returns: The resulting `QueryConstraint`.
- note: In order to sort you must use `Query.sortByTextScore()`.
Your `ParseObject` should conform to `ParseQueryScorable` to retrieve
the weight/rank via the "score" property of your `ParseObject`.
- warning: This may be slow for large datasets. Requires Parse Server > 2.5.0.
*/
public func matchesText(key: String,
text: String,
options: [ParseTextOption: Encodable]) throws -> QueryConstraint {
let search = try ParseTextOption.language.buildSearch(text, options: options)
let dictionary = [QueryConstraint.Comparator.search.rawValue: search]
return .init(key: key, value: AnyEncodable(dictionary), comparator: .text)
}
/**
Add a regular expression constraint for finding string values that match the provided regular expression.
- warning: This may be slow for large datasets.
- parameter key: The key that the string to match is stored in.
- parameter regex: The regular expression pattern to match.
- parameter modifiers: Any of the following supported PCRE modifiers (defaults to nil):
- `i` - Case insensitive search
- `m` - Search across multiple lines of input
- returns: The resulting `QueryConstraint`.
*/
public func matchesRegex(key: String, regex: String, modifiers: String? = nil) -> QueryConstraint {
if let modifiers = modifiers {
let dictionary = [
QueryConstraint.Comparator.regex.rawValue: regex,
QueryConstraint.Comparator.regexOptions.rawValue: modifiers
]
return .init(key: key, value: dictionary)
} else {
return .init(key: key, value: regex, comparator: .regex)
}
}
private func regexStringForString(_ inputString: String) -> String {
let escapedString = inputString.replacingOccurrences(of: "\\E", with: "\\E\\\\E\\Q")
return "\\Q\(escapedString)\\E"
}
/**
Add a constraint for finding string values that contain a provided substring.
- warning: This will be slow for large datasets.
- parameter key: The key that the string to match is stored in.
- parameter substring: The substring that the value must contain.
- parameter modifiers: Any of the following supported PCRE modifiers (defaults to nil):
- `i` - Case insensitive search
- `m` - Search across multiple lines of input
- returns: The resulting `QueryConstraint`.
*/
public func containsString(key: String, substring: String, modifiers: String? = nil) -> QueryConstraint {
let regex = regexStringForString(substring)
return matchesRegex(key: key, regex: regex, modifiers: modifiers)
}
/**
Add a constraint for finding string values that start with a provided prefix.
This will use smart indexing, so it will be fast for large datasets.
- parameter key: The key that the string to match is stored in.
- parameter prefix: The substring that the value must start with.
- parameter modifiers: Any of the following supported PCRE modifiers (defaults to nil):
- `i` - Case insensitive search
- `m` - Search across multiple lines of input
- returns: The resulting `QueryConstraint`.
*/
public func hasPrefix(key: String, prefix: String, modifiers: String? = nil) -> QueryConstraint {
let regex = "^\(regexStringForString(prefix))"
return matchesRegex(key: key, regex: regex, modifiers: modifiers)
}
/**
Add a constraint for finding string values that end with a provided suffix.
- warning: This will be slow for large datasets.
- parameter key: The key that the string to match is stored in.
- parameter suffix: The substring that the value must end with.
- parameter modifiers: Any of the following supported PCRE modifiers (defaults to nil):
- `i` - Case insensitive search
- `m` - Search across multiple lines of input
- returns: The resulting `QueryConstraint`.
*/
public func hasSuffix(key: String, suffix: String, modifiers: String? = nil) -> QueryConstraint {
let regex = "\(regexStringForString(suffix))$"
return matchesRegex(key: key, regex: regex, modifiers: modifiers)
}
/**
Add a constraint that requires that a key is equal to **null** or **undefined**.
- parameter key: The key that the value is stored in.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func isNull (key: String) -> QueryConstraint {
QueryConstraint(key: key, isNull: true)
}
/**
Add a constraint that requires that a key is not equal to **null** or **undefined**.
- parameter key: The key that the value is stored in.
- returns: The same instance of `QueryConstraint` as the receiver.
*/
public func isNotNull (key: String) -> QueryConstraint {
QueryConstraint(key: key, comparator: .notEqualTo, isNull: true)
}
/**
Add a constraint that requires a particular key to be equal to **undefined**.
- parameter key: The key that should exist.
- returns: The resulting `QueryConstraint`.
*/
public func exists(key: String) -> QueryConstraint {
.init(key: key, value: true, comparator: .exists)
}
/**
Add a constraint that requires a key to not be equal to **undefined**.
- parameter key: The key that should not exist.
- returns: The resulting `QueryConstraint`.
*/
public func doesNotExist(key: String) -> QueryConstraint {
.init(key: key, value: false, comparator: .exists)
}
internal struct RelatedKeyCondition: Encodable {
let key: String
}
internal struct RelatedObjectCondition <T>: Encodable where T: ParseObject {
let object: Pointer<T>
}
internal struct RelatedCondition <T>: Encodable where T: ParseObject {
let object: Pointer<T>
let key: String
}
/**
Add a constraint that requires a key is related.
- parameter key: The key that should be related.
- parameter object: The object that should be related.
- returns: The resulting `QueryConstraint`.
- throws: An error of type `ParseError`.
*/
public func related <T>(key: String, object: T) throws -> QueryConstraint where T: ParseObject {
let pointer = try object.toPointer()
let condition = RelatedCondition(object: pointer, key: key)
return .init(key: QueryConstraint.Comparator.relatedTo.rawValue, value: condition)
}
/**
Add a constraint that requires a key is related.
- parameter key: The key that should be related.
- parameter object: The pointer object that should be related.
- returns: The resulting `QueryConstraint`.
*/
public func related <T>(key: String, object: Pointer<T>) -> QueryConstraint where T: ParseObject {
let condition = RelatedCondition(object: object, key: key)
return .init(key: QueryConstraint.Comparator.relatedTo.rawValue, value: condition)
}
/**
Add a constraint that requires a key is related.
- parameter key: The key that should be related.
- returns: The resulting `QueryConstraint`.
- throws: An error of type `ParseError`.
*/
public func related(key: String) -> QueryConstraint {
let condition = RelatedKeyCondition(key: key)
return .init(key: QueryConstraint.Comparator.relatedTo.rawValue, value: condition)
}
/**
Add a constraint that requires a key is related.
- parameter object: The object that should be related.
- returns: The resulting `QueryConstraint`.
- throws: An error of type `ParseError`.
*/
public func related <T>(object: T) throws -> QueryConstraint where T: ParseObject {
let pointer = try object.toPointer()
let condition = RelatedObjectCondition(object: pointer)
return .init(key: QueryConstraint.Comparator.relatedTo.rawValue, value: condition)
}
/**
Add a constraint that requires a key is related.
- parameter object: The pointer object that should be related.
- returns: The resulting `QueryConstraint`.
*/
public func related <T>(object: Pointer<T>) -> QueryConstraint where T: ParseObject {
let condition = RelatedObjectCondition(object: object)
return .init(key: QueryConstraint.Comparator.relatedTo.rawValue, value: condition)
}