Skip to content

Commit

Permalink
[parser] parser: implement Restore for Assignment (pingcap#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 authored and xhebox committed Oct 8, 2021
1 parent 23128a3 commit a55df28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,14 @@ type Assignment struct {

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

// Accept implements Node Accept interface.
Expand Down
11 changes: 11 additions & 0 deletions parser/ast/dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ func (tc *testDMLSuite) TestOrderByClauseRestore(c *C) {
RunNodeRestoreTest(c, testCases, "SELECT 1 FROM t1 UNION SELECT 2 FROM t2 %s", extractNodeFromUnionStmtFunc)
}

func (tc *testDMLSuite) TestAssignmentRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"a=1", "`a`=1"},
{"b=1+2", "`b`=1+2"},
}
extractNodeFunc := func(node Node) Node {
return node.(*UpdateStmt).List[0]
}
RunNodeRestoreTest(c, testCases, "UPDATE t1 SET %s", extractNodeFunc)
}

func (ts *testDMLSuite) TestHavingClauseRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"HAVING a", "HAVING `a`"},
Expand Down

0 comments on commit a55df28

Please sign in to comment.