@@ -20,12 +20,12 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
20
20
}
21
21
}
22
22
23
- if newCol , ok := c .New_column_name ().(* parser.New_column_nameContext ); ok {
23
+ if newCol , ok := c .GetNew_column_name ().(* parser.Column_nameContext ); ok {
24
24
name := newCol .Any_name ().GetText ()
25
25
return & ast.RenameColumnStmt {
26
26
Table : parseTableName (c ),
27
27
Col : & ast.ColumnRef {
28
- Name : c .Column_name ().GetText (),
28
+ Name : c .GetOld_column_name ().GetText (),
29
29
},
30
30
NewName : & name ,
31
31
}
@@ -54,7 +54,7 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
54
54
}
55
55
56
56
func convertAttach_stmtContext (c * parser.Attach_stmtContext ) ast.Node {
57
- name := c .Database_name ().GetText ()
57
+ name := c .Schema_name ().GetText ()
58
58
return & ast.CreateSchemaStmt {
59
59
Name : & name ,
60
60
}
@@ -63,7 +63,7 @@ func convertAttach_stmtContext(c *parser.Attach_stmtContext) ast.Node {
63
63
func convertCreate_table_stmtContext (c * parser.Create_table_stmtContext ) ast.Node {
64
64
stmt := & ast.CreateTableStmt {
65
65
Name : parseTableName (c ),
66
- IfNotExists : c .K_EXISTS () != nil ,
66
+ IfNotExists : c .EXISTS_ () != nil ,
67
67
}
68
68
for _ , idef := range c .AllColumn_def () {
69
69
if def , ok := idef .(* parser.Column_defContext ); ok {
@@ -77,76 +77,115 @@ func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Nod
77
77
return stmt
78
78
}
79
79
80
- func convertDrop_table_stmtContext (c * parser.Drop_table_stmtContext ) ast.Node {
81
- return & ast.DropTableStmt {
82
- IfExists : c .K_EXISTS () != nil ,
83
- Tables : []* ast.TableName {parseTableName (c )},
80
+ func convertDrop_stmtContext (c * parser.Drop_stmtContext ) ast.Node {
81
+ // TODO confirm that this logic does what it looks like it should
82
+ if tableName , ok := c .TABLE_ ().(antlr.TerminalNode ); ok {
83
+
84
+ name := ast.TableName {
85
+ Name : tableName .GetText (),
86
+ }
87
+ if c .Schema_name () != nil {
88
+ name .Schema = c .Schema_name ().GetText ()
89
+ }
90
+
91
+ return & ast.DropTableStmt {
92
+ IfExists : c .EXISTS_ () != nil ,
93
+ Tables : []* ast.TableName {& name },
94
+ }
95
+ } else {
96
+ return & ast.TODO {}
84
97
}
85
98
}
86
99
87
100
func convertExprContext (c * parser.ExprContext ) ast.Node {
88
101
return & ast.TODO {}
89
102
}
90
103
91
- func convertFactored_select_stmtContext (c * parser.Factored_select_stmtContext ) ast.Node {
104
+ func convertSimpleSelect_stmtContext (c * parser.Simple_select_stmtContext ) ast.Node {
105
+ if core , ok := c .Select_core ().(* parser.Select_coreContext ); ok {
106
+ cols := getCols (core )
107
+ tables := getTables (core )
108
+
109
+ return & ast.SelectStmt {
110
+ FromClause : & ast.List {Items : tables },
111
+ TargetList : & ast.List {Items : cols },
112
+ }
113
+ }
114
+
115
+ return & ast.TODO {}
116
+ }
117
+
118
+ func convertMultiSelect_stmtContext (c multiselect ) ast.Node {
92
119
var tables []ast.Node
93
120
var cols []ast.Node
94
121
for _ , icore := range c .AllSelect_core () {
95
122
core , ok := icore .(* parser.Select_coreContext )
96
123
if ! ok {
97
124
continue
98
125
}
99
- for _ , icol := range core .AllResult_column () {
100
- col , ok := icol .(* parser.Result_columnContext )
101
- if ! ok {
102
- continue
103
- }
104
- var val ast.Node
105
- iexpr := col .Expr ()
106
- switch {
107
- case col .STAR () != nil :
108
- val = & ast.ColumnRef {
109
- Fields : & ast.List {
110
- Items : []ast.Node {
111
- & ast.A_Star {},
112
- },
113
- },
114
- Location : col .GetStart ().GetStart (),
115
- }
116
- case iexpr != nil :
117
- val = convert (iexpr )
118
- }
119
- if val == nil {
120
- continue
121
- }
122
- cols = append (cols , & ast.ResTarget {
123
- Val : val ,
124
- Location : col .GetStart ().GetStart (),
125
- })
126
- }
127
- for _ , ifrom := range core .AllTable_or_subquery () {
128
- from , ok := ifrom .(* parser.Table_or_subqueryContext )
129
- if ! ok {
130
- continue
131
- }
132
- rel := from .Table_name ().GetText ()
133
- name := ast.RangeVar {
134
- Relname : & rel ,
135
- Location : from .GetStart ().GetStart (),
136
- }
137
- if from .Schema_name () != nil {
138
- text := from .Schema_name ().GetText ()
139
- name .Schemaname = & text
140
- }
141
- tables = append (tables , & name )
142
- }
126
+ cols = append (cols , getCols (core )... )
127
+ tables = append (cols , getTables (core )... )
143
128
}
144
129
return & ast.SelectStmt {
145
130
FromClause : & ast.List {Items : tables },
146
131
TargetList : & ast.List {Items : cols },
147
132
}
148
133
}
149
134
135
+ func getTables (core * parser.Select_coreContext ) []ast.Node {
136
+ var tables []ast.Node
137
+ for _ , ifrom := range core .AllTable_or_subquery () {
138
+ from , ok := ifrom .(* parser.Table_or_subqueryContext )
139
+ if ! ok {
140
+ continue
141
+ }
142
+ rel := from .Table_name ().GetText ()
143
+ name := ast.RangeVar {
144
+ Relname : & rel ,
145
+ Location : from .GetStart ().GetStart (),
146
+ }
147
+ if from .Schema_name () != nil {
148
+ text := from .Schema_name ().GetText ()
149
+ name .Schemaname = & text
150
+ }
151
+ tables = append (tables , & name )
152
+ }
153
+ return tables
154
+ }
155
+
156
+ func getCols (core * parser.Select_coreContext ) []ast.Node {
157
+ var cols []ast.Node
158
+ for _ , icol := range core .AllResult_column () {
159
+ col , ok := icol .(* parser.Result_columnContext )
160
+ if ! ok {
161
+ continue
162
+ }
163
+ var val ast.Node
164
+ iexpr := col .Expr ()
165
+ switch {
166
+ case col .STAR () != nil :
167
+ val = & ast.ColumnRef {
168
+ Fields : & ast.List {
169
+ Items : []ast.Node {
170
+ & ast.A_Star {},
171
+ },
172
+ },
173
+ Location : col .GetStart ().GetStart (),
174
+ }
175
+ case iexpr != nil :
176
+ val = convert (iexpr )
177
+ }
178
+ if val == nil {
179
+ continue
180
+ }
181
+ cols = append (cols , & ast.ResTarget {
182
+ Val : val ,
183
+ Location : col .GetStart ().GetStart (),
184
+ })
185
+ }
186
+ return cols
187
+ }
188
+
150
189
func convertSql_stmtContext (n * parser.Sql_stmtContext ) ast.Node {
151
190
if stmt := n .Alter_table_stmt (); stmt != nil {
152
191
return convert (stmt )
@@ -163,9 +202,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
163
202
if stmt := n .Commit_stmt (); stmt != nil {
164
203
return convert (stmt )
165
204
}
166
- if stmt := n .Compound_select_stmt (); stmt != nil {
167
- return convert (stmt )
168
- }
169
205
if stmt := n .Create_index_stmt (); stmt != nil {
170
206
return convert (stmt )
171
207
}
@@ -190,19 +226,7 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
190
226
if stmt := n .Detach_stmt (); stmt != nil {
191
227
return convert (stmt )
192
228
}
193
- if stmt := n .Drop_index_stmt (); stmt != nil {
194
- return convert (stmt )
195
- }
196
- if stmt := n .Drop_table_stmt (); stmt != nil {
197
- return convert (stmt )
198
- }
199
- if stmt := n .Drop_trigger_stmt (); stmt != nil {
200
- return convert (stmt )
201
- }
202
- if stmt := n .Drop_view_stmt (); stmt != nil {
203
- return convert (stmt )
204
- }
205
- if stmt := n .Factored_select_stmt (); stmt != nil {
229
+ if stmt := n .Drop_stmt (); stmt != nil {
206
230
return convert (stmt )
207
231
}
208
232
if stmt := n .Insert_stmt (); stmt != nil {
@@ -223,9 +247,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
223
247
if stmt := n .Savepoint_stmt (); stmt != nil {
224
248
return convert (stmt )
225
249
}
226
- if stmt := n .Simple_select_stmt (); stmt != nil {
227
- return convert (stmt )
228
- }
229
250
if stmt := n .Select_stmt (); stmt != nil {
230
251
return convert (stmt )
231
252
}
@@ -253,18 +274,28 @@ func convert(node node) ast.Node {
253
274
case * parser.Create_table_stmtContext :
254
275
return convertCreate_table_stmtContext (n )
255
276
256
- case * parser.Drop_table_stmtContext :
257
- return convertDrop_table_stmtContext (n )
277
+ case * parser.Drop_stmtContext :
278
+ return convertDrop_stmtContext (n )
258
279
259
280
case * parser.ExprContext :
260
281
return convertExprContext (n )
261
282
262
283
case * parser.Factored_select_stmtContext :
263
- return convertFactored_select_stmtContext (n )
284
+ // TODO: need to handle this
285
+ return & ast.TODO {}
286
+
287
+ case * parser.Select_stmtContext :
288
+ return convertMultiSelect_stmtContext (n )
264
289
265
290
case * parser.Sql_stmtContext :
266
291
return convertSql_stmtContext (n )
267
292
293
+ case * parser.Simple_select_stmtContext :
294
+ return convertSimpleSelect_stmtContext (n )
295
+
296
+ case * parser.Compound_select_stmtContext :
297
+ return convertMultiSelect_stmtContext (n )
298
+
268
299
default :
269
300
return & ast.TODO {}
270
301
}
0 commit comments