forked from src-d/go-mysql-server
/
walk.go
77 lines (65 loc) · 2.03 KB
/
walk.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
package plan
import (
"github.com/xsec-lab/go-mysql-server/sql"
"github.com/xsec-lab/go-mysql-server/sql/expression"
)
// Visitor visits nodes in the plan.
type Visitor interface {
// Visit method is invoked for each node encountered by Walk.
// If the result Visitor is not nul, Walk visits each of the children
// of the node with that visitor, followed by a call of Visit(nil)
// to the returned visitor.
Visit(node sql.Node) Visitor
}
// Walk traverses the plan tree in depth-first order. It starts by calling
// v.Visit(node); node must not be nil. If the visitor returned by
// v.Visit(node) is not nil, Walk is invoked recursively with the returned
// visitor for each children of the node, followed by a call of v.Visit(nil)
// to the returned visitor.
func Walk(v Visitor, node sql.Node) {
if v = v.Visit(node); v == nil {
return
}
for _, child := range node.Children() {
Walk(v, child)
}
v.Visit(nil)
}
type inspector func(sql.Node) bool
func (f inspector) Visit(node sql.Node) Visitor {
if f(node) {
return f
}
return nil
}
// Inspect traverses the plan in depth-first order: It starts by calling
// f(node); node must not be nil. If f returns true, Inspect invokes f
// recursively for each of the children of node, followed by a call of
// f(nil).
func Inspect(node sql.Node, f func(sql.Node) bool) {
Walk(inspector(f), node)
}
// WalkExpressions traverses the plan and calls expression.Walk on any
// expression it finds.
func WalkExpressions(v expression.Visitor, node sql.Node) {
Inspect(node, func(node sql.Node) bool {
if n, ok := node.(sql.Expressioner); ok {
for _, err := range n.Expressions() {
expression.Walk(v, err)
}
}
return true
})
}
// InspectExpressions traverses the plan and calls expression.Inspect on any
// expression it finds.
func InspectExpressions(node sql.Node, f func(sql.Expression) bool) {
WalkExpressions(exprInspector(f), node)
}
type exprInspector func(sql.Expression) bool
func (f exprInspector) Visit(e sql.Expression) expression.Visitor {
if f(e) {
return f
}
return nil
}