Skip to content

Commit f457f24

Browse files
mccxjshenli
authored andcommitted
ast: fix format bug for some expressions (#7770)
1 parent 038ac63 commit f457f24

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

ast/expressions.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ type BetweenExpr struct {
145145
// Format the ExprNode into a Writer.
146146
func (n *BetweenExpr) Format(w io.Writer) {
147147
n.Expr.Format(w)
148-
fmt.Fprint(w, " BETWEEN ")
148+
if n.Not {
149+
fmt.Fprint(w, " NOT BETWEEN ")
150+
} else {
151+
fmt.Fprint(w, " BETWEEN ")
152+
}
149153
n.Left.Format(w)
150154
fmt.Fprint(w, " AND ")
151155
n.Right.Format(w)
@@ -540,7 +544,11 @@ type PatternInExpr struct {
540544
// Format the ExprNode into a Writer.
541545
func (n *PatternInExpr) Format(w io.Writer) {
542546
n.Expr.Format(w)
543-
fmt.Fprint(w, " IN (")
547+
if n.Not {
548+
fmt.Fprint(w, " NOT IN (")
549+
} else {
550+
fmt.Fprint(w, " IN (")
551+
}
544552
for i, expr := range n.List {
545553
expr.Format(w)
546554
if i != len(n.List)-1 {
@@ -673,7 +681,11 @@ type PatternLikeExpr struct {
673681
// Format the ExprNode into a Writer.
674682
func (n *PatternLikeExpr) Format(w io.Writer) {
675683
n.Expr.Format(w)
676-
fmt.Fprint(w, " LIKE ")
684+
if n.Not {
685+
fmt.Fprint(w, " NOT LIKE ")
686+
} else {
687+
fmt.Fprint(w, " LIKE ")
688+
}
677689
n.Pattern.Format(w)
678690
if n.Escape != '\\' {
679691
fmt.Fprint(w, " ESCAPE ")

ast/format_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {
4545

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

0 commit comments

Comments
 (0)