-
Notifications
You must be signed in to change notification settings - Fork 5
/
mongodb.go
144 lines (116 loc) · 2.91 KB
/
mongodb.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
package generators
import (
"encoding/json"
"fmt"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/pivot/filter"
)
// MongoDB Query Generator
type MongoDB struct {
filter.Generator
collection string
fields []string
criteria []map[string]interface{}
options map[string]interface{}
values []interface{}
facetFields []string
aggregateBy []filter.Aggregate
}
func NewMongoDBGenerator() *MongoDB {
return &MongoDB{
Generator: filter.Generator{},
}
}
func (self *MongoDB) Initialize(collectionName string) error {
self.Reset()
self.collection = collectionName
self.fields = make([]string, 0)
self.criteria = make([]map[string]interface{}, 0)
self.options = make(map[string]interface{})
self.values = make([]interface{}, 0)
return nil
}
func (self *MongoDB) Finalize(f *filter.Filter) error {
conjunction := `$and`
var query map[string]interface{}
if f.Conjunction == filter.OrConjunction {
conjunction = `$or`
}
if f.Spec == `all` {
query = map[string]interface{}{}
} else if len(self.criteria) == 1 {
query = self.criteria[0]
} else {
query = map[string]interface{}{
conjunction: self.criteria,
}
}
if data, err := json.MarshalIndent(query, ``, ` `); err == nil {
self.Push(data)
} else {
return err
}
return nil
}
func (self *MongoDB) WithField(field string) error {
if field == `id` {
field = `_id`
}
self.fields = append(self.fields, field)
return nil
}
func (self *MongoDB) SetOption(key string, value interface{}) error {
self.options[key] = value
return nil
}
func (self *MongoDB) GroupByField(field string) error {
if field == `id` {
field = `_id`
}
self.facetFields = append(self.facetFields, field)
return nil
}
func (self *MongoDB) AggregateByField(agg filter.Aggregation, field string) error {
if field == `id` {
field = `_id`
}
self.aggregateBy = append(self.aggregateBy, filter.Aggregate{
Aggregation: agg,
Field: field,
})
return nil
}
func (self *MongoDB) GetValues() []interface{} {
return self.values
}
func (self *MongoDB) WithCriterion(criterion filter.Criterion) error {
var c map[string]interface{}
var err error
if criterion.Field == `id` {
criterion.Field = `_id`
}
for i, value := range criterion.Values {
switch value.(type) {
case string:
criterion.Values[i] = stringutil.Autotype(value)
}
}
switch criterion.Operator {
case `is`, ``:
c, err = mongoCriterionOperatorIs(self, criterion)
case `not`:
c, err = mongoCriterionOperatorNot(self, criterion)
case `contains`, `prefix`, `suffix`, `like`, `unlike`:
c, err = mongoCriterionOperatorPattern(self, criterion.Operator, criterion)
case `gt`, `gte`, `lt`, `lte`, `range`:
c, err = mongoCriterionOperatorRange(self, criterion, criterion.Operator)
default:
return fmt.Errorf("Unimplemented operator '%s'", criterion.Operator)
}
if err != nil {
return err
} else {
self.criteria = append(self.criteria, c)
}
return nil
}