-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
base.go
135 lines (111 loc) · 3.05 KB
/
base.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
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package ast
import (
"sync"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/types"
)
// node is the struct implements Node interface except for Accept method.
// Node implementations should embed it in.
type node struct {
utf8Text string
enc charset.Encoding
once *sync.Once
text string
offset int
}
// SetOriginTextPosition implements Node interface.
func (n *node) SetOriginTextPosition(offset int) {
n.offset = offset
}
// OriginTextPosition implements Node interface.
func (n *node) OriginTextPosition() int {
return n.offset
}
// SetText implements Node interface.
func (n *node) SetText(enc charset.Encoding, text string) {
n.enc = enc
n.text = text
n.once = &sync.Once{}
}
// Text implements Node interface.
func (n *node) Text() string {
if n.once == nil {
return n.text
}
n.once.Do(func() {
if n.enc == nil {
n.utf8Text = n.text
return
}
utf8Lit, _ := n.enc.Transform(nil, charset.HackSlice(n.text), charset.OpDecodeReplace)
n.utf8Text = charset.HackString(utf8Lit)
})
return n.utf8Text
}
// OriginalText implements Node interface.
func (n *node) OriginalText() string {
return n.text
}
// stmtNode implements StmtNode interface.
// Statement implementations should embed it in.
type stmtNode struct {
node
}
// statement implements StmtNode interface.
func (sn *stmtNode) statement() {}
// ddlNode implements DDLNode interface.
// DDL implementations should embed it in.
type ddlNode struct {
stmtNode
}
// ddlStatement implements DDLNode interface.
func (dn *ddlNode) ddlStatement() {}
// dmlNode is the struct implements DMLNode interface.
// DML implementations should embed it in.
type dmlNode struct {
stmtNode
}
// dmlStatement implements DMLNode interface.
func (dn *dmlNode) dmlStatement() {}
// exprNode is the struct implements Expression interface.
// Expression implementations should embed it in.
type exprNode struct {
node
Type types.FieldType
flag uint64
}
// TexprNode is exported for parser driver.
type TexprNode = exprNode
// SetType implements ExprNode interface.
func (en *exprNode) SetType(tp *types.FieldType) {
en.Type = *tp
}
// GetType implements ExprNode interface.
func (en *exprNode) GetType() *types.FieldType {
return &en.Type
}
// SetFlag implements ExprNode interface.
func (en *exprNode) SetFlag(flag uint64) {
en.flag = flag
}
// GetFlag implements ExprNode interface.
func (en *exprNode) GetFlag() uint64 {
return en.flag
}
type funcNode struct {
exprNode
}
// functionExpression implements FunctionNode interface.
func (fn *funcNode) functionExpression() {}