-
Notifications
You must be signed in to change notification settings - Fork 11
/
query.go
324 lines (295 loc) · 8.89 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
package util
import (
"net/url"
"strings"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/mgo.v2/bson"
)
var (
// OrderParam is the key of the url
// order parameter
OrderParam = "order_by"
)
// QueryParser used to store the context and
// gin request form
type QueryParser struct {
context *gin.Context
From url.Values
}
// NewQueryParser initialize a new QueryParser
func NewQueryParser(c *gin.Context) QueryParser {
parser := QueryParser{}
parser.context = c
// parses the raw query from the URL and updates r.Form
parser.context.Request.ParseForm()
// copy to a local form
parser.From = parser.context.Request.Form
return parser
}
// OrderBy appends order_by clause to mongodb query
func (p *QueryParser) OrderBy() string {
return p.context.Query(OrderParam)
}
// Match adds equality condition to match an exact match
func (p *QueryParser) Match(s []string, query bson.M) bson.M {
for i := range s {
if q := p.context.Query(s[i]); q != "" {
query[s[i]] = q
}
}
return query
}
// Lookups calls all lookup functions
func (p *QueryParser) Lookups(fields []string, query bson.M) bson.M {
query = p.Exact(fields, query)
query = p.IExact(fields, query)
query = p.Contains(fields, query)
query = p.IContains(fields, query)
query = p.Startswith(fields, query)
query = p.IStartswith(fields, query)
query = p.Endswith(fields, query)
query = p.IEndswith(fields, query)
query = p.Gt(fields, query)
query = p.Gte(fields, query)
query = p.Lt(fields, query)
query = p.Lte(fields, query)
query = p.IsNull(fields, query)
query = p.In(fields, query)
query = p.Eq(fields, query)
query = p.Ne(fields, query)
query = p.Gte(fields, query)
return query
}
// Field lookups
// ------------------------
// Exact adds regex to mgo query to check a field is an exact match
// accepting fields must pass to fields parameter
func (p *QueryParser) Exact(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__exact"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$regex": bson.RegEx{Pattern: "^" + ar[j] + "$", Options: ""}}
}
}
}
return query
}
// IExact adds regex to mgo query to check a field is an exact match
// this is the case insensitive version of Exact
// accepting fields must pass to fields parameter
func (p *QueryParser) IExact(s []string, query bson.M) bson.M {
for i := range s {
// avoid hacks by using defined parameters
ic := s[i] + "__iexact"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[s[i]] = bson.M{"$regex": bson.RegEx{Pattern: "^" + ar[j] + "$", Options: "i"}}
}
}
}
return query
}
// Contains adds regex to mgo query to check a field contain a value
// accepting fields must pass to fields parameter
func (p *QueryParser) Contains(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__contains"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$regex": bson.RegEx{Pattern: ".*" + ar[j] + ".*", Options: ""}}
}
}
}
return query
}
// IContains adds regex to mgo query to check a field contain a value
// this is the case insensitive version of contains
// accepting fields must pass to fields parameter
func (p *QueryParser) IContains(s []string, query bson.M) bson.M {
for i := range s {
// avoid hacks by using defined parameters
ic := s[i] + "__icontains"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[s[i]] = bson.M{"$regex": bson.RegEx{Pattern: ".*" + ar[j] + ".*", Options: "i"}}
}
}
}
return query
}
// Startswith adds regex to mgo query to check a field startswith a value
// accepting fields must pass to fields parameter
func (p *QueryParser) Startswith(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__startswith"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$regex": bson.RegEx{Pattern: "^" + ar[j], Options: ""}}
}
}
}
return query
}
// IStartswith adds regex to mgo query to check a field startswith a value
// this is the case insensitive version of startswith
// accepting fields must pass to fields parameter
func (p *QueryParser) IStartswith(s []string, query bson.M) bson.M {
for i := range s {
// avoid hacks by using defined parameters
ic := s[i] + "__istartswith"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[s[i]] = bson.M{"$regex": bson.RegEx{Pattern: "^" + ar[j], Options: "i"}}
}
}
}
return query
}
// Endswith adds regex to mgo query to check a field endswith a value
// accepting fields must pass to fields parameter
func (p *QueryParser) Endswith(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__endswith"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$regex": bson.RegEx{Pattern: ar[j] + "$", Options: ""}}
}
}
}
return query
}
// IEndswith adds regex to mgo query to check a field endswith a value
// this is the case insensitive version of endswith
// accepting fields must pass to fields parameter
func (p *QueryParser) IEndswith(s []string, query bson.M) bson.M {
for i := range s {
// avoid hacks by using defined parameters
ic := s[i] + "__iendswith"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[s[i]] = bson.M{"$regex": bson.RegEx{Pattern: ar[j] + "$", Options: "i"}}
}
}
}
return query
}
// Gt adds $gt to mgo query to check a field greater than the comparison value
// accepting fields must pass to fields parameter
func (p *QueryParser) Gt(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__gt"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$gt": ar[j]}
}
}
}
return query
}
// Gte adds $gte to mgo query to check a field greater or equal to the comparison value
// accepting fields must pass to fields parameter
func (p *QueryParser) Gte(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__gte"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$gte": ar[j]}
}
}
}
return query
}
// Lt adds $lt to mgo query to check a field less than the comparison value
// accepting fields must pass to fields parameter
func (p *QueryParser) Lt(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__lt"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$lt": ar[j]}
}
}
}
return query
}
// Lte adds $lte to mgo query to check a field less than or equal to the comparison value
// accepting fields must pass to fields parameter
func (p *QueryParser) Lte(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__lte"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$lte": ar[j]}
}
}
}
return query
}
// IsNull check the given field or related object is null
func (p *QueryParser) IsNull(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__isnull"
if ar := p.From[ic]; len(ar) > 0 {
for range ar {
query[fields[i]] = bson.M{ "$or": bson.M{fields[i]: nil, "$exists": false}}
}
}
}
return query
}
// In Check the given field's value is present in the list provide
// accepting fields must pass to fields parameter
func (p *QueryParser) In(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__in"
// if [ field__in=value ] available in form
if ar := p.From[ic]; len(ar) > 0 {
// loop though all in operators
for j := range ar {
// split string by `,`
values := strings.Split(ar[j], ",")
if len(values) > 0 {
query[fields[i]] = bson.M{"$in": values}
}
}
}
}
return query
}
// Eq check the given field or related object is equel to given value
func (p *QueryParser) Eq(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__eq"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$eq": ar[j]}
}
}
}
return query
}
// Ne check the given field or related object is not equal to given value
func (p *QueryParser) Ne(fields []string, query bson.M) bson.M {
for i := range fields {
// avoid hacks by using defined parameters
ic := fields[i] + "__ne"
if ar := p.From[ic]; len(ar) > 0 {
for j := range ar {
query[fields[i]] = bson.M{"$ne": ar[j]}
}
}
}
return query
}