Skip to content

Commit

Permalink
[parser] parser: Implement Restore for IsTruthExpr (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
knarfeh authored and ti-chi-bot committed Oct 9, 2021
1 parent 58e5251 commit bb7f83d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion parser/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,20 @@ type IsTruthExpr struct {

// Restore implements Node interface.
func (n *IsTruthExpr) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
if err := n.Expr.Restore(ctx); err != nil {
return errors.Trace(err)
}
if n.Not {
ctx.WriteKeyWord(" IS NOT")
} else {
ctx.WriteKeyWord(" IS")
}
if n.True > 0 {
ctx.WriteKeyWord(" TRUE")
} else {
ctx.WriteKeyWord(" FALSE")
}
return nil
}

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

func (tc *testExpressionsSuite) TestIsTruthRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"a is true", "`a` IS TRUE"},
{"a is not true", "`a` IS NOT TRUE"},
{"a is FALSE", "`a` IS FALSE"},
{"a is not false", "`a` IS NOT FALSE"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
}
RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestBetweenExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"b between 1 and 2", "`b` BETWEEN 1 AND 2"},
Expand Down

0 comments on commit bb7f83d

Please sign in to comment.