-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_condition.go
404 lines (358 loc) · 11.8 KB
/
query_condition.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
package private_maprdb_go_client
import (
"errors"
"fmt"
"gopkg.in/karalabe/cookiejar.v1/collections/deque"
)
type endType int
const (
END endType = iota
)
var endT = [...]string{
"$end",
}
// QueryCondition logical operation type
type logicalOperation int
// QueryCondition logical operations constants
const (
AND logicalOperation = iota
OR
ELEMENT_AND
)
// String representation of QueryCondition logical operations
var logicalOperations = [...]string{
"$and",
"$or",
"$elementAnd",
}
// QueryCondition operation type
type conditionQueryOperation int
// QueryCondition operations constants
const (
EXISTS conditionQueryOperation = iota
NOT_EXISTS
IN
NOT_IN
TYPE_OF
NOT_TYPE_OF
MATCHES
NOT_MATCHES
LIKE
NOT_LIKE
)
// String representation of QueryCondition operations
var conditionQueryOperations = [...]string{
"$exists",
"$notexists",
"$in",
"$notin",
"$typeof",
"$nottypeof",
"$matches",
"$notmatches",
"$like",
"$notlike",
}
// QueryCondition comparison operation type
type Comparison int
// QueryCondition comparison operations constants
const (
LESS Comparison = iota
LESS_OR_EQUAL
GREATER
GREATER_OR_EQUAL
EQUAL
NOT_EQUAL
)
// String representation of QueryCondition comparison operations constants
var comparisonQueryOperations = [...]string{
"$lt",
"$le",
"$gt",
"$ge",
"$eq",
"$ne",
}
// Condition structure with required fields
type Condition struct {
tokens *deque.Deque
conditionContent map[string]interface{}
isBuilt bool
}
type ConditionOptions func(condition *Condition) (*Condition, error)
// MakeCondition returns new empty Condition
func MakeCondition(conditions ...ConditionOptions) (*Condition, error) {
condition := &Condition{tokens: deque.New(), conditionContent: make(map[string]interface{}), isBuilt: false}
var err error = nil
for _, opt := range conditions {
condition, err = opt(condition)
if err != nil {
return nil, err
}
}
return condition, nil
}
// IsEmpty checks Condition and returns boolean result
func (condition *Condition) IsEmpty() bool {
return len(condition.conditionContent) == 0
}
// IsBuilt checks is Condition built
func (condition *Condition) IsBuilt() bool {
return condition.isBuilt
}
// And adds the logical operator 'and' in query condition
func And() ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(AND)
return condition, nil
}
}
// Or adds the logical operator 'or' in query condition
func Or() ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(OR)
return condition, nil
}
}
// ElementAnd adds the logical operator 'elementAnd' in query condition
func ElementAnd(fieldPath string) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
if len(fieldPath) == 0 {
return nil, fmt.Errorf("fieldPath can't be empty")
}
condition.tokens.PushRight(ELEMENT_AND)
condition.tokens.PushRight(fieldPath)
return condition, nil
}
}
// Close adds the 'end' operation in condition queue.
// Close required after each logical operation as 'or', 'and'
// or 'elementAnd' and all query condition must ends on Close
func Close() ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(END)
return condition, nil
}
}
// Adds a condition that tests for existence of the specified.
func Exists(fieldPath string) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{conditionQueryOperations[EXISTS]: fieldPath})
return condition, nil
}
}
// Adds a condition that tests for non-existence of the specified fieldPath.
func NotExists(fieldPath string) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{conditionQueryOperations[NOT_EXISTS]: fieldPath})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is equal to at least one of the values in the specified list.
func In(fieldPath string, valueList []interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[IN]: map[string]interface{}{fieldPath: valueList}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is not equal to any of the values in the
func NotIn(fieldPath string, valueList []interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[NOT_IN]: map[string]interface{}{fieldPath: valueList}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is of the specified valueType.
func TypeOf(fieldPath string, valueType interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[TYPE_OF]: map[string]interface{}{fieldPath: valueType}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is not of the specified valueType.
func NotTypeOf(fieldPath string, valueType interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[NOT_TYPE_OF]: map[string]interface{}{fieldPath: valueType}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is a string and matches the specified regular expression.
func Matches(fieldPath string, regex interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[MATCHES]: map[string]interface{}{fieldPath: regex}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is a string and does not match the specified regular expression.
func NotMatches(fieldPath string, regex interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[NOT_MATCHES]: map[string]interface{}{fieldPath: regex}})
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath is a string and matches the specified SQL LIKE
// expression optionally escaped with the specified escape character.
func Like(fieldPath string, likeExpr ...interface{}) ConditionOptions {
return likeNotLike(LIKE, fieldPath, likeExpr)
}
// Adds a condition that tests if the value at the specified
// fieldPath is a string and does not match the specified SQL LIKE
// expression optionally escaped with the specified escape character.
func NotLike(fieldPath string, likeExpr ...interface{}) ConditionOptions {
return likeNotLike(NOT_LIKE, fieldPath, likeExpr)
}
// Adds a condition that tests if the value at the specified
// fieldPath equals the specified value. Two values are considered equal if and only if they contain the same
// key-value pair in the same order.
func Equals(fieldPath string, value interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
comparisonQueryOperations[EQUAL]: map[string]interface{}{fieldPath: value}})
return condition, nil
}
}
// Adds a condition that tests if the Value at the specified
// fieldPath does not equal the specified value.
// Two values are considered equal if and only if they contain the same key-value pair in the same order.
func NotEquals(fieldPath string, value interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
comparisonQueryOperations[NOT_EQUAL]: map[string]interface{}{fieldPath: value}})
return condition, nil
}
}
// Adds existing condition into new Query Condition
func AddCondition(conditionToAdd *Condition) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
if conditionToAdd.IsBuilt() {
condition.tokens.PushRight(conditionToAdd.AsMap())
} else {
return nil, errors.New("build condition before add it")
}
return condition, nil
}
}
// Adds existing condition into new Query Condition
func AddConditionMap(conditionToAdd map[string]interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(conditionToAdd)
return condition, nil
}
}
// Adds a condition that tests if the value at the specified
// fieldPath satisfies the given Op against the specified value.
func Is(fieldPath string, op Comparison, value interface{}) ConditionOptions {
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{comparisonQueryOperations[op]: map[string]interface{}{fieldPath: value}})
return condition, nil
}
}
func likeNotLike(conditionOperation conditionQueryOperation, fieldPath string, likeExpr []interface{}) ConditionOptions {
var queryLikeExpression interface{}
switch len(likeExpr) {
case 1:
queryLikeExpression = likeExpr[0]
case 2:
queryLikeExpression = likeExpr
default:
return func(condition *Condition) (*Condition, error) {
return condition, errors.New(conditionQueryOperations[conditionOperation] + " operation should have " +
"maximum 3 arguments")
}
}
return func(condition *Condition) (*Condition, error) {
condition.tokens.PushRight(map[string]interface{}{
conditionQueryOperations[conditionOperation]: map[string]interface{}{fieldPath: queryLikeExpression}})
return condition, nil
}
}
func (condition *Condition) parseQueue() error {
for !condition.tokens.Empty() {
token := condition.tokens.PopLeft()
if condition.tokens.Empty() {
if token != END {
return errors.New("all statements in condition must be closed")
} else {
continue
}
}
switch t := token.(type) {
case logicalOperation:
con, err := condition.buildBlock(t)
if err != nil {
return err
}
resMap, err := mergeQueryMaps(condition.conditionContent, con)
if err != nil {
return err
}
condition.conditionContent =
resMap.(map[string]interface{})
case map[string]interface{}:
resMap, err := mergeQueryMaps(condition.conditionContent, t)
if err != nil {
return err
}
condition.conditionContent =
resMap.(map[string]interface{})
case endType:
if !condition.tokens.Empty() {
return errors.New("all statements in condition must be closed")
}
}
}
return nil
}
func (condition *Condition) buildBlock(token logicalOperation) (map[string]interface{}, error) {
var statementList []interface{}
var elementAndEntry interface{}
if token == ELEMENT_AND {
elementAndEntry = condition.tokens.PopLeft()
}
for !condition.tokens.Empty() {
operation := condition.tokens.PopLeft()
switch t := operation.(type) {
case logicalOperation:
block, err := condition.buildBlock(t)
if err != nil {
return nil, err
}
statementList = append(statementList, block)
case endType:
if elementAndEntry == nil {
return map[string]interface{}{logicalOperations[token]: statementList}, nil
} else {
return map[string]interface{}{logicalOperations[token]: map[string]interface{}{elementAndEntry.(string): statementList}}, nil
}
default:
statementList = append(statementList, t)
}
}
return nil, errors.New("all statements in condition must be closed")
}
// Returns condition as Map
func (condition *Condition) AsMap() map[string]interface{} {
return condition.conditionContent
}
// Builds Query Condition
func (condition *Condition) Build() (*Condition, error) {
err := condition.parseQueue()
if err != nil {
return nil, err
}
condition.isBuilt = true
return condition, nil
}