This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcondition.go
377 lines (301 loc) · 8.4 KB
/
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
package go_cypherdsl
import (
"errors"
"fmt"
"strings"
)
type ConditionBuilder struct {
Start *operatorNode
Current *conditionNode
errors []error
}
//should be used to start a condition chain
func C(condition *ConditionConfig) ConditionOperator {
wq, err := NewCondition(condition)
if err != nil {
return &ConditionBuilder{
errors: []error{err},
}
}
cond := &ConditionBuilder{
Start: nil,
errors: nil,
Current: nil,
}
node := &operatorNode{
First: true,
Query: &conditionNode{
Condition: wq,
Next: nil,
},
}
err = cond.addNext(node)
if err != nil {
cond.addError(err)
}
return cond
}
//add an error to the condition chain
func (c *ConditionBuilder) addError(e error) {
if c.errors == nil {
c.errors = []error{e}
} else {
c.errors = append(c.errors, e)
}
}
//check if the builder has had any errors down the chain
func (c *ConditionBuilder) hasErrors() bool {
return c.errors != nil && len(c.errors) > 0
}
//add another node to the chain
func (c *ConditionBuilder) addNext(node *operatorNode) error {
if node == nil {
return errors.New("node can not be nil")
}
if node.Query == nil {
return errors.New("next can not be nil")
}
//different behavior if its the first of the chain
if c.Start == nil {
c.Start = node
c.Current = node.Query
} else {
c.Current.Next = node
c.Current = node.Query
}
return nil
}
func (c *ConditionBuilder) And(condition *ConditionConfig) ConditionOperator {
return c.addCondition(condition, "AND")
}
func (c *ConditionBuilder) Or(condition *ConditionConfig) ConditionOperator {
return c.addCondition(condition, "OR")
}
func (c *ConditionBuilder) Xor(condition *ConditionConfig) ConditionOperator {
return c.addCondition(condition, "XOR")
}
func (c *ConditionBuilder) Not(condition *ConditionConfig) ConditionOperator {
return c.addCondition(condition, "NOT")
}
func (c *ConditionBuilder) AndNested(query WhereQuery, err error) ConditionOperator {
return c.addNestedCondition(query, err, "AND")
}
func (c *ConditionBuilder) OrNested(query WhereQuery, err error) ConditionOperator {
return c.addNestedCondition(query, err, "OR")
}
func (c *ConditionBuilder) XorNested(query WhereQuery, err error) ConditionOperator {
return c.addNestedCondition(query, err, "XOR")
}
func (c *ConditionBuilder) NotNested(query WhereQuery, err error) ConditionOperator {
return c.addNestedCondition(query, err, "NOT")
}
func (c *ConditionBuilder) addNestedCondition(query WhereQuery, err error, condType string) ConditionOperator {
if c.hasErrors() {
return c
}
if err != nil {
c.addError(err)
return c
}
//create node, make sure to wrap the query in parenthases since its nested.
node := &operatorNode{
Condition: condType,
Query: &conditionNode{
Condition: "(" + query + ")",
Next: nil,
},
}
//add it
err = c.addNext(node)
if err != nil {
c.addError(err)
}
//return pointer to the builder
return c
}
func (c *ConditionBuilder) addCondition(condition *ConditionConfig, condType string) ConditionOperator {
//check if any errors are present, if they are, bail
if c.hasErrors() {
return c
}
//convert condition object into actual cypher
wq, err := NewCondition(condition)
if err != nil {
c.addError(err)
return c
}
//create the next node of the linked list
node := &operatorNode{
Condition: condType,
Query: &conditionNode{
Condition: wq,
Next: nil,
},
}
//add it
err = c.addNext(node)
if err != nil {
c.addError(err)
}
//return pointer to the builder
return c
}
func (c *ConditionBuilder) Build() (WhereQuery, error) {
//if it has errors, compile that and return
if c.hasErrors() {
errStr := ""
for _, err := range c.errors {
errStr += err.Error() + ";"
}
errStr = strings.TrimSuffix(errStr, ";")
return "", fmt.Errorf("(%v) errors occurred: %s", len(c.errors), errStr)
}
query := ""
//if start is not defined, something went wrong
if c.Start == nil {
return "", errors.New("no condition defined")
}
i := c.Start
//iterate...
for {
if i == nil || i.Query == nil {
break
}
t := ""
if i.First {
} else {
t += i.Condition + " "
}
query += t + i.Query.Condition.ToString() + " "
//iterate up
i = i.Query.Next
}
//return entire condition
return WhereQuery(strings.TrimSuffix(query, " ")), nil
}
type conditionNode struct {
Condition WhereQuery
Next *operatorNode
}
type operatorNode struct {
First bool
Condition string
Query *conditionNode
}
type ConditionOperator interface {
And(c *ConditionConfig) ConditionOperator
AndNested(query WhereQuery, err error) ConditionOperator
Or(c *ConditionConfig) ConditionOperator
OrNested(query WhereQuery, err error) ConditionOperator
Xor(c *ConditionConfig) ConditionOperator
XorNested(query WhereQuery, err error) ConditionOperator
Not(c *ConditionConfig) ConditionOperator
NotNested(query WhereQuery, err error) ConditionOperator
Build() (WhereQuery, error)
}
type BooleanOperator string
const (
LessThanOperator BooleanOperator = "<"
GreaterThanOperator BooleanOperator = ">"
LessThanOrEqualToOperator BooleanOperator = "<="
GreaterThanOrEqualToOperator BooleanOperator = ">="
EqualToOperator BooleanOperator = "="
InOperator BooleanOperator = "IN"
IsOperator BooleanOperator = "IS"
RegexEqualToOperator BooleanOperator = "=~"
StartsWithOperator BooleanOperator = "STARTS WITH"
EndsWithOperator BooleanOperator = "ENDS WITH"
ContainsOperator BooleanOperator = "CONTAINS"
)
//configuration object for where condition
type ConditionConfig struct {
//operators that can be used
ConditionOperator BooleanOperator
//condition functions that can be used
ConditionFunction string
Name string
Field string
Label string
//exclude parentheses
FieldManipulationFunction string
//if its a single check
Check interface{}
//if its a slice check
CheckSlice []interface{}
}
func (condition *ConditionConfig) ToString() (string, error) {
//check initial error conditions
if condition.Name == "" {
return "", errors.New("var name can not be empty")
}
if (condition.Field == "" && condition.Label == "") && condition.FieldManipulationFunction == "" {
return "", errors.New("field, function or label can not be empty")
}
if condition.Field != "" && condition.Label != "" && condition.FieldManipulationFunction != "" {
return "", errors.New("field and label can not both be defined")
}
query := ""
//build the fields
if condition.Field != "" {
query += fmt.Sprintf("%s.%s", condition.Name, condition.Field)
} else if condition.Label != "" {
//we're done here
return fmt.Sprintf("%s:%s", condition.Name, condition.Label), nil
} else {
query = condition.Name
}
if condition.FieldManipulationFunction != "" {
query = fmt.Sprintf("%s(%s)", condition.FieldManipulationFunction, query)
}
if condition.ConditionOperator == "" && condition.ConditionFunction == "" {
return "", errors.New("either condition operator or condition function has to be defined")
}
if condition.ConditionOperator != "" && condition.ConditionFunction != "" {
return "", errors.New("operator and function can not both be defined")
}
//build the operators
if condition.ConditionOperator != "" {
query += fmt.Sprintf(" %s", condition.ConditionOperator)
} else if condition.ConditionFunction != "" {
//if its a condition function, we're done
return fmt.Sprintf("%s(%s)", condition.ConditionFunction, query), nil
}
//check if its valid for in
if condition.ConditionOperator == InOperator {
if condition.CheckSlice == nil {
return "", errors.New("slice can not be nil")
}
if condition.Check != nil {
return "", errors.New("check should not be defined when using in operator")
}
if len(condition.CheckSlice) == 0 {
return "", errors.New("slice should not be nil")
}
q := "["
for _, val := range condition.CheckSlice {
str, err := cypherizeInterface(val)
if err != nil {
return "", err
}
q += fmt.Sprintf("%s,", str)
}
query += " " + strings.TrimSuffix(q, ",") + "]"
} else {
str, err := cypherizeInterface(condition.Check)
if err != nil {
return "", err
}
query += " " + str
}
return query, nil
}
func NewCondition(condition *ConditionConfig) (WhereQuery, error) {
if condition == nil {
return "", errors.New("condition can not be nil")
}
str, err := condition.ToString()
if err != nil {
return "", err
}
return WhereQuery(str), nil
}