Skip to content

Commit

Permalink
ast: fix format bug for some expressions (#7770)
Browse files Browse the repository at this point in the history
  • Loading branch information
mccxj authored and shenli committed Sep 23, 2018
1 parent 038ac63 commit f457f24
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ type BetweenExpr struct {
// Format the ExprNode into a Writer.
func (n *BetweenExpr) Format(w io.Writer) {
n.Expr.Format(w)
fmt.Fprint(w, " BETWEEN ")
if n.Not {
fmt.Fprint(w, " NOT BETWEEN ")
} else {
fmt.Fprint(w, " BETWEEN ")
}
n.Left.Format(w)
fmt.Fprint(w, " AND ")
n.Right.Format(w)
Expand Down Expand Up @@ -540,7 +544,11 @@ type PatternInExpr struct {
// Format the ExprNode into a Writer.
func (n *PatternInExpr) Format(w io.Writer) {
n.Expr.Format(w)
fmt.Fprint(w, " IN (")
if n.Not {
fmt.Fprint(w, " NOT IN (")
} else {
fmt.Fprint(w, " IN (")
}
for i, expr := range n.List {
expr.Format(w)
if i != len(n.List)-1 {
Expand Down Expand Up @@ -673,7 +681,11 @@ type PatternLikeExpr struct {
// Format the ExprNode into a Writer.
func (n *PatternLikeExpr) Format(w io.Writer) {
n.Expr.Format(w)
fmt.Fprint(w, " LIKE ")
if n.Not {
fmt.Fprint(w, " NOT LIKE ")
} else {
fmt.Fprint(w, " LIKE ")
}
n.Pattern.Format(w)
if n.Escape != '\\' {
fmt.Fprint(w, " ESCAPE ")
Expand Down
3 changes: 3 additions & 0 deletions ast/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {

// Expressions.
{`f between 30 and 50`, "`f` BETWEEN 30 AND 50"},
{`f not between 30 and 50`, "`f` NOT BETWEEN 30 AND 50"},
{`345 + " hello "`, `345 + " hello "`},
{`"hello world" >= 'hello world'`, `"hello world" >= "hello world"`},
{`case 3 when 1 then false else true end`, `CASE 3 WHEN 1 THEN FALSE ELSE TRUE END`},
Expand All @@ -56,7 +57,9 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {
{`3 is false`, `3 IS FALSE`},
{` ( x is false )`, "(`x` IS FALSE)"},
{`3 in ( a,b,"h",6 )`, "3 IN (`a`,`b`,\"h\",6)"},
{`3 not in ( a,b,"h",6 )`, "3 NOT IN (`a`,`b`,\"h\",6)"},
{`"abc" like '%b%'`, `"abc" LIKE "%b%"`},
{`"abc" not like '%b%'`, `"abc" NOT LIKE "%b%"`},
{`"abc" like '%b%' escape '_'`, `"abc" LIKE "%b%" ESCAPE '_'`},
{`"abc" regexp '.*bc?'`, `"abc" REGEXP ".*bc?"`},
{`"abc" not regexp '.*bc?'`, `"abc" NOT REGEXP ".*bc?"`},
Expand Down

0 comments on commit f457f24

Please sign in to comment.