Skip to content

Commit

Permalink
parser: implement Restore for OnCondition and TableRefsClause (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
exialin authored and leoppro committed Dec 28, 2018
1 parent 20bc24a commit a4bdcab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (n *Join) Restore(ctx *RestoreCtx) error {
ctx.JoinLevel--

if n.On != nil {
ctx.WriteKeyWord(" ON ")
if err := n.On.Expr.Restore(ctx); err != nil {
ctx.WritePlain(" ")
if err := n.On.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore Join.On")
}
}
Expand Down Expand Up @@ -325,7 +325,11 @@ type OnCondition struct {

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

// Accept implements Node Accept interface.
Expand Down Expand Up @@ -551,7 +555,10 @@ type TableRefsClause struct {

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

// Accept implements Node Accept interface.
Expand Down
25 changes: 25 additions & 0 deletions ast/dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ func (tc *testDMLSuite) TestTableSourceRestore(c *C) {
RunNodeRestoreTest(c, testCases, "select * from %s", extractNodeFunc)
}

func (tc *testDMLSuite) TestOnConditionRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"on t1.a=t2.a", "ON `t1`.`a`=`t2`.`a`"},
{"on t1.a=t2.a and t1.b=t2.b", "ON `t1`.`a`=`t2`.`a`&&`t1`.`b`=`t2`.`b`"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).From.TableRefs.On
}
RunNodeRestoreTest(c, testCases, "select * from t1 join t2 %s", extractNodeFunc)
}

func (tc *testDMLSuite) TestJoinRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"t1 natural join t2", "`t1` NATURAL JOIN `t2`"},
Expand All @@ -215,6 +226,19 @@ func (tc *testDMLSuite) TestJoinRestore(c *C) {
}
RunNodeRestoreTest(c, testCases, "select * from %s", extractNodeFunc)
}

func (ts *testDMLSuite) TestTableRefsClauseRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"t", "`t`"},
{"t1 join t2", "`t1` JOIN `t2`"},
{"t1, t2", "(`t1`) JOIN `t2`"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).From
}
RunNodeRestoreTest(c, testCases, "select * from %s", extractNodeFunc)
}

func (tc *testDMLSuite) TestDeleteTableListRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"t1,t2", "`t1`,`t2`"},
Expand All @@ -225,6 +249,7 @@ func (tc *testDMLSuite) TestDeleteTableListRestore(c *C) {
RunNodeRestoreTest(c, testCases, "DELETE %s FROM t1, t2;", extractNodeFunc)
RunNodeRestoreTest(c, testCases, "DELETE FROM %s USING t1, t2;", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestByItemRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"a", "`a`"},
Expand Down

0 comments on commit a4bdcab

Please sign in to comment.