forked from araddon/qlbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlvm.go
65 lines (55 loc) · 1.6 KB
/
sqlvm.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
package vm
import (
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/rel"
"github.com/araddon/qlbridge/value"
)
// EvalSql Is a partial SQL statement evaluator (that doesn't get it all right). See
// exec package for full sql evaluator. Can be used to evaluate a read context and write
// results to write context. This does not project columns prior to running WHERE.
//
// @writeContext = Write out results of projection
// @readContext = Message to evaluate does it match where clause? if so proceed to projection
func EvalSql(sel *rel.SqlSelect, writeContext expr.ContextWriter, readContext expr.EvalContext) (bool, error) {
// Check and see if we are where Guarded, which would discard the entire message
if sel.Where != nil {
whereValue, ok := Eval(readContext, sel.Where.Expr)
if !ok {
return false, nil
}
switch whereVal := whereValue.(type) {
case value.BoolValue:
if whereVal.Val() == false {
return false, nil
}
// ok, continue
default:
return false, nil
}
}
for _, col := range sel.Columns {
if col.Guard != nil {
ifColValue, ok := Eval(readContext, col.Guard)
if !ok {
continue
}
switch ifVal := ifColValue.(type) {
case value.BoolValue:
if ifVal.Val() == false {
continue // filter out this col
}
default:
continue // filter out
}
}
v, ok := Eval(readContext, col.Expr)
if !ok {
u.Debugf("Could not evaluate: %s ctx: %#v", col.Expr, readContext)
} else {
// Write out the result of the evaluation
writeContext.Put(col, readContext, v)
}
}
return true, nil
}