Skip to content

Commit e50d578

Browse files
author
Viktor Pentyukhov
committed
Added ALTER TABLE support
1 parent 14b60c5 commit e50d578

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

internal/engine/ydb/convert.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
470544
func (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

internal/engine/ydb/utils.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func parseIdOrType(ctx parser.IId_or_typeContext) string {
8585
}
8686
Id := ctx.(*parser.Id_or_typeContext)
8787
if Id.Id() != nil {
88-
return identifier(parseIdTable(Id.Id()))
88+
return identifier(parseId(Id.Id()))
8989
}
9090

9191
return ""
@@ -112,13 +112,25 @@ func parseAnIdSchema(ctx parser.IAn_id_schemaContext) string {
112112
return ""
113113
}
114114

115-
func parseIdTable(ctx parser.IIdContext) string {
115+
func parseId(ctx parser.IIdContext) string {
116116
if ctx == nil {
117117
return ""
118118
}
119119
return ctx.GetText()
120120
}
121121

122+
func parseAnIdTable(ctx parser.IAn_id_tableContext) string {
123+
if ctx == nil {
124+
return ""
125+
}
126+
if id := ctx.Id_table(); id != nil {
127+
return id.GetText()
128+
} else if str := ctx.STRING_VALUE(); str != nil {
129+
return str.GetText()
130+
}
131+
return ""
132+
}
133+
122134
func parseIntegerValue(text string) (int64, error) {
123135
text = strings.ToLower(text)
124136
base := 10

0 commit comments

Comments
 (0)