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 PatternInExpr #92

Merged
merged 4 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,30 @@ type PatternInExpr struct {

// Restore implements Node interface.
func (n *PatternInExpr) 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 PatternInExpr.Expr")
}
if n.Not {
ctx.WriteKeyWord(" NOT IN ")
} else {
ctx.WriteKeyWord(" IN ")
}
ctx.WritePlain("(")
for i, expr := range n.List {
if i != 0 {
ctx.WritePlain(",")
}
if err := expr.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore PatternInExpr.List[%d]", i)
}
}
if n.Sel != nil {
if err := n.Sel.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore PatternInExpr.Sel")
}
}
ctx.WritePlain(")")
return nil
}

// Format the ExprNode into a Writer.
Expand All @@ -618,10 +641,10 @@ func (n *PatternInExpr) Format(w io.Writer) {
fmt.Fprint(w, " IN (")
}
for i, expr := range n.List {
expr.Format(w)
if i != len(n.List)-1 {
if i != 0 {
fmt.Fprint(w, ",")
}
expr.Format(w)
}
fmt.Fprint(w, ")")
}
Expand Down
15 changes: 15 additions & 0 deletions ast/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ func (tc *testExpressionsSuite) TestDefaultExpr(c *C) {
RunNodeRestoreTest(c, testCases, "insert into t values(%s)", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestPatternInExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"'a' in ('b')", "'a' IN ('b')"},
{"2 in (0,3,7)", "2 IN (0,3,7)"},
{"2 not in (0,3,7)", "2 NOT IN (0,3,7)"},
tony612 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Test for subquery when it's implemented
// 2 in (select 2)
// 2 not in (select 2)
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
}
RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestPatternLikeExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"a like 't1'", "`a` LIKE 't1'"},
Expand Down