-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
exp.go
311 lines (273 loc) · 6.2 KB
/
exp.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
package qcode
import (
"errors"
"fmt"
"github.com/dosco/graphjin/core/internal/graph"
"github.com/dosco/graphjin/core/internal/sdata"
"github.com/dosco/graphjin/core/internal/util"
)
func (co *Compiler) compileArgObj(ti sdata.DBTableInfo, st *util.StackInf, arg *graph.Arg) (*Exp, bool, error) {
if arg.Val.Type != graph.NodeObj {
return nil, false, fmt.Errorf("expecting an object")
}
return co.compileArgNode(ti, st, arg.Val, true)
}
type aexp struct {
exp *Exp
node *graph.Node
}
func (co *Compiler) compileArgNode(
ti sdata.DBTableInfo,
st *util.StackInf,
node *graph.Node,
usePool bool) (*Exp, bool, error) {
var root *Exp
var needsUser bool
if node == nil || len(node.Children) == 0 {
return nil, false, errors.New("invalid argument value")
}
pushChild(st, nil, node)
for {
if st.Len() == 0 {
break
}
intf := st.Pop()
av, ok := intf.(aexp)
if !ok {
return nil, needsUser, fmt.Errorf("16: unexpected value %v (%t)", intf, intf)
}
// Objects inside a list
if av.node.Name == "" {
pushChildren(st, av.exp, av.node)
continue
}
ex, err := newExp(ti, st, av, usePool)
if err != nil {
return nil, needsUser, err
}
if ex == nil {
continue
}
if ex.Type == ValVar && ex.Val == "user_id" {
needsUser = true
}
if av.exp == nil {
root = ex
} else {
av.exp.Children = append(av.exp.Children, ex)
}
}
return root, needsUser, nil
}
func newExp(ti sdata.DBTableInfo, st *util.StackInf, av aexp, usePool bool) (*Exp, error) {
node := av.node
name := node.Name
if name[0] == '_' {
name = name[1:]
}
var ex *Exp
if usePool {
ex = expPool.Get().(*Exp)
ex.Reset()
} else {
ex = &Exp{doFree: false, internal: true}
}
ex.Children = ex.childrenA[:0]
switch name {
case "and":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'AND' operator")
}
ex.Op = OpAnd
pushChildren(st, ex, node)
case "or":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'OR' operator")
}
ex.Op = OpOr
pushChildren(st, ex, node)
case "not":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'NOT' operator")
}
ex.Op = OpNot
pushChild(st, ex, node)
case "eq", "equals":
ex.Op = OpEquals
ex.Val = node.Val
case "neq", "not_equals":
ex.Op = OpNotEquals
ex.Val = node.Val
case "gt", "greater_than":
ex.Op = OpGreaterThan
ex.Val = node.Val
case "lt", "lesser_than":
ex.Op = OpLesserThan
ex.Val = node.Val
case "gte", "gteq", "greater_or_equals":
ex.Op = OpGreaterOrEquals
ex.Val = node.Val
case "lte", "lteq", "lesser_or_equals":
ex.Op = OpLesserOrEquals
ex.Val = node.Val
case "in":
ex.Op = OpIn
setListVal(ex, node)
case "nin", "not_in":
ex.Op = OpNotIn
setListVal(ex, node)
case "like":
ex.Op = OpLike
ex.Val = node.Val
case "nlike", "not_like":
ex.Op = OpNotLike
ex.Val = node.Val
case "ilike":
ex.Op = OpILike
ex.Val = node.Val
case "nilike", "not_ilike":
ex.Op = OpNotILike
ex.Val = node.Val
case "similar":
ex.Op = OpSimilar
ex.Val = node.Val
case "nsimilar", "not_similar":
ex.Op = OpNotSimilar
ex.Val = node.Val
case "regex":
ex.Op = OpRegex
ex.Val = node.Val
case "nregex", "not_regex":
ex.Op = OpNotRegex
ex.Val = node.Val
case "iregex":
ex.Op = OpIRegex
ex.Val = node.Val
case "niregex", "not_iregex":
ex.Op = OpNotIRegex
ex.Val = node.Val
case "contains":
ex.Op = OpContains
ex.Val = node.Val
case "contained_in":
ex.Op = OpContainedIn
ex.Val = node.Val
case "has_key":
ex.Op = OpHasKey
ex.Val = node.Val
case "has_key_any":
ex.Op = OpHasKeyAny
ex.Val = node.Val
case "has_key_all":
ex.Op = OpHasKeyAll
ex.Val = node.Val
case "is_null":
ex.Op = OpIsNull
ex.Val = node.Val
case "null_eq", "ndis", "not_distinct":
ex.Op = OpNotDistinct
ex.Val = node.Val
case "null_neq", "dis", "distinct":
ex.Op = OpDistinct
ex.Val = node.Val
default:
if len(node.Children) == 0 {
return nil, fmt.Errorf("[Where] invalid operation: %s", name)
}
pushChildren(st, av.exp, node)
return nil, nil // skip node
}
if ex.Op != OpAnd && ex.Op != OpOr && ex.Op != OpNot {
switch node.Type {
case graph.NodeStr:
ex.Type = ValStr
case graph.NodeNum:
ex.Type = ValNum
case graph.NodeBool:
ex.Type = ValBool
case graph.NodeList:
ex.Type = ValList
case graph.NodeVar:
ex.Type = ValVar
default:
return nil, fmt.Errorf("[Where] invalid values for: %s", name)
}
if err := setWhereColName(ti, ex, node); err != nil {
return nil, err
}
}
return ex, nil
}
func setListVal(ex *Exp, node *graph.Node) {
if len(node.Children) != 0 {
switch node.Children[0].Type {
case graph.NodeStr:
ex.ListType = ValStr
case graph.NodeNum:
ex.ListType = ValNum
case graph.NodeBool:
ex.ListType = ValBool
}
} else {
ex.Val = node.Val
return
}
for i := range node.Children {
ex.ListVal = append(ex.ListVal, node.Children[i].Val)
}
}
func setWhereColName(ti sdata.DBTableInfo, ex *Exp, node *graph.Node) error {
var list []string
for n := node.Parent; n != nil; n = n.Parent {
if n.Type != graph.NodeObj {
continue
}
if n.Name != "" {
k := n.Name
if k == "and" || k == "or" || k == "not" ||
k == "_and" || k == "_or" || k == "_not" {
continue
}
list = append([]string{k}, list...)
}
}
switch len(list) {
case 0:
return fmt.Errorf("invalid where clause")
case 1:
if col, err := ti.GetColumn(list[0]); err == nil {
ex.Col = col
} else {
return err
}
default:
prev := ti.Name
for i := 0; i < len(list)-1; i++ {
curr := list[i]
// if at, ok := ti.Schema.GetAliasTable(list[i], prev); ok {
// curr = at
// }
if rel, err := ti.Schema.GetRel(curr, prev, ""); err == nil {
ex.Rels = append(ex.Rels, rel)
prev = curr
} else {
return err
}
}
rel := ex.Rels[len(ex.Rels)-1]
if col, err := rel.Left.Ti.GetColumn(list[len(list)-1]); err == nil {
ex.Col = col
} else {
return err
}
}
return nil
}
func pushChildren(st *util.StackInf, exp *Exp, node *graph.Node) {
for i := range node.Children {
st.Push(aexp{exp: exp, node: node.Children[i]})
}
}
func pushChild(st *util.StackInf, exp *Exp, node *graph.Node) {
st.Push(aexp{exp: exp, node: node.Children[0]})
}