Skip to content

Commit

Permalink
[parser] parser: implement Restore for PatternRegexpExpr (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangkaisen authored and ti-chi-bot committed Oct 9, 2021
1 parent 958c8ed commit 3828d52
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion parser/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,21 @@ type PatternRegexpExpr struct {

// Restore implements Node interface.
func (n *PatternRegexpExpr) 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 PatternRegexpExpr.Expr")
}

if n.Not {
ctx.WriteKeyWord(" NOT REGEXP ")
} else {
ctx.WriteKeyWord(" REGEXP ")
}

if err := n.Pattern.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore PatternRegexpExpr.Pattern")
}

return nil
}

// Format the ExprNode into a Writer.
Expand Down
17 changes: 17 additions & 0 deletions parser/ast/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,20 @@ func (tc *testExpressionsSuite) TestValuesExpr(c *C) {
}
RunNodeRestoreTest(c, testCases, "insert into t values (1,2,3) on duplicate key update c=%s", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestPatternRegexpExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"a regexp 't1'", "`a` REGEXP 't1'"},
{"a regexp '^[abc][0-9]{11}|ok$'", "`a` REGEXP '^[abc][0-9]{11}|ok$'"},
{"a rlike 't1'", "`a` REGEXP 't1'"},
{"a rlike '^[abc][0-9]{11}|ok$'", "`a` REGEXP '^[abc][0-9]{11}|ok$'"},
{"a not regexp 't1'", "`a` NOT REGEXP 't1'"},
{"a not regexp '^[abc][0-9]{11}|ok$'", "`a` NOT REGEXP '^[abc][0-9]{11}|ok$'"},
{"a not rlike 't1'", "`a` NOT REGEXP 't1'"},
{"a not rlike '^[abc][0-9]{11}|ok$'", "`a` NOT REGEXP '^[abc][0-9]{11}|ok$'"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
}
RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc)
}

0 comments on commit 3828d52

Please sign in to comment.