-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathQuery.swift
More file actions
556 lines (487 loc) · 16.6 KB
/
Copy pathQuery.swift
File metadata and controls
556 lines (487 loc) · 16.6 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
import Foundation
enum QueryValue: Codable {
case string(String)
case int(Int)
case double(Double)
case bool(Bool)
case query(Query)
case array([QueryValue]) // for nested arrays
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let stringValue = try? container.decode(String.self) {
self = .string(stringValue)
} else if let intValue = try? container.decode(Int.self) {
self = .int(intValue)
} else if let doubleValue = try? container.decode(Double.self) {
self = .double(doubleValue)
} else if let boolValue = try? container.decode(Bool.self) {
self = .bool(boolValue)
} else if let queryValue = try? container.decode(Query.self) {
self = .query(queryValue)
} else if let arrayValue = try? container.decode([QueryValue].self) {
self = .array(arrayValue)
} else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "QueryValue cannot be decoded"
)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .string(let value):
try container.encode(value)
case .int(let value):
try container.encode(value)
case .double(let value):
try container.encode(value)
case .bool(let value):
try container.encode(value)
case .query(let value):
try container.encode(value)
case .array(let value):
try container.encode(value)
}
}
}
public struct Query : Codable, CustomStringConvertible {
var method: String
var attribute: String?
var values: [QueryValue]?
init(method: String, attribute: String? = nil, values: Any? = nil) {
self.method = method
self.attribute = attribute
self.values = Query.convertToQueryValueArray(values)
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.method = try container.decode(String.self, forKey: .method)
self.attribute = try container.decodeIfPresent(String.self, forKey: .attribute)
self.values = try container.decodeIfPresent([QueryValue].self, forKey: .values)
}
private static func convertToQueryValueArray(_ values: Any?) -> [QueryValue]? {
switch values {
case let valueArray as [QueryValue]:
return valueArray
case let stringArray as [String]:
return stringArray.map { .string($0) }
case let intArray as [Int]:
return intArray.map { .int($0) }
case let doubleArray as [Double]:
return doubleArray.map { .double($0) }
case let boolArray as [Bool]:
return boolArray.map { .bool($0) }
case let queryArray as [Query]:
return queryArray.map { .query($0) }
case let stringValue as String:
return [.string(stringValue)]
case let intValue as Int:
return [.int(intValue)]
case let doubleValue as Double:
return [.double(doubleValue)]
case let boolValue as Bool:
return [.bool(boolValue)]
case let queryValue as Query:
return [.query(queryValue)]
case let anyArray as [Any]:
// Handle nested arrays
let nestedValues = anyArray.compactMap { item -> QueryValue? in
if let stringValue = item as? String {
return .string(stringValue)
} else if let intValue = item as? Int {
return .int(intValue)
} else if let doubleValue = item as? Double {
return .double(doubleValue)
} else if let boolValue = item as? Bool {
return .bool(boolValue)
} else if let queryValue = item as? Query {
return .query(queryValue)
} else if let nestedArray = item as? [Any] {
// Convert nested array to QueryValue.array
if let converted = convertToQueryValueArray(nestedArray) {
return .array(converted)
}
return nil
}
return nil
}
return nestedValues.isEmpty ? nil : nestedValues
default:
return nil
}
}
enum CodingKeys: String, CodingKey {
case method
case attribute
case values
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(method, forKey: .method)
if (self.attribute != nil) {
try container.encode(attribute, forKey: .attribute)
}
if (values != nil) {
try container.encode(values, forKey: .values)
}
}
public var description: String {
guard let data = try? JSONEncoder().encode(self) else {
return ""
}
return String(data: data, encoding: .utf8) ?? ""
}
public static func equal(_ attribute: String, value: Any) -> String {
return Query(
method: "equal",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func notEqual(_ attribute: String, value: Any) -> String {
return Query(
method: "notEqual",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func lessThan(_ attribute: String, value: Any) -> String {
return Query(
method: "lessThan",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func lessThanEqual(attribute: String, value: Any) -> String {
return Query(
method: "lessThanEqual",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func greaterThan(_ attribute: String, value: Any) -> String {
return Query(
method: "greaterThan",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func greaterThanEqual(_ attribute: String, value: Any) -> String {
return Query(
method: "greaterThanEqual",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func isNull(_ attribute: String) -> String {
return Query(
method: "isNull",
attribute: attribute
).description
}
public static func isNotNull(_ attribute: String) -> String {
return Query(
method: "isNotNull",
attribute: attribute
).description
}
public static func between(_ attribute: String, start: Int, end: Int) -> String {
return Query(
method: "between",
attribute: attribute,
values: [start, end]
).description
}
public static func between(_ attribute: String, start: Double, end: Double) -> String {
return Query(
method: "between",
attribute: attribute,
values: [start, end]
).description
}
public static func between(_ attribute: String, start: String, end: String) -> String {
return Query(
method: "between",
attribute: attribute,
values: [start, end]
).description
}
public static func startsWith(_ attribute: String, value: String) -> String {
return Query(
method: "startsWith",
attribute: attribute,
values: [value]
).description
}
public static func endsWith(_ attribute: String, value: String) -> String {
return Query(
method: "endsWith",
attribute: attribute,
values: [value]
).description
}
public static func select(_ attributes: [String]) -> String {
return Query(
method: "select",
values: attributes
).description
}
public static func search(_ attribute: String, value: String) -> String {
return Query(
method: "search",
attribute: attribute,
values: [value]
).description
}
public static func orderAsc(_ attribute: String) -> String {
return Query(
method: "orderAsc",
attribute: attribute
).description
}
public static func orderDesc(_ attribute: String) -> String {
return Query(
method: "orderDesc",
attribute: attribute
).description
}
public static func orderRandom() -> String {
return Query(
method: "orderRandom"
).description
}
public static func cursorBefore(_ id: String) -> String {
return Query(
method: "cursorBefore",
values: [id]
).description
}
public static func cursorAfter(_ id: String) -> String {
return Query(
method: "cursorAfter",
values: [id]
).description
}
public static func limit(_ limit: Int) -> String {
return Query(
method: "limit",
values: [limit]
).description
}
public static func offset(_ offset: Int) -> String {
return Query(
method: "offset",
values: [offset]
).description
}
public static func contains(_ attribute: String, value: Any) -> String {
return Query(
method: "contains",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func notContains(_ attribute: String, value: Any) -> String {
return Query(
method: "notContains",
attribute: attribute,
values: Query.parseValue(value)
).description
}
public static func notSearch(_ attribute: String, value: String) -> String {
return Query(
method: "notSearch",
attribute: attribute,
values: [value]
).description
}
public static func notBetween(_ attribute: String, start: Int, end: Int) -> String {
return Query(
method: "notBetween",
attribute: attribute,
values: [start, end]
).description
}
public static func notBetween(_ attribute: String, start: Double, end: Double) -> String {
return Query(
method: "notBetween",
attribute: attribute,
values: [start, end]
).description
}
public static func notBetween(_ attribute: String, start: String, end: String) -> String {
return Query(
method: "notBetween",
attribute: attribute,
values: [start, end]
).description
}
public static func notStartsWith(_ attribute: String, value: String) -> String {
return Query(
method: "notStartsWith",
attribute: attribute,
values: [value]
).description
}
public static func notEndsWith(_ attribute: String, value: String) -> String {
return Query(
method: "notEndsWith",
attribute: attribute,
values: [value]
).description
}
public static func createdBefore(_ value: String) -> String {
return Query(
method: "createdBefore",
values: [value]
).description
}
public static func createdAfter(_ value: String) -> String {
return Query(
method: "createdAfter",
values: [value]
).description
}
public static func createdBetween(_ start: String, _ end: String) -> String {
return Query(
method: "createdBetween",
values: [start, end]
).description
}
public static func updatedBefore(_ value: String) -> String {
return Query(
method: "updatedBefore",
values: [value]
).description
}
public static func updatedAfter(_ value: String) -> String {
return Query(
method: "updatedAfter",
values: [value]
).description
}
public static func updatedBetween(_ start: String, _ end: String) -> String {
return Query(
method: "updatedBetween",
values: [start, end]
).description
}
public static func or(_ queries: [String]) -> String {
let decoder = JSONDecoder()
let decodedQueries = queries.compactMap { queryStr -> Query? in
guard let data = queryStr.data(using: .utf8) else {
return nil
}
return try? decoder.decode(Query.self, from: data)
}
return Query(
method: "or",
values: decodedQueries
).description
}
public static func and(_ queries: [String]) -> String {
let decoder = JSONDecoder()
let decodedQueries = queries.compactMap { queryStr -> Query? in
guard let data = queryStr.data(using: .utf8) else {
return nil
}
return try? decoder.decode(Query.self, from: data)
}
return Query(
method: "and",
values: decodedQueries
).description
}
public static func distanceEqual(_ attribute: String, values: [Any], distance: Double, meters: Bool = true) -> String {
return Query(
method: "distanceEqual",
attribute: attribute,
values: [[values, distance, meters]]
).description
}
public static func distanceNotEqual(_ attribute: String, values: [Any], distance: Double, meters: Bool = true) -> String {
return Query(
method: "distanceNotEqual",
attribute: attribute,
values: [[values, distance, meters]]
).description
}
public static func distanceGreaterThan(_ attribute: String, values: [Any], distance: Double, meters: Bool = true) -> String {
return Query(
method: "distanceGreaterThan",
attribute: attribute,
values: [[values, distance, meters]]
).description
}
public static func distanceLessThan(_ attribute: String, values: [Any], distance: Double, meters: Bool = true) -> String {
return Query(
method: "distanceLessThan",
attribute: attribute,
values: [[values, distance, meters]]
).description
}
public static func intersects(_ attribute: String, values: [Any]) -> String {
return Query(
method: "intersects",
attribute: attribute,
values: [values]
).description
}
public static func notIntersects(_ attribute: String, values: [Any]) -> String {
return Query(
method: "notIntersects",
attribute: attribute,
values: [values]
).description
}
public static func crosses(_ attribute: String, values: [Any]) -> String {
return Query(
method: "crosses",
attribute: attribute,
values: [values]
).description
}
public static func notCrosses(_ attribute: String, values: [Any]) -> String {
return Query(
method: "notCrosses",
attribute: attribute,
values: [values]
).description
}
public static func overlaps(_ attribute: String, values: [Any]) -> String {
return Query(
method: "overlaps",
attribute: attribute,
values: [values]
).description
}
public static func notOverlaps(_ attribute: String, values: [Any]) -> String {
return Query(
method: "notOverlaps",
attribute: attribute,
values: [values]
).description
}
public static func touches(_ attribute: String, values: [Any]) -> String {
return Query(
method: "touches",
attribute: attribute,
values: [values]
).description
}
public static func notTouches(_ attribute: String, values: [Any]) -> String {
return Query(
method: "notTouches",
attribute: attribute,
values: [values]
).description
}
private static func parseValue(_ value: Any) -> [Any] {
if let value = value as? [Any] {
return value
} else {
return [value]
}
}
}