Skip to content

Commit

Permalink
[parser] parser: implement Restore for Limit (#100)
Browse files Browse the repository at this point in the history
* parser: implement Restore for Limit

* fix

* change test content
  • Loading branch information
hanchuanchuan authored and ti-chi-bot committed Oct 9, 2021
1 parent 8862bb5 commit 20ff6cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,17 @@ type Limit struct {

// Restore implements Node interface.
func (n *Limit) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("LIMIT ")
if n.Offset != nil {
if err := n.Offset.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore Limit.Offset")
}
ctx.WritePlain(",")
}
if err := n.Count.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore Limit.Count")
}
return nil
}

// Accept implements Node Accept interface.
Expand Down
13 changes: 13 additions & 0 deletions parser/ast/dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,16 @@ func (tc *testDMLSuite) TestTableNameIndexHintsRestore(c *C) {
}
RunNodeRestoreTest(c, testCases, "SELECT * FROM %s", extractNodeFunc)
}

func (tc *testDMLSuite) TestLimitRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"limit 10", "LIMIT 10"},
{"limit 10,20", "LIMIT 10,20"},
{"limit 20 offset 10", "LIMIT 10,20"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Limit
}
RunNodeRestoreTest(c, testCases, "SELECT 1 %s", extractNodeFunc)
}

0 comments on commit 20ff6cc

Please sign in to comment.