@@ -12,26 +12,47 @@ import (
12
12
)
13
13
14
14
var (
15
- sanitizeQuotesRegexp = regexp .MustCompile ("('[^']*')" )
16
- renameColumnRegexp = regexp .MustCompile (`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+` )
17
- dropColumnRegexp = regexp .MustCompile (`(?i)\bdrop\s+(column\s+|)([\S]+)$` )
18
- renameTableRegexp = regexp .MustCompile (`(?i)\brename\s+(to|as)\s+` )
15
+ sanitizeQuotesRegexp = regexp .MustCompile ("('[^']*')" )
16
+ renameColumnRegexp = regexp .MustCompile (`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+` )
17
+ dropColumnRegexp = regexp .MustCompile (`(?i)\bdrop\s+(column\s+|)([\S]+)$` )
18
+ renameTableRegexp = regexp .MustCompile (`(?i)\brename\s+(to|as)\s+` )
19
+ alterTableExplicitSchemaTableRegexps = []* regexp.Regexp {
20
+ regexp .MustCompile (`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)` ),
21
+ regexp .MustCompile (`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]([\S]+)\s+(.*$)` ),
22
+ regexp .MustCompile (`(?i)\balter\s+table\s+([\S]+)[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)` ),
23
+ regexp .MustCompile (`(?i)\balter\s+table\s+([\S]+)[.]([\S]+)\s+(.*$)` ),
24
+ }
25
+ alterTableExplicitTableRegexps = []* regexp.Regexp {
26
+ regexp .MustCompile (`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)` ),
27
+ regexp .MustCompile (`(?i)\balter\s+table\s+([\S]+)\s+(.*$)` ),
28
+ }
19
29
)
20
30
21
- type Parser struct {
31
+ type AlterTableParser struct {
22
32
columnRenameMap map [string ]string
23
33
droppedColumns map [string ]bool
24
34
isRenameTable bool
35
+
36
+ alterTokens []string
37
+
38
+ explicitSchema string
39
+ explicitTable string
25
40
}
26
41
27
- func NewParser () * Parser {
28
- return & Parser {
42
+ func NewAlterTableParser () * AlterTableParser {
43
+ return & AlterTableParser {
29
44
columnRenameMap : make (map [string ]string ),
30
45
droppedColumns : make (map [string ]bool ),
31
46
}
32
47
}
33
48
34
- func (this * Parser ) tokenizeAlterStatement (alterStatement string ) (tokens []string , err error ) {
49
+ func NewParserFromAlterStatement (alterStatement string ) * AlterTableParser {
50
+ parser := NewAlterTableParser ()
51
+ parser .ParseAlterStatement (alterStatement )
52
+ return parser
53
+ }
54
+
55
+ func (this * AlterTableParser ) tokenizeAlterStatement (alterStatement string ) (tokens []string , err error ) {
35
56
terminatingQuote := rune (0 )
36
57
f := func (c rune ) bool {
37
58
switch {
@@ -58,13 +79,13 @@ func (this *Parser) tokenizeAlterStatement(alterStatement string) (tokens []stri
58
79
return tokens , nil
59
80
}
60
81
61
- func (this * Parser ) sanitizeQuotesFromAlterStatement (alterStatement string ) (strippedStatement string ) {
82
+ func (this * AlterTableParser ) sanitizeQuotesFromAlterStatement (alterStatement string ) (strippedStatement string ) {
62
83
strippedStatement = alterStatement
63
84
strippedStatement = sanitizeQuotesRegexp .ReplaceAllString (strippedStatement , "''" )
64
85
return strippedStatement
65
86
}
66
87
67
- func (this * Parser ) parseAlterToken (alterToken string ) (err error ) {
88
+ func (this * AlterTableParser ) parseAlterToken (alterToken string ) (err error ) {
68
89
{
69
90
// rename
70
91
allStringSubmatch := renameColumnRegexp .FindAllStringSubmatch (alterToken , - 1 )
@@ -97,16 +118,33 @@ func (this *Parser) parseAlterToken(alterToken string) (err error) {
97
118
return nil
98
119
}
99
120
100
- func (this * Parser ) ParseAlterStatement (alterStatement string ) (err error ) {
121
+ func (this * AlterTableParser ) ParseAlterStatement (alterStatement string ) (err error ) {
122
+
123
+ for _ , alterTableRegexp := range alterTableExplicitSchemaTableRegexps {
124
+ if submatch := alterTableRegexp .FindStringSubmatch (alterStatement ); len (submatch ) > 0 {
125
+ this .explicitSchema = submatch [1 ]
126
+ this .explicitTable = submatch [2 ]
127
+ alterStatement = submatch [3 ]
128
+ break
129
+ }
130
+ }
131
+ for _ , alterTableRegexp := range alterTableExplicitTableRegexps {
132
+ if submatch := alterTableRegexp .FindStringSubmatch (alterStatement ); len (submatch ) > 0 {
133
+ this .explicitTable = submatch [1 ]
134
+ alterStatement = submatch [2 ]
135
+ break
136
+ }
137
+ }
101
138
alterTokens , _ := this .tokenizeAlterStatement (alterStatement )
102
139
for _ , alterToken := range alterTokens {
103
140
alterToken = this .sanitizeQuotesFromAlterStatement (alterToken )
104
141
this .parseAlterToken (alterToken )
142
+ this .alterTokens = append (this .alterTokens , alterToken )
105
143
}
106
144
return nil
107
145
}
108
146
109
- func (this * Parser ) GetNonTrivialRenames () map [string ]string {
147
+ func (this * AlterTableParser ) GetNonTrivialRenames () map [string ]string {
110
148
result := make (map [string ]string )
111
149
for column , renamed := range this .columnRenameMap {
112
150
if column != renamed {
@@ -116,14 +154,29 @@ func (this *Parser) GetNonTrivialRenames() map[string]string {
116
154
return result
117
155
}
118
156
119
- func (this * Parser ) HasNonTrivialRenames () bool {
157
+ func (this * AlterTableParser ) HasNonTrivialRenames () bool {
120
158
return len (this .GetNonTrivialRenames ()) > 0
121
159
}
122
160
123
- func (this * Parser ) DroppedColumnsMap () map [string ]bool {
161
+ func (this * AlterTableParser ) DroppedColumnsMap () map [string ]bool {
124
162
return this .droppedColumns
125
163
}
126
164
127
- func (this * Parser ) IsRenameTable () bool {
165
+ func (this * AlterTableParser ) IsRenameTable () bool {
128
166
return this .isRenameTable
129
167
}
168
+ func (this * AlterTableParser ) GetExplicitSchema () string {
169
+ return this .explicitSchema
170
+ }
171
+
172
+ func (this * AlterTableParser ) HasExplicitSchema () bool {
173
+ return this .GetExplicitSchema () != ""
174
+ }
175
+
176
+ func (this * AlterTableParser ) GetExplicitTable () string {
177
+ return this .explicitTable
178
+ }
179
+
180
+ func (this * AlterTableParser ) HasExplicitTable () bool {
181
+ return this .GetExplicitTable () != ""
182
+ }
0 commit comments