-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
213 lines (197 loc) · 5.84 KB
/
table.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
package sqlparser
import (
"fmt"
"github.com/viant/parsly"
"github.com/viant/sqlparser/column"
del "github.com/viant/sqlparser/delete"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/insert"
"github.com/viant/sqlparser/node"
"github.com/viant/sqlparser/query"
"github.com/viant/sqlparser/table"
"github.com/viant/sqlparser/update"
)
// TableName returns main table name
func TableName(node node.Node) string {
switch actual := node.(type) {
case *query.Select:
return queryTableName(actual)
case *insert.Statement:
return trimEnclosure(actual.Target.X)
case *update.Statement:
return trimEnclosure(actual.Target.X)
case *del.Statement:
return trimEnclosure(actual.Target.X)
case *table.Create:
return trimEnclosure(actual.Spec.Name)
case *table.Drop:
return trimEnclosure(actual.Name)
}
return ""
}
func queryTableName(sel *query.Select) string {
if sel.From.X == nil {
return ""
}
switch actual := sel.From.X.(type) {
case *expr.Ident:
return actual.Name
case *expr.Selector:
return trimEnclosure(actual)
case *expr.Parenthesis:
raw := trimEnclosure(actual.Raw)
if sel, _ := ParseQuery(raw); sel != nil {
actual.X = sel
return TableName(sel)
}
case *expr.Raw:
if actual.X != nil {
return TableName(actual.X)
}
default:
panic(fmt.Sprintf("not supported:%T ", actual))
}
return ""
}
func trimEnclosure(node node.Node) string {
if node == nil {
return ""
}
var name string
var ok bool
if name, ok = node.(string); !ok {
name = Stringify(node)
}
switch name[0] {
case '`', '"', '[', '\'', '(':
name = name[1 : len(name)-1]
}
return name
}
// ParseTruncateTable parses truncate table
func ParseTruncateTable(SQL string) (*table.Truncate, error) {
result := &table.Truncate{}
SQL = removeSQLComments(SQL)
cursor := parsly.NewCursor("", []byte(SQL), 0)
err := parseTruncateTable(cursor, result)
if err != nil {
return result, fmt.Errorf("%s", SQL)
}
return result, err
}
func parseTruncateTable(cursor *parsly.Cursor, result *table.Truncate) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, truncateKeywordMatcher)
if match.Code != truncateTableKeyword {
return cursor.NewError(truncateKeywordMatcher)
}
if match = cursor.MatchOne(whitespaceMatcher); match.Code != whitespaceCode {
return cursor.NewError(whitespaceMatcher)
}
match = cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
if match.Code != identifierCode {
return cursor.NewError(identifierMatcher)
}
result.Table = match.Text(cursor)
return nil
}
// ParseDropTable parses drop table
func ParseDropTable(SQL string) (*table.Drop, error) {
result := &table.Drop{}
SQL = removeSQLComments(SQL)
cursor := parsly.NewCursor("", []byte(SQL), 0)
err := parseDropTable(cursor, result)
if err != nil {
return result, fmt.Errorf("%s", SQL)
}
return result, err
}
func parseDropTable(cursor *parsly.Cursor, dest *table.Drop) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, dropTableMatcher)
if match.Code != dropTableToken {
return cursor.NewError(createTableMatcher)
}
if match = cursor.MatchOne(whitespaceMatcher); match.Code != whitespaceCode {
return cursor.NewError(whitespaceMatcher)
}
if match = cursor.MatchOne(ifExistsMatcher); match.Code == ifExistsToken {
dest.IfExists = true
}
match = cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
if match.Code != identifierCode {
return cursor.NewError(ifNotExistsMatcher)
}
dest.Name = match.Text(cursor)
return nil
}
// ParseCreateTable parses create table
func ParseCreateTable(SQL string) (*table.Create, error) {
result := &table.Create{}
SQL = removeSQLComments(SQL)
cursor := parsly.NewCursor("", []byte(SQL), 0)
err := parseCreateTable(cursor, result)
if err != nil {
return result, fmt.Errorf("%s", SQL)
}
return result, err
}
func parseCreateTable(cursor *parsly.Cursor, dest *table.Create) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, createTableMatcher)
if match.Code != createTableToken {
return cursor.NewError(createTableMatcher)
}
if match = cursor.MatchOne(whitespaceMatcher); match.Code != whitespaceCode {
return cursor.NewError(whitespaceMatcher)
}
if match = cursor.MatchOne(ifNotExistsMatcher); match.Code == ifNotExistsToken {
dest.IfDoesExists = true
}
match = cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
if match.Code != identifierCode {
return cursor.NewError(ifNotExistsMatcher)
}
dest.Spec.Name = match.Text(cursor)
pos := cursor.Pos
match = cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher)
if match.Code != parenthesesCode {
return cursor.NewError(parenthesesMatcher)
}
columnSpec := match.Text(cursor)
columnCursor := parsly.NewCursor(cursor.Path, []byte(columnSpec[1:len(columnSpec)-1]), pos)
return parseColumnSpec(dest, match, columnCursor)
}
func parseColumnSpec(dest *table.Create, match *parsly.TokenMatch, cursor *parsly.Cursor) error {
for {
col := &column.Spec{}
dest.Spec.Columns = append(dest.Spec.Columns, col)
match = cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
if match.Code != identifierCode {
return cursor.NewError(ifNotExistsMatcher)
}
col.Name = match.Text(cursor)
node, err := expectOperand(cursor)
if err != nil {
return err
}
col.Type = Stringify(node)
match = cursor.MatchAfterOptional(whitespaceMatcher, keyMatcher)
if match.Code == keyTokenCode {
col.Key = match.Text(cursor)
}
match = cursor.MatchAfterOptional(whitespaceMatcher, notNullMatcher)
col.IsNullable = match.Code != notNullToken
match = cursor.MatchAfterOptional(whitespaceMatcher, defaultMatcher)
if match.Code == defaultToken {
node, err := expectOperand(cursor)
if err != nil {
return err
}
def := Stringify(node)
col.Default = &def
}
match = cursor.MatchAfterOptional(whitespaceMatcher, nextMatcher)
if match.Code != nextCode {
break
}
}
return nil
}