-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpression.go
331 lines (273 loc) · 9.29 KB
/
expression.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
package nqb
import (
"bytes"
"fmt"
)
var (
nullExpr = &Expression{"NULL"}
trueExpr = &Expression{"TRUE"}
falseExpr = &Expression{"FALSE"}
missingExpr = &Expression{"MISSING"}
emptyExpr = &Expression{""}
)
// Expression represents a N1QL Expression.
type Expression struct {
value interface{}
}
// X creates an arbitrary expression from the given value.
//
// No quoting or escaping will be done on the input.
// In addition, it is not checked if the given value is an actual valid (N1QL syntax wise) expression.
func X(value interface{}) *Expression {
switch value.(type) {
case *Expression:
return value.(*Expression)
case bool:
if value == true {
return trueExpr
}
return falseExpr
default:
return &Expression{value}
}
}
// Sub creates an expression from a given sub-Statement, wrapping it in parentheses.
func Sub(statement Statement) *Expression {
return &Expression{"(" + statement.(fmt.Stringer).String() + ")"}
}
// Par wraps an Expression in parentheses.
func Par(expression *Expression) *Expression {
return infix(expression.String(), "(", ")")
}
// Path constructs a path ("a.b.c") from Expressions or values.
// Strings are considered identifiers (so they won't be quoted).
func Path(pathComponents ...interface{}) *Expression {
if pathComponents == nil || len(pathComponents) == 0 {
return emptyExpr
}
path := bytes.Buffer{}
for i, p := range pathComponents {
if i > 0 {
path.WriteString(".")
}
switch p.(type) {
case *Expression:
path.WriteString(p.(*Expression).String())
default:
path.WriteString(fmt.Sprint(p))
}
}
return &Expression{path.String()}
}
// P is an alias for the Path function
func P(pathComponents ...interface{}) *Expression {
return Path(pathComponents...)
}
// I constructs an identifier or list of identifiers escaped using back-quotes (`).
//
// Useful for example for identifiers that contains a dash like "beer-sample".
// Multiple identifiers are returned as a list of escaped identifiers separated by ", ".
func I(identifiers ...string) *Expression {
return &Expression{wrapWith('`', identifiers...)}
}
// S constructs an identifier or list of identifiers which will be quoted as strings (with "").
func S(strings ...string) *Expression {
return &Expression{wrapWith('"', strings...)}
}
// TRUE returns an expression representing boolean TRUE.
func TRUE() *Expression {
return trueExpr
}
// FALSE returns an expression representing boolean FALSE.
func FALSE() *Expression {
return falseExpr
}
// NULL returns an expression representing NULL.
func NULL() *Expression {
return nullExpr
}
// MISSING returns an expression representing MISSING.
func MISSING() *Expression {
return missingExpr
}
// Ph returns a positional or named query placeholder string (i.e. $1 or $name)
func Ph(nameOrPosition string) string {
return "$" + nameOrPosition
}
// Not negates the given expression by prefixing a NOT.
func (e *Expression) Not() *Expression {
return prefix("NOT", e.String())
}
// And AND-combines two expressions.
func (e *Expression) And(right interface{}) *Expression {
return infix("AND", e.String(), toString(right))
}
// Or OR-combines two expressions.
func (e *Expression) Or(right interface{}) *Expression {
return infix("OR", e.String(), toString(right))
}
// Eq combines two expressions with the equals operator ("=").
func (e *Expression) Eq(right interface{}) *Expression {
return infix("=", e.String(), toString(right))
}
// Ne combines two expressions with the not equals operator ("!=").
func (e *Expression) Ne(right interface{}) *Expression {
return infix("!=", e.String(), toString(right))
}
// Gt combines two expressions with the greater than operator (">").
func (e *Expression) Gt(right interface{}) *Expression {
return infix(">", e.String(), toString(right))
}
// Lt combines two expressions with the less than operator ("<").
func (e *Expression) Lt(right interface{}) *Expression {
return infix("<", e.String(), toString(right))
}
// Gte combines two expressions with the greater or equals than operator (">=").
func (e *Expression) Gte(right interface{}) *Expression {
return infix(">=", e.String(), toString(right))
}
// Concat combines two expressions with the concatenation operator ("||").
func (e *Expression) Concat(right interface{}) *Expression {
return infix("||", e.String(), toString(right))
}
// Lte combines two expressions with the less or equals than operator ("<=").
func (e *Expression) Lte(right interface{}) *Expression {
return infix("<=", e.String(), toString(right))
}
// IsValued appends an "IS VALUED" to the expression.
func (e *Expression) IsValued() *Expression {
return postfix("IS VALUED", e.String())
}
// IsNotValued appends an "IS NOT VALUED" to the expression.
func (e *Expression) IsNotValued() *Expression {
return postfix("IS NOT VALUED", e.String())
}
// IsNull appends an "IS NULL" to the expression.
func (e *Expression) IsNull() *Expression {
return postfix("IS NULL", e.String())
}
// IsNotNull appends an "IS NOT NULL" to the expression.
func (e *Expression) IsNotNull() *Expression {
return postfix("IS NOT NULL", e.String())
}
// IsMissing appends an "IS MISSING" to the expression.
func (e *Expression) IsMissing() *Expression {
return postfix("IS MISSING", e.String())
}
// IsNotMissing appends an "IS NOT MISSING" to the expression.
func (e *Expression) IsNotMissing() *Expression {
return postfix("IS NOT MISSING", e.String())
}
// Between adds a BETWEEN clause between the current and the given expression.
func (e *Expression) Between(right interface{}) *Expression {
return infix("BETWEEN", e.String(), toString(right))
}
// NotBetween adds a NOT BETWEEN clause between the current and the given expression.
func (e *Expression) NotBetween(right interface{}) *Expression {
return infix("NOT BETWEEN", e.String(), toString(right))
}
// Like adds a LIKE clause between the current and the given expression.
func (e *Expression) Like(right interface{}) *Expression {
return infix("LIKE", e.String(), toString(right))
}
// NotLike adds a NOT LIKE clause between the current and the given expression.
func (e *Expression) NotLike(right interface{}) *Expression {
return infix("NOT LIKE", e.String(), toString(right))
}
// Exists prefixes the current expression with the EXISTS clause.
func (e *Expression) Exists() *Expression {
return prefix("EXISTS", e.String())
}
// In adds a IN clause between the current and the given expression.
func (e *Expression) In(right interface{}) *Expression {
return infix("IN", e.String(), toString(right))
}
// NotIn adds a NOT IN clause between the current and the given expression.
func (e *Expression) NotIn(right interface{}) *Expression {
return infix("NOT IN", e.String(), toString(right))
}
// As Adds a AS clause between the current and the given expression. Often used to alias an identifier.
func (e *Expression) As(alias interface{}) *Expression {
return infix("AS", e.String(), toString(alias))
}
// Add establishes arithmetic addition between current and given expression.
func (e *Expression) Add(value interface{}) *Expression {
return infix("+", e.String(), toString(value))
}
// Subtract establishes arithmetic v between current and given expression.
func (e *Expression) Subtract(value interface{}) *Expression {
return infix("-", e.String(), toString(value))
}
// Multiply establishes arithmetic multiplication between current and given expression.
func (e *Expression) Multiply(value interface{}) *Expression {
return infix("*", e.String(), toString(value))
}
// Divide establishes arithmetic division between current and given expression.
func (e *Expression) Divide(value interface{}) *Expression {
return infix("/", e.String(), toString(value))
}
// Get an attribute of an object using the given value as attribute name.
func (e *Expression) Get(expression interface{}) *Expression {
switch expression.(type) {
case *Expression:
return e.Get(expression.(*Expression).String())
default:
return &Expression{Path(e.String(), X(expression))}
}
}
// Helper method to prefix a string.
func prefix(prefix, right string) *Expression {
return &Expression{prefix + " " + right}
}
// Helper method to infix a string.
func infix(infix, left, right string) *Expression {
return &Expression{left + " " + infix + " " + right}
}
// Helper method to postfix a string.
func postfix(postfix, left string) *Expression {
return &Expression{left + " " + postfix}
}
// Helper method to wrap variadic arguments with the given character.
// Separates multiple arguments with a ", "
func wrapWith(wrapper byte, input ...string) string {
escaped := bytes.Buffer{}
for n, i := range input {
if n > 0 {
escaped.WriteString(", ")
}
escaped.WriteByte(wrapper)
escaped.WriteString(i)
escaped.WriteByte(wrapper)
}
return escaped.String()
}
func (e *Expression) String() string {
switch e.value.(type) {
case string:
return e.value.(string)
default:
return fmt.Sprint(e.value)
}
}
func toExpressions(values ...interface{}) []*Expression {
var converted []*Expression
for _, value := range values {
switch value.(type) {
case *Expression:
converted = append(converted, value.(*Expression))
default:
converted = append(converted, X(value))
}
}
return converted
}
func toString(value interface{}) string {
switch value.(type) {
case *Expression:
return value.(*Expression).String()
case string:
return value.(string)
default:
return fmt.Sprint(value)
}
}