Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: implement Restore for BetweenExpr #71

Merged
merged 10 commits into from
Dec 17, 2018
17 changes: 16 additions & 1 deletion ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,22 @@ type BetweenExpr struct {

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

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

func (tc *testExpressionsSuite) TestBetweenExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"b between 1 and 2", "`b` BETWEEN 1 AND 2"},
{"b not between 1 and 2", "`b` NOT BETWEEN 1 AND 2"},
{"b between a and b", "`b` BETWEEN `a` AND `b`"},
{"b between '' and 'b'", "`b` BETWEEN '' AND 'b'"},
{"b between '2018-11-01' and '2018-11-02'", "`b` BETWEEN '2018-11-01' AND '2018-11-02'"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
}
RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc)
}