forked from jtlabsio/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querybuilder.go
322 lines (276 loc) · 8.54 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
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
package querybuilder
import (
"fmt"
"strconv"
queryoptions "go.jtlabs.io/query"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// QueryBuilder is a type that makes working with Mongo driver Find methods easier
// when used in combination with a QueryOptions struct that specifies filters,
// pagination details, sorting instructions and field projection details.
type QueryBuilder struct {
collection string
fieldTypes map[string]string
strictValidation bool
}
// NewQueryBuilder returns a new instance of a QueryBuilder object for constructing
// filters and options suitable for use with Mongo driver Find methods
func NewQueryBuilder(collection string, schema bson.M, strictValidation ...bool) *QueryBuilder {
qb := QueryBuilder{
collection: collection,
fieldTypes: map[string]string{},
strictValidation: false,
}
// parse the schema
if schema != nil {
qb.discoverFields(schema)
}
// override strict validation if provided
if len(strictValidation) > 0 {
qb.strictValidation = strictValidation[0]
}
return &qb
}
// Filter builds a suitable bson document to send to any of the find methods
// exposed by the Mongo driver. This method can validate the provided query
// options against the schema that was used to build the QueryBuilder instance
// when the QueryBuilder has strict validation enabled.
//
// The supported bson types for filter/search are:
// * array (strings only and not with $in operator unless sub items are strings)
// * bool
// * date
// * decimal
// * double
// * int
// * long
// * object (field detection)
// * string
// * timestamp
//
// The non-supported bson types for filter/search at this time
// * object (actual object comparison... only fields within the object are supported)
// * array (non string data)
// * binData
// * objectId
// * null
// * regex
// * dbPointer
// * javascript
// * symbol
// * javascriptWithScope
// * minKey
// * maxKey
func (qb QueryBuilder) Filter(qo queryoptions.Options) (bson.M, error) {
filter := bson.M{}
// fmt.Printf("fffffffffffffffff query options %v\n", qo)
if len(qo.Filter) > 0 {
for field, values := range qo.Filter {
// fmt.Printf("ffffffffffff field, value %v-------------%v\n", field, values)
var bsonType string
// lookup the field
if bt, ok := qb.fieldTypes[field]; ok {
bsonType = bt
}
// check for strict field validation
if bsonType == "" && qb.strictValidation {
return nil, fmt.Errorf("field %s does not exist in collection %s", field, qb.collection)
}
switch bsonType {
case "array":
f := detectStringComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "bool":
for _, value := range values {
bv, _ := strconv.ParseBool(value)
f := primitive.M{field: bv}
filter = combine(filter, f)
}
case "date":
f := detectDateComparisonOperator(field, values)
filter = combine(filter, f)
case "decimal":
f := detectNumericComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "double":
f := detectNumericComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "int":
f := detectNumericComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "long":
f := detectNumericComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "object":
f := detectStringComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "string":
f := detectStringComparisonOperator(field, values, bsonType)
filter = combine(filter, f)
case "timestamp":
// handle just like dates
f := detectDateComparisonOperator(field, values)
filter = combine(filter, f)
}
}
}
return filter, nil
}
// FindOptions creates a mongo.FindOptions struct with pagination details, sorting,
// and field projection instructions set as specified in the query options input
func (qb QueryBuilder) FindOptions(qo queryoptions.Options) (*options.FindOptions, error) {
opts := options.Find()
// determine pagination for the options
qb.setPaginationOptions(qo.Page, opts)
// determine projection for the options
if err := qb.setProjectionOptions(qo.Fields, opts); err != nil {
return nil, err
}
// determine sorting for the options
if err := qb.setSortOptions(qo.Sort, opts); err != nil {
return nil, err
}
return opts, nil
}
func (qb QueryBuilder) discoverFields(schema bson.M) {
// ensure fieldTypes is set
if qb.fieldTypes == nil {
qb.fieldTypes = map[string]string{}
}
// check to see if top level is $jsonSchema
if js, ok := schema["$jsonSchema"]; ok {
schema = js.(bson.M)
}
// bsonType, required, properties at top level
// looking for properties field, specifically
if properties, ok := schema["properties"]; ok {
properties := properties.(bson.M)
qb.iterateProperties("", properties)
}
}
func (qb QueryBuilder) iterateProperties(parentPrefix string, properties bson.M) {
// iterate each field within properties
for field, value := range properties {
switch value := value.(type) {
case bson.M:
// retrieve the type of the field
if bsonType, ok := value["bsonType"]; ok {
bsonType := bsonType.(string)
// capture type in the fieldTypes map
if bsonType != "" {
qb.fieldTypes[fmt.Sprintf("%s%s", parentPrefix, field)] = bsonType
}
if bsonType == "array" {
// look at "items" to get the bsonType
if items, ok := value["items"]; ok {
value = items.(bson.M)
// fix for issue where Array of type strings is not properly
// allowing filter with $in keyword
if bsonType, ok := value["bsonType"]; ok {
bsonType := bsonType.(string)
// capture type in the fieldTypes map
if bsonType != "" {
qb.fieldTypes[fmt.Sprintf("%s%s", parentPrefix, field)] = bsonType
}
}
}
}
// handle any sub-document schema details
if subProperties, ok := value["properties"]; ok {
subProperties := subProperties.(bson.M)
qb.iterateProperties(
fmt.Sprintf("%s%s.", parentPrefix, field), subProperties)
}
continue
}
// check for enum (without bsonType specified)
if _, ok := value["enum"]; ok {
qb.fieldTypes[fmt.Sprintf("%s%s", parentPrefix, field)] = "object"
}
default:
// properties are not of type bson.M
continue
}
}
}
func (qb QueryBuilder) setPaginationOptions(pagination map[string]int, opts *options.FindOptions) {
// check for limit
if limit, ok := pagination["limit"]; ok {
opts.SetLimit(int64(limit))
// check for offset (once limit is set)
if offset, ok := pagination["offset"]; ok {
opts.SetSkip(int64(offset))
}
// check for skip (once limit is set)
if skip, ok := pagination["skip"]; ok {
opts.SetSkip(int64(skip))
}
}
// check for page and size
if size, ok := pagination["size"]; ok {
opts.SetLimit(int64(size))
// set skip (requires understanding of size)
if page, ok := pagination["page"]; ok {
opts.SetSkip(int64(page * size))
}
}
}
func (qb QueryBuilder) setProjectionOptions(fields []string, opts *options.FindOptions) error {
// set field projections option
if len(fields) > 0 {
prj := map[string]int{}
for _, field := range fields {
val := 1
// handle when the first char is a - (don't display field in result)
if field[0:1] == "-" {
field = field[1:]
val = 0
}
// handle scenarios where the first char is a + (redundant)
if field[0:1] == "+" {
field = field[1:]
}
// lookup field in the fieldTypes dictionary if strictValidation is true
if qb.strictValidation {
if _, ok := qb.fieldTypes[field]; !ok {
// we have a problem
return fmt.Errorf("field %s does not exist in collection %s", field, qb.collection)
}
}
// add the field to the project dictionary
prj[field] = val
}
// add the projection to the FindOptions
if len(prj) > 0 {
opts.SetProjection(prj)
}
}
return nil
}
func (qb QueryBuilder) setSortOptions(fields []string, opts *options.FindOptions) error {
if len(fields) > 0 {
sort := map[string]int{}
for _, field := range fields {
val := 1
if field[0:1] == "-" {
field = field[1:]
val = -1
}
if field[0:1] == "+" {
field = field[1:]
}
// lookup field in the fieldTypes dictionary if strictValidation is true
if qb.strictValidation {
if _, ok := qb.fieldTypes[field]; !ok {
// we have a problem
return fmt.Errorf("field %s does not exist in collection %s", field, qb.collection)
}
}
sort[field] = val
}
opts.SetSort(sort)
}
return nil
}