-
Notifications
You must be signed in to change notification settings - Fork 14
/
context.go
513 lines (456 loc) · 11.2 KB
/
context.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package goql
import (
"fmt"
"sort"
"strconv"
"github.com/fzerorubigd/goql/parse"
)
type fieldType int
const (
fieldTypeColumn fieldType = iota
fieldTypeCopy
fieldTypeStaticNumber
fieldTypeStaticString
fieldTypeStaticBool
fieldTypeFunction
fieldTypeParameter // means this is a ? and must fill it from the query parameters
)
type field struct {
order int
name string // TODO : support for alias
typ fieldType
staticStr string // for static column only
staticNum float64
copy int // for duplicated field, copy from another field
argsOrder []int // for function column only, the order of arguments in the fields list
show bool
staticBool bool
paramIdx int // for parameters from query input
}
type context struct {
pkg interface{}
q *parse.Query
table string
definition map[string]columnDef
flds []field
selected map[string]int
where parse.Stack
order int
}
const (
itemColumn parse.ItemType = -999
)
// A hack to handle column, I don't like this kind of hacks but I'm too bored :)
type dummy struct {
typ parse.ItemType
pos int
value string
data int
}
func (d dummy) Type() parse.ItemType {
return d.typ
}
func (d dummy) Pos() int {
return d.pos
}
func (d dummy) Value() string {
return d.value
}
func (d dummy) Data() int {
return d.data
}
func (d dummy) String() string {
return "dummy"
}
func newItem(t parse.ItemType, v string, p int, d int) parse.Item {
return dummy{
typ: t,
pos: p,
value: v,
data: d,
}
}
// execute the query
func execute(c interface{}, src *parse.Query, params ...interface{}) ([]string, [][]Getter, error) {
var err error
ctx := &context{pkg: c, q: src}
err = selectColumn(ctx)
if err != nil {
return nil, nil, err
}
return doQuery(ctx, params...)
}
func getStaticColumn(ctx *context, fl parse.Field, show bool) field {
assertType(fl.Item, parse.ItemNumber, parse.ItemTrue, parse.ItemFalse, parse.ItemLiteral1, parse.ItemQuestionMark)
defer func() {
ctx.order++
}()
name := "static"
var t field
if fl.Item.Type() == parse.ItemLiteral1 {
t = field{
order: ctx.order,
name: name,
show: show,
typ: fieldTypeStaticString,
staticStr: parse.GetTokenString(fl.Item),
}
}
if fl.Item.Type() == parse.ItemNumber {
f, _ := strconv.ParseFloat(fl.Item.Value(), 64)
t = field{
order: ctx.order,
name: name,
show: show,
typ: fieldTypeStaticNumber,
staticNum: f,
}
}
if fl.Item.Type() == parse.ItemTrue || fl.Item.Type() == parse.ItemFalse {
f := fl.Item.Type() == parse.ItemTrue
t = field{
order: ctx.order,
name: name,
show: show,
typ: fieldTypeStaticBool,
staticBool: f,
}
}
if fl.Item.Type() == parse.ItemQuestionMark {
t = field{
order: ctx.order,
name: name,
show: show,
typ: fieldTypeParameter,
paramIdx: fl.Item.Data(),
}
}
return t
}
func getFieldColumn(ctx *context, fl parse.Field, show bool) (field, error) {
if fl.Table != "" && fl.Table != ctx.table {
return field{}, fmt.Errorf("table %s is not in select, join is not supported", fl.Table)
}
_, ok := ctx.definition[fl.Item.Value()]
if !ok {
return field{}, fmt.Errorf("field %s is not available in table %s", fl.Item.Value(), ctx.table)
}
defer func() {
ctx.order++
}()
if o, sel := ctx.selected[fl.Item.Value()]; sel {
// this is already selected, simply use the copy type for it
return field{
order: ctx.order,
name: fl.Item.Value(),
show: show,
typ: fieldTypeCopy,
copy: o,
}, nil
}
ctx.selected[fl.Item.Value()] = ctx.order
return field{
order: ctx.order,
name: fl.Item.Value(),
show: show,
typ: fieldTypeColumn,
}, nil
}
func getFieldStar(ctx *context) []field {
res := make([]field, len(ctx.definition))
for i := range ctx.definition {
// the order in * is based on order in definition, so fix it
f, _ := getFieldColumn(ctx, parse.Field{Item: newItem(parse.ItemAlpha, i, 0, 0)}, true)
f.order = ctx.definition[i].Order()
ctx.selected[f.name] = f.order
res[f.order] = f
}
return res
}
func getFieldFunction(ctx *context, fl parse.Field, show bool) ([]field, error) {
assertType(fl.Item, parse.ItemFunc)
if !hasFunction(fl.Item.Value()) {
return nil, fmt.Errorf("function '%s' is not registered", fl.Item.Value())
}
f := []field{
{
order: ctx.order,
name: fl.Item.Value(),
show: show,
typ: fieldTypeFunction,
},
}
var params []field
ctx.order++
var err error
params, f[0].argsOrder, err = getFields(ctx, false, fl.Parameters...)
if err != nil {
return nil, err
}
return append(f, params...), nil
}
func isStatic(t parse.ItemType) bool {
return t == parse.ItemNumber ||
t == parse.ItemTrue ||
t == parse.ItemFalse ||
t == parse.ItemNull ||
t == parse.ItemLiteral1 ||
t == parse.ItemQuestionMark
}
func getFields(ctx *context, show bool, fls ...parse.Field) ([]field, []int, error) {
var direct []int
var res []field
for i := range fls {
direct = append(direct, ctx.order)
switch {
case fls[i].Item.Type() == parse.ItemWildCard:
if !show {
return nil, nil, fmt.Errorf("invalid * position")
}
res = append(res, getFieldStar(ctx)...)
case isStatic(fls[i].Item.Type()):
res = append(res, getStaticColumn(ctx, fls[i], show))
case fls[i].Item.Type() == parse.ItemFunc:
fs, err := getFieldFunction(ctx, fls[i], show)
if err != nil {
return nil, nil, err
}
res = append(res, fs...)
default:
f, err := getFieldColumn(ctx, fls[i], show)
if err != nil {
return nil, nil, err
}
res = append(res, f)
}
}
return res, direct, nil
}
func getOrderFileds(ctx *context, o ...parse.Order) ([]field, error) {
var res []field
for i := range o {
fs, err := getFieldColumn(ctx, parse.Field{Item: newItem(parse.ItemAlpha, o[i].Field, 0, 0)}, false)
if err != nil {
return nil, err
}
res = append(res, fs)
}
return res, nil
}
func getWhereField(ctx *context) (parse.Stack, []field, error) {
ss := ctx.q.Statement.(*parse.SelectStmt)
var res []field
s := parse.NewStack(0)
// which column are needed in where?
if st := ss.Where; st != nil {
for {
p, err := st.Pop()
if err != nil {
break
}
ts := p
switch p.Type() {
case parse.ItemAlpha, parse.ItemLiteral2:
v := parse.GetTokenString(p)
f, err := getFieldColumn(ctx, parse.Field{Item: newItem(parse.ItemAlpha, v, 0, 0)}, false)
if err != nil {
return nil, nil, err
}
res = append(res, f)
ts = newItem(itemColumn, v, 0, f.order)
case parse.ItemQuestionMark:
res = append(res, field{
order: ctx.order,
name: "static",
show: false,
typ: fieldTypeParameter,
paramIdx: p.Data(),
})
ts = newItem(itemColumn, "static", 0, ctx.order)
ctx.order++
case parse.ItemFunc:
fn := p.(parse.FuncItem)
fs, err := getFieldFunction(ctx, parse.Field{Item: p, Parameters: fn.Parameters()}, false)
if err != nil {
return nil, nil, err
}
res = append(res, fs...)
// function is calculated on fill gaps, then simply we can use the result at the end a static
ts = newItem(itemColumn, fn.Value(), 0, fs[0].order)
}
s.Push(ts)
}
}
return s, res, nil
}
func selectColumn(ctx *context) error {
ss := ctx.q.Statement.(*parse.SelectStmt)
tbl, err := getTable(ss.Table)
if err != nil {
return err
}
ctx.table = ss.Table
ctx.definition = tbl
ctx.order = 0
ctx.selected = make(map[string]int)
ctx.where = parse.NewStack(0)
// fields after select
fl, _, err := getFields(ctx, true, ss.Fields...)
if err != nil {
return err
}
ctx.flds = fl
// order fields
fl, err = getOrderFileds(ctx, ss.Order...)
if err != nil {
return err
}
ctx.flds = append(ctx.flds, fl...)
// where fields
ctx.where, fl, err = getWhereField(ctx)
if err != nil {
return err
}
ctx.flds = append(ctx.flds, fl...)
return nil
}
func filterColumn(ctx *context, items ...Getter) []Getter {
fl := ctx.flds
res := make([]Getter, 0, len(items))
for i := range fl {
if fl[i].show {
res = append(res, items[i])
}
}
return res
}
func fillGaps(ctx *context, res []Getter) error {
fl := ctx.flds
for i := range fl {
switch fl[i].typ {
case fieldTypeCopy:
res[fl[i].order] = res[fl[i].copy]
case fieldTypeStaticNumber:
res[i] = Number{Number: fl[i].staticNum}
case fieldTypeStaticString:
res[i] = String{String: fl[i].staticStr}
case fieldTypeStaticBool:
res[i] = Bool{Bool: fl[i].staticBool}
}
}
var err error
// once more for functions :/ if there is a way to fill it in one loop :/
// TODO : exec this at the getTableFields not after that
// since functions added before its parameter, doing it backward to make sure if a function is argument to another function
// is field before
for i := len(fl) - 1; i > -1; i-- {
if fl[i].typ == fieldTypeFunction {
args := make([]Getter, len(fl[i].argsOrder))
for j, v := range fl[i].argsOrder {
args[j] = res[v]
}
res[fl[i].order], err = executeFunction(fl[i].name, args...)
if err != nil {
return err
}
}
}
return nil
}
func callWhere(where getter, i []Getter) (ok bool, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("error : %v", e)
ok = false
}
}()
return toBool(where(i)), nil
}
func paramToStatic(f *field, p interface{}) {
// See https://golang.org/pkg/database/sql/driver/#Value
switch t := p.(type) {
case bool:
f.typ = fieldTypeStaticBool
f.staticBool = t
case string:
f.typ = fieldTypeStaticString
f.staticStr = t
case []byte:
f.typ = fieldTypeStaticString
f.staticStr = string(t)
case int64:
f.typ = fieldTypeStaticNumber
f.staticNum = float64(t)
case float64:
f.typ = fieldTypeStaticNumber
f.staticNum = t
default:
f.typ = fieldTypeStaticString
f.staticStr = fmt.Sprintf("%T", t)
}
}
func doQuery(ctx *context, params ...interface{}) ([]string, [][]Getter, error) {
res := make(chan []Getter, 3)
ss := ctx.q.Statement.(*parse.SelectStmt)
var all = make([]string, len(ctx.flds))
for i := range ctx.flds {
// only fields are allowed
if ctx.flds[i].typ == fieldTypeColumn {
all[i] = ctx.flds[i].name
}
if ctx.flds[i].typ == fieldTypeParameter {
paramToStatic(&ctx.flds[i], params[ctx.flds[i].paramIdx-1])
}
}
quit := make(chan struct{}, 1)
defer func() {
quit <- struct{}{}
close(quit) // prevent the channel leak.
}()
err := getTableFields(ctx.pkg, ss.Table, res, quit, all...)
if err != nil {
return nil, nil, err
}
where, err := buildFilter(ctx.where)
if err != nil {
return nil, nil, err
}
a := make([][]Getter, 0)
for i := range res {
if err = fillGaps(ctx, i); err != nil {
return nil, nil, err
}
ok, err := callWhere(where, i)
if err != nil {
return nil, nil, err
}
if !ok {
continue
}
a = append(a, filterColumn(ctx, i...))
}
column := make([]string, 0, len(ctx.flds))
for i := range ctx.flds {
if ctx.flds[i].show {
column = append(column, ctx.flds[i].name)
}
}
// sort
s := &sortMe{
data: a,
order: ss.Order,
}
sort.Sort(s)
a = s.data
if ss.Count >= 0 && ss.Start >= 0 {
l := len(a)
if ss.Start >= l {
a = [][]Getter{}
} else if ss.Start+ss.Count >= l {
a = a[ss.Start:] // to the end
} else {
a = a[ss.Start : ss.Start+ss.Count]
}
}
return column, a, nil
}