-
Notifications
You must be signed in to change notification settings - Fork 33
/
node.go
277 lines (220 loc) · 6.07 KB
/
node.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
package ast
import (
"fmt"
"reflect"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
)
// Node is an AST node.
type Node interface {
// Parent returns the node's parent, which may be nil..
Parent() Node
// SetParent sets the node's parent, returning an error if illegal.
SetParent(n Node) error
// Children returns the node's children (may be empty).
Children() []Node
// SetChildren sets the node's children, returning an error if illegal.
SetChildren(children []Node) error
// AddChild adds a child node, returning an error if illegal.
AddChild(child Node) error
// Context returns the parse tree context.
Context() antlr.ParseTree
// SetContext sets the parse tree context, returning an error if illegal.
SetContext(ctx antlr.ParseTree) error
// String returns a debug-friendly string representation.
String() string
// Text returns the node's text representation.
Text() string
}
// Selectable is a marker interface to indicate that the node can be
// selected from. That is, the node represents a SQL table, view, or
// join table, and can be used like "SELECT * FROM [selectable]".
type Selectable interface {
Selectable()
}
// ColExpr indicates a column selection expression such as a
// column name, or context-appropriate function, e.g. "COUNT(*)".
type ColExpr interface {
// IsColName returns true if the expr is a column name, e.g. "uid" or "users.uid".
IsColName() bool
ColExpr() (string, error)
String() string
}
// baseNode is a base implementation of Node.
type baseNode struct {
parent Node
children []Node
ctx antlr.ParseTree
}
func (bn *baseNode) Parent() Node {
return bn.parent
}
func (bn *baseNode) SetParent(parent Node) error {
bn.parent = parent
return nil
}
func (bn *baseNode) Children() []Node {
return bn.children
}
func (bn *baseNode) AddChild(child Node) error {
return errorf(msgNodeNoAddChild, bn, child)
}
func (bn *baseNode) addChild(child Node) {
bn.children = append(bn.children, child)
}
func (bn *baseNode) SetChildren(children []Node) error {
return errorf(msgNodeNoAddChildren, bn, len(children))
}
func (bn *baseNode) setChildren(children []Node) {
bn.children = children
}
func (bn *baseNode) Text() string {
if bn.ctx == nil {
return ""
}
return bn.ctx.GetText()
}
func (bn *baseNode) Context() antlr.ParseTree {
return bn.ctx
}
func (bn *baseNode) SetContext(ctx antlr.ParseTree) error {
bn.ctx = ctx
return nil
}
// nodeString returns a default value suitable for use by Node.String().
func nodeString(n Node) string {
return fmt.Sprintf("%T: %s", n, n.Text())
}
// replaceNode replaces old with new. That is, nu becomes a child
// of old's parent.
func replaceNode(old, nu Node) error {
err := nu.SetContext(old.Context())
if err != nil {
return err
}
parent := old.Parent()
index := childIndex(parent, old)
if index < 0 {
return errorf("parent %T(%q) does not appear to have child %T(%q)", parent, parent.Text(), old, old.Text())
}
siblings := parent.Children()
siblings[index] = nu
return parent.SetChildren(siblings)
}
// childIndex returns the index of child in parent's children, or -1.
func childIndex(parent, child Node) int {
index := -1
for i, node := range parent.Children() {
if node == child {
index = i
break
}
}
return index
}
// nodesWithType returns a new slice containing each member of nodes that is
// of the specified type.
func nodesWithType(nodes []Node, typ reflect.Type) []Node {
s := make([]Node, 0)
for _, n := range nodes {
if reflect.TypeOf(n) == typ {
s = append(s, n)
}
}
return s
}
// Terminal is a terminal/leaf node that typically is interpreted simply as its
// text value.
type Terminal struct {
baseNode
}
func (t *Terminal) String() string {
return nodeString(t)
}
// Group models GROUP BY.
type Group struct {
baseNode
}
func (g *Group) AddChild(child Node) error {
_, ok := child.(*ColSelector)
if !ok {
return errorf("GROUP() only accepts children of type %s, but got %T", typeColSelector, child)
}
g.addChild(child)
return child.SetParent(g)
}
func (g *Group) String() string {
text := nodeString(g)
return text
}
// Expr models a SLQ expression such as ".uid > 4".
type Expr struct {
baseNode
}
func (e *Expr) AddChild(child Node) error {
e.addChild(child)
return child.SetParent(e)
}
func (e *Expr) String() string {
text := nodeString(e)
return text
}
// Operator is a leaf node in an expression representing an operator such as ">" or "==".
type Operator struct {
baseNode
}
func (o *Operator) String() string {
return nodeString(o)
}
// Literal is a leaf node representing a literal such as a number or a string.
type Literal struct {
baseNode
}
func (li *Literal) String() string {
return nodeString(li)
}
// Where represents a SQL WHERE clause, i.e. a filter on the SELECT.
type Where struct {
baseNode
}
func (w *Where) String() string {
return nodeString(w)
}
// Expr returns the expression that constitutes the SetWhere clause, or nil if no expression.
func (w *Where) Expr() *Expr {
if len(w.children) == 0 {
return nil
}
return w.children[0].(*Expr)
}
func (w *Where) AddChild(node Node) error {
expr, ok := node.(*Expr)
if !ok {
return errorf("WHERE child must be *Expr, but got: %T", node)
}
if len(w.children) > 0 {
return errorf("WHERE has max 1 child: failed to add: %T", node)
}
w.addChild(expr)
return nil
}
// isOperator returns true if the supplied string is a recognized operator, e.g. "!=" or ">".
func isOperator(text string) bool {
switch text {
case "-", "+", "~", "!", "||", "*", "/", "%", "<<", ">>", "&", "<", "<=", ">", ">=", "==", "!=", "&&":
return true
default:
return false
}
}
// Cached results from reflect.TypeOf for node types.
var (
typeAST = reflect.TypeOf((*AST)(nil))
typeDatasource = reflect.TypeOf((*Datasource)(nil))
typeSegment = reflect.TypeOf((*Segment)(nil))
typeJoin = reflect.TypeOf((*Join)(nil))
typeSelector = reflect.TypeOf((*Selector)(nil))
typeColSelector = reflect.TypeOf((*ColSelector)(nil))
typeTblSelector = reflect.TypeOf((*TblSelector)(nil))
typeRowRange = reflect.TypeOf((*RowRange)(nil))
typeExpr = reflect.TypeOf((*Expr)(nil))
)