Skip to content

Commit

Permalink
[parser] parser: implement Restore for DefaultExpr (pingcap#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
exialin authored and leoppro committed Dec 18, 2018
1 parent 898430e commit 533fee6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion parser/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,15 @@ type DefaultExpr struct {

// Restore implements Node interface.
func (n *DefaultExpr) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("DEFAULT")
if n.Name != nil {
ctx.WritePlain("(")
if err := n.Name.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore DefaultExpr.Name")
}
ctx.WritePlain(")")
}
return nil
}

// Format the ExprNode into a Writer.
Expand Down
11 changes: 11 additions & 0 deletions parser/ast/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,14 @@ func (tc *testExpressionsSuite) TestWhenClause(c *C) {
}
RunNodeRestoreTest(c, testCases, "select case %s end", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestDefaultExpr(c *C) {
testCases := []NodeRestoreTestCase{
{"default", "DEFAULT"},
{"default(i)", "DEFAULT(`i`)"},
}
extractNodeFunc := func(node Node) Node {
return node.(*InsertStmt).Lists[0][0]
}
RunNodeRestoreTest(c, testCases, "insert into t values(%s)", extractNodeFunc)
}

0 comments on commit 533fee6

Please sign in to comment.