@@ -20,12 +20,12 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
2020 }
2121 }
2222
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 {
2424 name := newCol .Any_name ().GetText ()
2525 return & ast.RenameColumnStmt {
2626 Table : parseTableName (c ),
2727 Col : & ast.ColumnRef {
28- Name : c .Column_name ().GetText (),
28+ Name : c .GetOld_column_name ().GetText (),
2929 },
3030 NewName : & name ,
3131 }
@@ -54,7 +54,7 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
5454}
5555
5656func convertAttach_stmtContext (c * parser.Attach_stmtContext ) ast.Node {
57- name := c .Database_name ().GetText ()
57+ name := c .Schema_name ().GetText ()
5858 return & ast.CreateSchemaStmt {
5959 Name : & name ,
6060 }
@@ -63,7 +63,7 @@ func convertAttach_stmtContext(c *parser.Attach_stmtContext) ast.Node {
6363func convertCreate_table_stmtContext (c * parser.Create_table_stmtContext ) ast.Node {
6464 stmt := & ast.CreateTableStmt {
6565 Name : parseTableName (c ),
66- IfNotExists : c .K_EXISTS () != nil ,
66+ IfNotExists : c .EXISTS_ () != nil ,
6767 }
6868 for _ , idef := range c .AllColumn_def () {
6969 if def , ok := idef .(* parser.Column_defContext ); ok {
@@ -77,76 +77,115 @@ func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Nod
7777 return stmt
7878}
7979
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 {}
8497 }
8598}
8699
87100func convertExprContext (c * parser.ExprContext ) ast.Node {
88101 return & ast.TODO {}
89102}
90103
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 {
92119 var tables []ast.Node
93120 var cols []ast.Node
94121 for _ , icore := range c .AllSelect_core () {
95122 core , ok := icore .(* parser.Select_coreContext )
96123 if ! ok {
97124 continue
98125 }
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 )... )
143128 }
144129 return & ast.SelectStmt {
145130 FromClause : & ast.List {Items : tables },
146131 TargetList : & ast.List {Items : cols },
147132 }
148133}
149134
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+
150189func convertSql_stmtContext (n * parser.Sql_stmtContext ) ast.Node {
151190 if stmt := n .Alter_table_stmt (); stmt != nil {
152191 return convert (stmt )
@@ -163,9 +202,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
163202 if stmt := n .Commit_stmt (); stmt != nil {
164203 return convert (stmt )
165204 }
166- if stmt := n .Compound_select_stmt (); stmt != nil {
167- return convert (stmt )
168- }
169205 if stmt := n .Create_index_stmt (); stmt != nil {
170206 return convert (stmt )
171207 }
@@ -190,19 +226,7 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
190226 if stmt := n .Detach_stmt (); stmt != nil {
191227 return convert (stmt )
192228 }
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 {
206230 return convert (stmt )
207231 }
208232 if stmt := n .Insert_stmt (); stmt != nil {
@@ -223,9 +247,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
223247 if stmt := n .Savepoint_stmt (); stmt != nil {
224248 return convert (stmt )
225249 }
226- if stmt := n .Simple_select_stmt (); stmt != nil {
227- return convert (stmt )
228- }
229250 if stmt := n .Select_stmt (); stmt != nil {
230251 return convert (stmt )
231252 }
@@ -253,18 +274,28 @@ func convert(node node) ast.Node {
253274 case * parser.Create_table_stmtContext :
254275 return convertCreate_table_stmtContext (n )
255276
256- case * parser.Drop_table_stmtContext :
257- return convertDrop_table_stmtContext (n )
277+ case * parser.Drop_stmtContext :
278+ return convertDrop_stmtContext (n )
258279
259280 case * parser.ExprContext :
260281 return convertExprContext (n )
261282
262283 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 )
264289
265290 case * parser.Sql_stmtContext :
266291 return convertSql_stmtContext (n )
267292
293+ case * parser.Simple_select_stmtContext :
294+ return convertSimpleSelect_stmtContext (n )
295+
296+ case * parser.Compound_select_stmtContext :
297+ return convertMultiSelect_stmtContext (n )
298+
268299 default :
269300 return & ast.TODO {}
270301 }
0 commit comments