@@ -467,6 +467,80 @@ func (c *cc) convertRollback_stmtContext(n *parser.Rollback_stmtContext) ast.Nod
467467 return todo ("convertRollback_stmtContext" , n )
468468}
469469
470+ func (c * cc ) convertAlter_table_stmtContext (n * parser.Alter_table_stmtContext ) ast.Node {
471+ if n .ALTER () == nil || n .TABLE () == nil || n .Simple_table_ref () == nil || len (n .AllAlter_table_action ()) == 0 {
472+ return todo ("convertAlter_table_stmtContext" , n )
473+ }
474+
475+ stmt := & ast.AlterTableStmt {
476+ Table : parseTableName (n .Simple_table_ref ().Simple_table_ref_core ()),
477+ Cmds : & ast.List {},
478+ }
479+
480+ for _ , action := range n .AllAlter_table_action () {
481+ if action == nil {
482+ continue
483+ }
484+
485+ switch {
486+ case action .Alter_table_add_column () != nil :
487+ ac := action .Alter_table_add_column ()
488+ if ac .ADD () != nil && ac .Column_schema () != nil {
489+ columnDef := c .convertColumnSchema (ac .Column_schema ().(* parser.Column_schemaContext ))
490+ stmt .Cmds .Items = append (stmt .Cmds .Items , & ast.AlterTableCmd {
491+ Name : & columnDef .Colname ,
492+ Subtype : ast .AT_AddColumn ,
493+ Def : columnDef ,
494+ })
495+ }
496+ case action .Alter_table_drop_column () != nil :
497+ ac := action .Alter_table_drop_column ()
498+ if ac .DROP () != nil && ac .An_id () != nil {
499+ name := parseAnId (ac .An_id ())
500+ stmt .Cmds .Items = append (stmt .Cmds .Items , & ast.AlterTableCmd {
501+ Name : & name ,
502+ Subtype : ast .AT_DropColumn ,
503+ })
504+ }
505+ case action .Alter_table_alter_column_drop_not_null () != nil :
506+ ac := action .Alter_table_alter_column_drop_not_null ()
507+ if ac .DROP () != nil && ac .NOT () != nil && ac .NULL () != nil && ac .An_id () != nil {
508+ name := parseAnId (ac .An_id ())
509+ stmt .Cmds .Items = append (stmt .Cmds .Items , & ast.AlterTableCmd {
510+ Name : & name ,
511+ Subtype : ast .AT_DropNotNull ,
512+ })
513+ }
514+ case action .Alter_table_rename_to () != nil :
515+ ac := action .Alter_table_rename_to ()
516+ if ac .RENAME () != nil && ac .TO () != nil && ac .An_id_table () != nil {
517+ // TODO: Returning here may be incorrect if there are multiple specs
518+ newName := parseAnIdTable (ac .An_id_table ())
519+ return & ast.RenameTableStmt {
520+ Table : stmt .Table ,
521+ NewName : & newName ,
522+ }
523+ }
524+ case action .Alter_table_add_index () != nil ,
525+ action .Alter_table_drop_index () != nil ,
526+ action .Alter_table_add_column_family () != nil ,
527+ action .Alter_table_alter_column_family () != nil ,
528+ action .Alter_table_set_table_setting_uncompat () != nil ,
529+ action .Alter_table_set_table_setting_compat () != nil ,
530+ action .Alter_table_reset_table_setting () != nil ,
531+ action .Alter_table_add_changefeed () != nil ,
532+ action .Alter_table_alter_changefeed () != nil ,
533+ action .Alter_table_drop_changefeed () != nil ,
534+ action .Alter_table_rename_index_to () != nil ,
535+ action .Alter_table_alter_index () != nil :
536+ // All these actions do not change column schema relevant to sqlc; no-op.
537+ // Intentionally ignored.
538+ }
539+ }
540+
541+ return stmt
542+ }
543+
470544func (c * cc ) convertDrop_table_stmtContext (n * parser.Drop_table_stmtContext ) ast.Node {
471545 if n .DROP () != nil && (n .TABLESTORE () != nil || (n .EXTERNAL () != nil && n .TABLE () != nil ) || n .TABLE () != nil ) {
472546 name := parseTableName (n .Simple_table_ref ().Simple_table_ref_core ())
@@ -2553,6 +2627,9 @@ func (c *cc) convert(node node) ast.Node {
25532627 case * parser.Update_stmtContext :
25542628 return c .convertUpdate_stmtContext (n )
25552629
2630+ case * parser.Alter_table_stmtContext :
2631+ return c .convertAlter_table_stmtContext (n )
2632+
25562633 case * parser.Drop_table_stmtContext :
25572634 return c .convertDrop_table_stmtContext (n )
25582635
0 commit comments