-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
244 lines (204 loc) · 5.94 KB
/
helpers.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
package sqlparser
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Visit defines the signature of a function that
// can be used to visit all nodes of a parse tree.
type Visit func(node Node) (stop bool, err error)
// Walk calls visit on every node.
// If visit returns false, the underlying nodes
// are also visited. If it returns an error, walking
// is interrupted, and the error is returned.
func Walk(visit Visit, nodes ...Node) error {
for _, node := range nodes {
if node == nil {
continue
}
stop, err := visit(node)
if err != nil {
return err
}
if !stop {
if err = node.walkSubtree(visit); err != nil {
return err
}
}
}
return nil
}
// GetUniqueTableReferences returns a slice of tables' names referenced by the node.
func GetUniqueTableReferences(node Node) []string {
if node == nil {
return []string{}
}
tables := map[string]struct{}{}
tableNames := []string{}
// it's ok to ignore the error because the visit function does not throw an error
_ = Walk(func(node Node) (bool, error) {
if table, ok := node.(*Table); ok && table != nil && table.IsTarget {
tableName := table.Name.String()
if _, ok := tables[tableName]; !ok {
tables[tableName] = struct{}{}
tableNames = append(tableNames, tableName)
}
}
return false, nil
}, node)
return tableNames
}
// ValidateTargetTables recursively validates all tables found in the node and return them.
func ValidateTargetTables(node Node) ([]*ValidatedTable, error) {
if node == nil {
return []*ValidatedTable{}, nil
}
tables := map[string]struct{}{}
validTables := []*ValidatedTable{}
err := Walk(func(node Node) (bool, error) {
if table, ok := node.(*Table); ok && table != nil && table.IsTarget {
tables[table.String()] = struct{}{}
validTable, err := ValidateTargetTable(table)
if err != nil {
return true, fmt.Errorf("validate: %s", err)
}
validTables = append(validTables, validTable)
}
return false, nil
}, node)
if err != nil {
return []*ValidatedTable{}, fmt.Errorf("walk subtree: %s", err)
}
return validTables, nil
}
var (
tableNameRegEx = regexp.MustCompile("^([A-Za-z]+[A-Za-z0-9_]*)*_[0-9]+_[0-9]+$")
createTableNameRegEx = regexp.MustCompile("^([A-Za-z]+[A-Za-z0-9_]*)*_[0-9]+$")
)
// ValidateTargetTable validates the tables' names of statements.
func ValidateTargetTable(table *Table) (*ValidatedTable, error) {
if !table.IsTarget {
return nil, fmt.Errorf("table is not target")
}
closingChar := map[byte]byte{
'"': '"',
'`': '`',
'[': ']',
}
tableName := table.String()
for start, end := range closingChar {
if tableName[0] == start && tableName[len(tableName)-1] == end {
tableName = tableName[1 : len(tableName)-1]
break
}
}
if !tableNameRegEx.MatchString(tableName) {
return nil, &ErrTableNameWrongFormat{Name: tableName}
}
parts := strings.Split(tableName, "_")
if len(parts) < 3 {
return nil, fmt.Errorf("not enough parts in the name")
}
prefix := strings.Join(parts[:len(parts)-2], "_")
chainIDstr := parts[len(parts)-2]
tokenIDstr := parts[len(parts)-1]
chainID, err := strconv.ParseInt(chainIDstr, 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing chain id in table name: %s", err)
}
tokenID, err := strconv.ParseInt(tokenIDstr, 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing token id in table name: %s", err)
}
return &ValidatedTable{name: table.String(), prefix: prefix, chainID: chainID, tokenID: tokenID}, nil
}
// ValidateCreateTargetTable validates the table name for CREATE statements.
func ValidateCreateTargetTable(table *Table) (*ValidatedCreateTable, error) {
if !table.IsTarget {
return nil, fmt.Errorf("table is not target")
}
closingChar := map[byte]byte{
'"': '"',
'`': '`',
'[': ']',
}
tableName := table.String()
for start, end := range closingChar {
if tableName[0] == start && tableName[len(tableName)-1] == end {
tableName = tableName[1 : len(tableName)-1]
break
}
}
if !createTableNameRegEx.MatchString(tableName) {
return nil, &ErrTableNameWrongFormat{Name: tableName}
}
parts := strings.Split(tableName, "_")
if len(parts) < 2 {
return nil, fmt.Errorf("not enough parts in the name")
}
prefix := strings.Join(parts[:len(parts)-1], "_")
chainIDstr := parts[len(parts)-1]
chainID, err := strconv.ParseInt(chainIDstr, 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing chain id in table name: %s", err)
}
return &ValidatedCreateTable{name: table.String(), prefix: prefix, chainID: chainID}, nil
}
// containsSubquery checks recursively if the node contains a subquery.
func containsSubquery(node Node) bool {
if node == nil {
return false
}
var containsSubquery bool
// it's ok to ignore the error because the visit function does not throw an error
_ = Walk(func(node Node) (bool, error) {
if _, ok := node.(*Subquery); ok {
containsSubquery = true
return true, nil
}
return false, nil
}, node)
return containsSubquery
}
// ValidatedTable is a Table that was validated by ValidateTargetTable.
type ValidatedTable struct {
name string
prefix string
chainID int64
tokenID int64
}
// Name returns the table's name.
func (node *ValidatedTable) Name() string {
return node.name
}
// ChainID returns the table's chain id.
func (node *ValidatedTable) ChainID() int64 {
return node.chainID
}
// TokenID returns the table's token id.
func (node *ValidatedTable) TokenID() int64 {
return node.tokenID
}
// Prefix returns table's prefix.
func (node *ValidatedTable) Prefix() string {
return node.prefix
}
// ValidatedCreateTable is a Table that was validated by ValidateCreateTargetTable.
type ValidatedCreateTable struct {
name string
prefix string
chainID int64
}
// Name returns the table's name.
func (node *ValidatedCreateTable) Name() string {
return node.name
}
// ChainID returns the table's chain id.
func (node *ValidatedCreateTable) ChainID() int64 {
return node.chainID
}
// Prefix returns table's prefix.
func (node *ValidatedCreateTable) Prefix() string {
return node.prefix
}