-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquerybuilder.go
59 lines (47 loc) · 1.14 KB
/
querybuilder.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
package redkeep
import (
"strings"
"gopkg.in/mgo.v2/bson"
)
func checkKey(hackstack []string, field string) bool {
for _, b := range hackstack {
if b == field {
return true
}
if strings.HasPrefix(field, b+".") {
return true
}
}
return false
}
//BuildInsertQuery generates the query
func BuildInsertQuery(w Watch, command map[string]interface{}) bson.M {
normalizingFields := bson.M{}
for key, value := range command {
if checkKey(w.TrackFields, key) {
normalizingFields[w.TargetNormalizedField+"."+key] = value
}
}
if len(normalizingFields) == 0 {
return nil
}
return bson.M{"$set": normalizingFields}
}
//BuildUpdateQuery generates the query
func BuildUpdateQuery(w Watch, command map[string]interface{}) bson.M {
normalizingFields := bson.M{}
for queryType, query := range command {
if mappedQuery, ok := query.(map[string]interface{}); ok {
for key, value := range mappedQuery {
if checkKey(w.TrackFields, key) {
normalizingFields[w.TargetNormalizedField+"."+key] = value
}
}
}
if len(normalizingFields) == 0 {
return nil
}
return bson.M{queryType: normalizingFields}
}
return nil
}