-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
exp.go
463 lines (414 loc) · 9.73 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package psql
import (
"strings"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/core/internal/sdata"
"github.com/dosco/graphjin/core/internal/util"
)
type expContext struct {
*compilerContext
ti sdata.DBTable
prefixPath []string
skipNested bool
}
func (c *compilerContext) renderExp(ti sdata.DBTable, ex *qcode.Exp, skipNested bool) {
c.renderExpPath(ti, ex, skipNested, nil)
}
func (c *compilerContext) renderExpPath(ti sdata.DBTable, ex *qcode.Exp, skipNested bool, prefixPath []string) {
ec := expContext{
compilerContext: c,
ti: ti,
prefixPath: prefixPath,
skipNested: skipNested,
}
ec.render(ex)
}
func (c *expContext) render(ex *qcode.Exp) {
st := util.NewStackInf()
st.Push(ex)
for {
if st.Len() == 0 {
break
}
intf := st.Pop()
switch val := intf.(type) {
case int32:
switch val {
case '(':
c.w.WriteString(`(`)
case ')':
c.w.WriteString(`)`)
}
case qcode.ExpOp:
switch val {
case qcode.OpAnd:
c.w.WriteString(` AND `)
case qcode.OpOr:
c.w.WriteString(` OR `)
case qcode.OpNot:
c.w.WriteString(`NOT `)
case qcode.OpFalse:
c.w.WriteString(`false`)
}
case *qcode.Exp:
if val == nil {
return
}
switch val.Op {
case qcode.OpFalse:
st.Push(val.Op)
case qcode.OpAnd, qcode.OpOr:
st.Push(')')
for i := len(val.Children) - 1; i >= 0; i-- {
st.Push(val.Children[i])
if i > 0 {
st.Push(val.Op)
}
}
st.Push('(')
case qcode.OpNot:
st.Push(val.Children[0])
st.Push(qcode.OpNot)
case qcode.OpSelectExists:
if !c.skipNested {
c.renderNestedExp(st, val)
}
default:
c.renderOp(val)
}
}
}
}
func (c *expContext) renderNestedExp(st *util.StackInf, ex *qcode.Exp) {
firstJoin := ex.Joins[0]
c.w.WriteString(`EXISTS (SELECT 1 FROM `)
c.w.WriteString(firstJoin.Rel.Left.Col.Table)
if len(ex.Joins) > 1 {
for i := 1; i < len(ex.Joins); i++ {
c.renderJoin(ex.Joins[i])
}
}
c.w.WriteString(` WHERE `)
c.render(firstJoin.Filter)
c.w.WriteString(` AND `)
st.Push(')')
for i := len(ex.Children) - 1; i >= 0; i-- {
st.Push(ex.Children[i])
}
}
func (c *expContext) renderOp(ex *qcode.Exp) {
if ex.Op == qcode.OpNop {
return
}
if c.renderValPrefix(ex) {
return
}
if ex.Left.Col.Name != "" {
var table string
if ex.Left.Table == "" {
table = ex.Left.Col.Table
} else {
table = ex.Left.Table
}
c.w.WriteString(`((`)
if ex.Left.ID == -1 {
c.colWithTable(table, ex.Left.Col.Name)
} else {
colWithTableID(c.w, table, ex.Left.ID, ex.Left.Col.Name)
}
c.w.WriteString(`) `)
}
switch ex.Op {
case qcode.OpEquals:
c.w.WriteString(`=`)
case qcode.OpNotEquals:
c.w.WriteString(`!=`)
case qcode.OpNotDistinct:
c.w.WriteString(`IS NOT DISTINCT FROM`)
case qcode.OpDistinct:
c.w.WriteString(`IS DISTINCT FROM`)
case qcode.OpGreaterOrEquals:
c.w.WriteString(`>=`)
case qcode.OpLesserOrEquals:
c.w.WriteString(`<=`)
case qcode.OpGreaterThan:
c.w.WriteString(`>`)
case qcode.OpLesserThan:
c.w.WriteString(`<`)
case qcode.OpIn:
c.w.WriteString(`= ANY`)
case qcode.OpNotIn:
c.w.WriteString(`!= ALL`)
case qcode.OpLike:
c.w.WriteString(`LIKE`)
case qcode.OpNotLike:
c.w.WriteString(`NOT LIKE`)
case qcode.OpILike:
c.w.WriteString(`ILIKE`)
case qcode.OpNotILike:
c.w.WriteString(`NOT ILIKE`)
case qcode.OpSimilar:
c.w.WriteString(`SIMILAR TO`)
case qcode.OpNotSimilar:
c.w.WriteString(`NOT SIMILAR TO`)
case qcode.OpRegex:
switch c.ct {
case "mysql":
c.w.WriteString(`REGEXP`)
default:
c.w.WriteString(`~`)
}
case qcode.OpNotRegex:
switch c.ct {
case "mysql":
c.w.WriteString(`NOT REGEXP`)
default:
c.w.WriteString(`!~`)
}
case qcode.OpIRegex:
switch c.ct {
case "mysql":
c.w.WriteString(`REGEXP`)
default:
c.w.WriteString(`~*`)
}
case qcode.OpNotIRegex:
switch c.ct {
case "mysql":
c.w.WriteString(`NOT REGEXP`)
default:
c.w.WriteString(`!~*`)
}
case qcode.OpContains:
c.w.WriteString(`@>`)
case qcode.OpContainedIn:
c.w.WriteString(`<@`)
case qcode.OpHasKey:
c.w.WriteString(`?`)
case qcode.OpHasKeyAny:
c.w.WriteString(`?|`)
case qcode.OpHasKeyAll:
c.w.WriteString(`?&`)
case qcode.OpEqualsTrue:
c.w.WriteString(`(`)
c.renderParam(Param{Name: ex.Right.Val, Type: "boolean"})
c.w.WriteString(` IS TRUE)`)
return
case qcode.OpNotEqualsTrue:
c.w.WriteString(`(`)
c.renderParam(Param{Name: ex.Right.Val, Type: "boolean"})
c.w.WriteString(` IS NOT TRUE)`)
return
case qcode.OpIsNull:
if strings.EqualFold(ex.Right.Val, "false") {
c.w.WriteString(`IS NOT NULL)`)
} else {
c.w.WriteString(`IS NULL)`)
}
return
case qcode.OpIsNotNull:
if strings.EqualFold(ex.Right.Val, "false") {
c.w.WriteString(`IS NULL)`)
} else {
c.w.WriteString(`IS NOT NULL)`)
}
return
case qcode.OpTsQuery:
switch c.ct {
case "mysql":
//MATCH (name) AGAINST ('phone' IN BOOLEAN MODE);
c.w.WriteString(`(MATCH(`)
for i, col := range c.ti.FullText {
if i != 0 {
c.w.WriteString(`, `)
}
c.colWithTable(c.ti.Name, col.Name)
}
c.w.WriteString(`) AGAINST (`)
c.renderParam(Param{Name: ex.Right.Val, Type: "text"})
c.w.WriteString(` IN NATURAL LANGUAGE MODE))`)
default:
//fmt.Fprintf(w, `(("%s") @@ websearch_to_tsquery('%s'))`, c.ti.TSVCol, val.Val)
c.w.WriteString(`((`)
for i, col := range c.ti.FullText {
if i != 0 {
c.w.WriteString(` OR (`)
}
c.colWithTable(c.ti.Name, col.Name)
if c.cv >= 110000 {
c.w.WriteString(`) @@ websearch_to_tsquery(`)
} else {
c.w.WriteString(`) @@ to_tsquery(`)
}
c.renderParam(Param{Name: ex.Right.Val, Type: "text"})
c.w.WriteString(`)`)
}
c.w.WriteString(`)`)
}
return
}
c.w.WriteString(` `)
switch ex.Right.ValType {
case qcode.ValList:
c.renderList(ex)
default:
c.renderVal(ex)
}
c.w.WriteString(`)`)
}
func (c *expContext) renderValPrefix(ex *qcode.Exp) bool {
if ex.Right.ValType == qcode.ValVar {
return c.renderValVarPrefix(ex)
}
return false
}
func (c *expContext) renderValVarPrefix(ex *qcode.Exp) bool {
if ex.Op == qcode.OpIn || ex.Op == qcode.OpNotIn {
if c.ct == "mysql" {
c.w.WriteString(`JSON_CONTAINS(`)
c.renderParam(Param{Name: ex.Right.Val, Type: ex.Left.Col.Type, IsArray: true})
c.w.WriteString(`, CAST(`)
c.colWithTable(c.ti.Name, ex.Left.Col.Name)
c.w.WriteString(` AS JSON), '$')`)
return true
}
}
return false
}
func (c *expContext) renderVal(ex *qcode.Exp) {
if ex.Right.Col.Name != "" {
var table string
if ex.Right.Table == "" {
table = ex.Right.Col.Table
} else {
table = ex.Right.Table
}
pid := ex.Right.ID
if ex.Right.ID != -1 {
pid = ex.Right.ID
}
c.w.WriteString(`(`)
if ex.Right.Col.Array {
c.renderValArrayColumn(ex, table, pid)
} else {
if pid == -1 {
c.colWithTable(table, ex.Right.Col.Name)
} else {
colWithTableID(c.w, table, pid, ex.Right.Col.Name)
}
}
c.w.WriteString(`)`)
return
}
switch ex.Right.ValType {
case qcode.ValVar:
c.renderValVar(ex)
default:
if len(ex.Right.Path) == 0 {
c.squoted(ex.Right.Val)
return
}
path := append(c.prefixPath, ex.Right.Path...)
j := (len(path) - 1)
c.w.WriteString(`CAST(i.j`)
for i := 0; i < j; i++ {
c.w.WriteString(`->`)
c.squoted(path[i])
}
c.w.WriteString(`->>`)
c.squoted(path[j])
c.w.WriteString(` AS `)
c.w.WriteString(ex.Left.Col.Type)
c.w.WriteString(`)`)
}
}
func (c *expContext) renderValVar(ex *qcode.Exp) {
val, isVal := c.svars[ex.Right.Val]
switch {
case isVal && strings.HasPrefix(val, "sql:"):
c.w.WriteString(`(`)
c.renderVar(val[4:])
c.w.WriteString(`)`)
case isVal:
c.w.WriteString(`'`)
c.renderVar(val)
c.w.WriteString(`'`)
case ex.Op == qcode.OpIn || ex.Op == qcode.OpNotIn:
c.w.WriteString(`(ARRAY(SELECT json_array_elements_text(`)
c.renderParam(Param{Name: ex.Right.Val, Type: ex.Left.Col.Type, IsArray: true})
c.w.WriteString(`))`)
c.w.WriteString(` :: `)
c.w.WriteString(ex.Left.Col.Type)
c.w.WriteString(`[])`)
default:
c.renderParam(Param{Name: ex.Right.Val, Type: ex.Left.Col.Type, IsArray: false})
}
}
func (c *expContext) renderList(ex *qcode.Exp) {
switch c.ct {
case "mysql":
c.renderListMysql(ex)
default:
c.renderListPostgres(ex)
}
}
func (c *expContext) renderListPostgres(ex *qcode.Exp) {
c.w.WriteString(`(ARRAY[`)
for i := range ex.Right.ListVal {
if i != 0 {
c.w.WriteString(`, `)
}
switch ex.Right.ListType {
case qcode.ValBool, qcode.ValNum:
c.w.WriteString(ex.Right.ListVal[i])
case qcode.ValStr:
c.w.WriteString(`'`)
c.w.WriteString(ex.Right.ListVal[i])
c.w.WriteString(`'`)
}
}
c.w.WriteString(`])`)
}
func (c *expContext) renderListMysql(ex *qcode.Exp) {
c.w.WriteString(`(`)
for i := range ex.Right.ListVal {
if i != 0 {
c.w.WriteString(` UNION `)
}
c.w.WriteString(`SELECT `)
switch ex.Right.ListType {
case qcode.ValBool, qcode.ValNum:
c.w.WriteString(ex.Right.ListVal[i])
case qcode.ValStr:
c.w.WriteString(`'`)
c.w.WriteString(ex.Right.ListVal[i])
c.w.WriteString(`'`)
}
}
c.w.WriteString(`)`)
}
func (c *compilerContext) renderValArrayColumn(ex *qcode.Exp, table string, pid int32) {
col := ex.Right.Col
switch c.ct {
case "mysql":
c.w.WriteString(`SELECT _gj_jt.* FROM `)
c.w.WriteString(`(SELECT CAST(`)
if pid == -1 {
c.colWithTable(table, col.Name)
} else {
colWithTableID(c.w, table, pid, col.Name)
}
c.w.WriteString(` AS JSON) as ids) j, `)
c.w.WriteString(`JSON_TABLE(j.ids, "$[*]" COLUMNS(`)
c.w.WriteString(col.Name)
c.w.WriteString(` `)
c.w.WriteString(ex.Left.Col.Type)
c.w.WriteString(` PATH "$" ERROR ON ERROR)) AS _gj_jt`)
default:
if pid == -1 {
c.colWithTable(table, col.Name)
} else {
colWithTableID(c.w, table, pid, col.Name)
}
}
}