-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
734 lines (686 loc) · 16.8 KB
/
query.go
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
// Package mongo
package mongo
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/assembly-hub/basics/util"
)
type queryNode struct {
Key string
Value interface{}
}
// Query
// = : "key": "val" or "key__eq": "val"
// < : "key__lt": 1
// <= : "key__lte": 1
// > : "key__gt": 1
// >= : "key__gte": 1
// != : "key__ne": 1
// in : "key__in": [1]
// all : "key__all": [1, 2, 3]
// not in : "key__nin": [1]
// size : "arr__size": 1
// exists : "key__exists": true
// mod : "key__mod": [10, 1], 基数,余数
// elemMatch : "key__match": MongoQuery
// like :
//
// "key__istartswith": "123"
// "key__startswith": "123"
// "key__iendswith": "123"
// "key__endswith": "123"
// "key__icontains": "123"
// "key__contains": "123"
//
// geo within query 2dsphere
//
// key__geo_within_polygon: [][]float64 or [][][]float64 闭合的多边形
// key__geo_within_multi_polygon: [][][]float64 or [][][][]float64 多个闭合的多边形
// key__geo_within_center_sphere: []float64, 长度必须是3,分辨为 lng, lat, radius(米)
//
// geo intersects query 2dsphere
//
// key__geo_intersects_polygon: [][]float64 or [][][]float64 闭合的多边形
//
// geo near query 2dsphere
//
// key__near: []float64 坐标点 or map[string]interface{}{
// "point": []float64 坐标点,
// "min": 最小距离,单位:米,
// "max": 最大距离,单位:米
// }
// key__near_sphere: []float64 坐标点 or map[string]interface{}{
// "point": []float64 坐标点,
// "min": 最小距离,单位:米,
// "max": 最大距离,单位:米
// }
//
// geo within query 2d
//
// key__geo_within_2d_box: [][]float64, bottom left, top right
// key__geo_within_2d_polygon: [][]float64 非闭合的多边形
// key__geo_within_2d_center: []float64, 长度必须是3,分辨为 lng, lat, radius(米)
type Query struct {
nodes []queryNode
rawCond map[string]interface{}
}
func NewQuery() *Query {
q := new(Query)
q.nodes = []queryNode{}
q.rawCond = map[string]interface{}{}
return q
}
func NotQ(key string, value interface{}) *Query {
rawQ := Q(key, value)
if len(rawQ.nodes) <= 0 {
panic(fmt.Sprintf("MongoNotQ key:%s, value:%v", key, value))
}
q := NewQuery()
if len(rawQ.nodes) == 1 {
q.nodes = []queryNode{{
Key: "$not",
Value: rawQ.nodes[0],
}}
} else {
var arrQ []queryNode
for _, subQ := range rawQ.nodes {
arrQ = append(arrQ, queryNode{
Key: "$not",
Value: subQ,
})
}
q.nodes = []queryNode{{
Key: "$or",
Value: arrQ,
}}
}
return q
}
func idFormat(value interface{}) interface{} {
switch val := value.(type) {
case string:
value = TryString2ObjectID(val)
case []string:
newVal := make([]interface{}, len(val))
for i, v := range val {
newVal[i] = TryString2ObjectID(v)
}
value = newVal
case [][]byte:
newVal := make([]interface{}, len(val))
for i, v := range val {
newVal[i] = TryString2ObjectID(string(v))
}
value = newVal
case []interface{}:
newVal := make([]interface{}, len(val))
for i, v := range val {
switch v := v.(type) {
case string:
newVal[i] = TryString2ObjectID(v)
case []byte:
newVal[i] = TryString2ObjectID(string(v))
default:
newVal[i] = v
}
}
value = newVal
}
return value
}
func generateForeignKeyCondition(key string, value interface{}) *Query {
if value != nil {
var q *refQ
switch value := value.(type) {
case refQ:
q = &value
case *refQ:
q = value
}
if q != nil {
if strings.Contains(key, "__") {
panic("refQ cols name can not contains '__'")
}
val, tp := q.getData(key)
mq := NewQuery()
if tp == mongoRefMatch {
mq.nodes = []queryNode{{
Key: key + ".$id__all",
Value: val,
}, {
Key: key + "__size",
Value: len(val),
}}
} else if tp == mongoRefAll {
mq.nodes = []queryNode{{
Key: key + ".$id__all",
Value: val,
}}
} else if tp == mongoRefDefault {
mq.nodes = []queryNode{{
Key: key + ".$id__in",
Value: val,
}}
} else {
panic("mongotool ref type error")
}
return mq
}
}
return nil
}
func geoGeoComplexCondition(key string, keys []string, value interface{}) (string, interface{}, bool) {
has := true
switch keys[1] {
case "geo_within_polygon":
var coordinates [][][]float64
switch value := value.(type) {
case [][]float64:
coordinates = [][][]float64{value}
case [][][]float64:
coordinates = value
default:
panic("type of geo_within_polygon's value must be [][]float64 or [][][]float64")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$geometry": map[string]interface{}{
"type": "Polygon",
"coordinates": coordinates,
},
}
case "geo_within_multi_polygon":
var coordinates [][][][]float64
switch value := value.(type) {
case [][][]float64:
coordinates = [][][][]float64{value}
case [][][][]float64:
coordinates = value
default:
panic("type of geo_within_multi_polygon's value must be [][][]float64 or [][][][]float64")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$geometry": map[string]interface{}{
"type": "MultiPolygon",
"coordinates": coordinates,
},
}
case "geo_within_center_sphere":
var floatArr []float64
switch value := value.(type) {
case []float64:
floatArr = value
default:
panic("type of geo_within_center_sphere's value must be []float64 and length need 3")
}
if len(floatArr) != 3 {
panic("type of geo_within_center_sphere's value must be []float64 and length need 3")
}
if floatArr[2] <= 0 {
panic("geo_within_center_sphere radius must be gt 0")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$centerSphere": []interface{}{[]float64{floatArr[0], floatArr[1]}, floatArr[2] / 1609.344 / 3963.2},
}
case "geo_intersects_polygon":
var coordinates [][][]float64
switch value := value.(type) {
case [][]float64:
coordinates = [][][]float64{value}
case [][][]float64:
coordinates = value
default:
panic("type of geo_within_polygon's value must be [][]float64 or [][][]float64")
}
key = keys[0] + "__geoIntersects"
value = map[string]interface{}{
"$geometry": map[string]interface{}{
"type": "Polygon",
"coordinates": coordinates,
},
}
case "geo_within_2d_box":
var box [][]float64
switch value := value.(type) {
case [][]float64:
if len(value) != 2 {
panic("type of geo_within_2d_box's value length is 2")
}
default:
panic("type of geo_within_2d_box's value must be [][]float64")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$box": box,
}
case "geo_within_2d_polygon":
var polygon [][]float64
switch value := value.(type) {
case [][]float64:
if len(value) < 3 {
panic("type of geo_within_2d_polygon's value length gte 3")
}
default:
panic("type of geo_within_2d_polygon's value must be [][]float64")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$polygon": polygon,
}
case "geo_within_2d_center":
var floatArr []float64
switch value := value.(type) {
case []float64:
floatArr = value
default:
panic("type of geo_within_2d_center's value must be []float64 and length need 3")
}
if len(floatArr) != 3 {
panic("type of geo_within_2d_center's value must be []float64 and length need 3")
}
if floatArr[2] <= 0 {
panic("geo_within_2d_center radius must be gt 0")
}
key = keys[0] + "__geoWithin"
value = map[string]interface{}{
"$center": []interface{}{[]float64{floatArr[0], floatArr[1]}, floatArr[2] / 1609.344 / 3963.2},
}
default:
has = false
}
return key, value, has
}
func geoGeoCondition(key string, keys []string, value interface{}) (string, interface{}, bool) {
key, value, has := geoGeoComplexCondition(key, keys, value)
if has {
return key, value, has
}
has = true
switch keys[1] {
case "near":
var point []float64
disMap := map[string]interface{}{}
switch value := value.(type) {
case []float64:
if len(value) != 2 {
panic("type of near's point length need 2")
}
case map[string]interface{}:
if min, ok := value["min"]; ok {
disMap["$minDistance"] = min
}
if max, ok := value["max"]; ok {
disMap["maxDistance"] = max
}
point = value["point"].([]float64)
if len(point) != 2 {
panic("type of near's point length need 2")
}
default:
panic("type of near's value must be []float64 or map[string]interface{}")
}
key = keys[0] + "__near"
temp := map[string]interface{}{
"$geometry": map[string]interface{}{
"type": "Point",
"coordinates": point,
},
}
for disKey, disVal := range disMap {
temp[disKey] = disVal
}
value = temp
case "near_sphere":
var point []float64
disMap := map[string]interface{}{}
switch value := value.(type) {
case []float64:
if len(value) != 2 {
panic("type of near's point length need 2")
}
case map[string]interface{}:
if min, ok := value["min"]; ok {
disMap["$minDistance"] = min
}
if max, ok := value["max"]; ok {
disMap["maxDistance"] = max
}
point = value["point"].([]float64)
if len(point) != 2 {
panic("type of near's point length need 2")
}
default:
panic("type of near's value must be []float64 or map[string]interface{}")
}
key = keys[0] + "__nearSphere"
temp := map[string]interface{}{
"$geometry": map[string]interface{}{
"type": "Point",
"coordinates": point,
},
}
for disKey, disVal := range disMap {
temp[disKey] = disVal
}
value = temp
default:
has = false
}
return key, value, has
}
func geoCondition(key string, keys []string, value interface{}) (string, interface{}) {
key, value, has := geoGeoCondition(key, keys, value)
if has {
return key, value
}
switch keys[1] {
case "istartswith":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": "^" + regexp.QuoteMeta(value.(string)) + ".*",
"$options": "i",
}
case "startswith":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": "^" + regexp.QuoteMeta(value.(string)) + ".*",
}
case "iendswith":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": ".*" + regexp.QuoteMeta(value.(string)) + "$",
"$options": "i",
}
case "endswith":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": ".*" + regexp.QuoteMeta(value.(string)) + "$",
}
case "icontains":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": ".*" + regexp.QuoteMeta(value.(string)) + ".*",
"$options": "i",
}
case "contains":
key = keys[0] + "__regex"
value = map[string]interface{}{
"$regex": ".*" + regexp.QuoteMeta(value.(string)) + ".*",
}
case "match":
if _, ok := value.(*Query); !ok {
panic("operator[match]'s value type must be MongoQuery' pointer")
}
key = keys[0] + "__elemMatch"
c := value.(*Query).Cond()
if len(c) <= 0 {
panic("match where cond is nil")
}
value = c
}
return key, value
}
func Q(key string, value interface{}) *Query {
q := generateForeignKeyCondition(key, value)
if q != nil {
return q
}
if strings.Contains(key, "__") {
keys := strings.Split(key, "__")
if keys[0] == "_id" || util.EndWith(keys[0], ".$id", false) {
value = idFormat(value)
}
key, value = geoCondition(key, keys, value)
} else {
if key == "_id" || util.EndWith(key, ".$id", false) {
value = idFormat(value)
}
}
q = NewQuery()
q.nodes = []queryNode{{
Key: key,
Value: value,
}}
return q
}
// MixQ
// ~: 结果取反,其中:$and、$or、$nor不适应
// $and、$or、$nor: values type must be map[string]interface{} or []*MongoQuery or []interface or []map[string]interface{}
func MixQ(cond map[string]interface{}) *Query {
if len(cond) <= 0 {
return NewQuery()
}
q := NewQuery()
for k, v := range cond {
if k == "" {
continue
}
if k[0] == '~' {
if util.ElemIn(k[1:], []string{"$and", "$or", "$nor"}) {
panic("$and、$or、$nor not supported '~'")
}
q.And(NotQ(k[1:], v))
} else if k == "$and" || k == "$or" || k == "$nor" {
var arrQuery []interface{}
switch v := v.(type) {
case []interface{}:
arrQuery = append(arrQuery, v...)
case []map[string]interface{}:
for _, v := range v {
arrQuery = append(arrQuery, v)
}
case []*Query:
for _, v := range v {
arrQuery = append(arrQuery, v)
}
case map[string]interface{}:
if k == "$and" {
arrQuery = append(arrQuery, v)
} else {
for subKey, subVal := range v {
arrQuery = append(arrQuery, map[string]interface{}{
subKey: subVal,
})
}
}
default:
panic("$and、$or、$nor: values type must be []map[string]interface{} or []*MongoQuery or mix type")
}
if len(arrQuery) <= 0 {
continue
} else {
var arr []*Query
for _, vv := range arrQuery {
switch vv := vv.(type) {
case map[string]interface{}:
subQ := MixQ(vv)
if !subQ.Empty() {
arr = append(arr, subQ)
}
case *Query:
if !vv.Empty() {
arr = append(arr, vv)
}
default:
panic("$and、$or、$nor value must be map[string]interface{} or *MongoQuery")
}
}
if len(arr) <= 0 {
continue
}
if k == "$and" {
q.And(arr...)
} else if k == "$or" {
q.Or(arr...)
} else if k == "$nor" {
q.Nor(arr...)
}
}
} else {
q.And(Q(k, v))
}
}
return q
}
// SetRawCond cond default nil means is not HQL and cond is not nil means use MongoQuery
// cond nil close raw query
func (q *Query) SetRawCond(cond map[string]interface{}) *Query {
q.rawCond = cond
return q
}
func (q *Query) And(filter ...*Query) *Query {
for _, f := range filter {
q.nodes = append(q.nodes, f.nodes...)
}
return q
}
func NewAnd(filter ...*Query) *Query {
q := NewQuery()
q.And(filter...)
return q
}
func (q *Query) Empty() bool {
return len(q.rawCond) <= 0 && len(q.nodes) <= 0
}
func (q *Query) orNor(filter ...*Query) []queryNode {
var ns []queryNode
for _, f := range filter {
m := f.Cond()
if len(m) <= 0 {
continue
}
var v []map[string]interface{}
if _, ok := m["$and"]; ok && len(m) == 1 {
v = m["$and"].([]map[string]interface{})
} else {
v = []map[string]interface{}{
m,
}
}
ns = append(ns, queryNode{
Key: "$and",
Value: v,
})
}
return ns
}
func (q *Query) Or(filter ...*Query) *Query {
var ns = q.orNor(filter...)
if len(ns) <= 0 {
return q
}
node := queryNode{
Key: "$or",
Value: ns,
}
q.nodes = append(q.nodes, node)
return q
}
func NewOr(filter ...*Query) *Query {
q := NewQuery()
q.Or(filter...)
return q
}
func (q *Query) Nor(filter ...*Query) *Query {
var ns = q.orNor(filter...)
if len(ns) <= 0 {
return q
}
node := queryNode{
Key: "$nor",
Value: ns,
}
q.nodes = append(q.nodes, node)
return q
}
func NewNor(filter ...*Query) *Query {
q := NewQuery()
q.Nor(filter...)
return q
}
func (q *Query) innerOrNorFilter(nodeList []queryNode) []map[string]interface{} {
var filterList []map[string]interface{}
for _, node := range nodeList {
m := q.innerFilter([]queryNode{node})
filterList = append(filterList, m)
}
return filterList
}
func (q *Query) innerNodeFilter(node queryNode) (k string, v interface{}) {
if node.Key == "$or" || node.Key == "$nor" {
orNor := q.innerOrNorFilter(node.Value.([]queryNode))
return node.Key, orNor
} else if node.Key == "$not" {
key, val := q.innerNodeFilter(node.Value.(queryNode))
return key, map[string]interface{}{
"$not": val,
}
} else if node.Key == "$and" {
return node.Key, node.Value
} else if strings.Contains(node.Key, "__") {
keys := strings.Split(node.Key, "__")
if keys[1] == "regex" {
return keys[0], node.Value
}
return keys[0], map[string]interface{}{
"$" + keys[1]: node.Value,
}
}
return node.Key, map[string]interface{}{
"$eq": node.Value,
}
}
func (q *Query) innerFilter(nodes []queryNode) map[string]interface{} {
filter := map[string]interface{}{}
for _, node := range nodes {
k, v := q.innerNodeFilter(node)
if k == "$or" || k == "$nor" {
if andQ, ok := filter["$and"]; ok {
andQ = append(andQ.([]map[string]interface{}), map[string]interface{}{
k: v,
})
filter["$and"] = andQ
} else {
filter["$and"] = []map[string]interface{}{{
k: v,
}}
}
} else {
if mQ, ok := filter[k]; ok {
for c, d := range v.(map[string]interface{}) {
if c == "$not" {
old := mQ.(map[string]interface{})[c]
if old != nil {
for kk, vv := range old.(map[string]interface{}) {
d.(map[string]interface{})[kk] = vv
}
}
}
mQ.(map[string]interface{})[c] = d
}
filter[k] = mQ
} else {
filter[k] = v
}
}
}
return filter
}
func (q *Query) Cond() map[string]interface{} {
if len(q.rawCond) > 0 {
return q.rawCond
}
filter := q.innerFilter(q.nodes)
return filter
}
func (q *Query) JSON() string {
marshal, err := json.Marshal(q.Cond())
if err != nil {
return ""
}
return string(marshal)
}