Skip to content

Commit

Permalink
[parser] parser: implement Restore for ColumnPosition (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony612 authored and ti-chi-bot committed Oct 9, 2021
1 parent ef194cc commit 8c816f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,20 @@ type ColumnPosition struct {

// Restore implements Node interface.
func (n *ColumnPosition) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
switch n.Tp {
case ColumnPositionNone:
// do nothing
case ColumnPositionFirst:
ctx.WriteKeyWord("FIRST")
case ColumnPositionAfter:
ctx.WriteKeyWord("AFTER ")
if err := n.RelativeColumn.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ColumnPosition.RelativeColumn")
}
default:
return errors.Errorf("invalid ColumnPositionType: %d", n.Tp)
}
return nil
}

// Accept implements Node Accept interface.
Expand Down
12 changes: 12 additions & 0 deletions parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,15 @@ func (ts *testDDLSuite) TestDDLTruncateTableStmtRestore(c *C) {
}
RunNodeRestoreTest(c, testCases, "%s", extractNodeFunc)
}

func (ts *testDDLSuite) TestColumnPositionRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"", ""},
{"first", "FIRST"},
{"after b", "AFTER `b`"},
}
extractNodeFunc := func(node Node) Node {
return node.(*AlterTableStmt).Specs[0].Position
}
RunNodeRestoreTest(c, testCases, "alter table t add column a varchar(255) %s", extractNodeFunc)
}

0 comments on commit 8c816f7

Please sign in to comment.