Skip to content

Commit 2aa1ade

Browse files
authored
remove dependency on TiDB (#679)
1 parent 53ef012 commit 2aa1ade

13 files changed

Lines changed: 1144 additions & 351 deletions

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ TiDB SQL Parser
77

88
## How to use it
99

10+
```go
11+
import (
12+
"fmt"
13+
"github.com/pingcap/parser"
14+
_ "github.com/pingcap/tidb/types/parser_driver"
15+
)
16+
17+
// This example show how to parse a text sql into ast.
18+
func example() {
19+
20+
// 0. make sure import parser_driver implemented by TiDB(user also can implement own driver by self).
21+
// and add `import _ "github.com/pingcap/tidb/types/parser_driver"` in the head of file.
22+
23+
// 1. Create a parser. The parser is NOT goroutine safe and should
24+
// not be shared among multiple goroutines. However, parser is also
25+
// heavy, so each goroutine should reuse its own local instance if
26+
// possible.
27+
p := parser.New()
28+
29+
// 2. Parse a text SQL into AST([]ast.StmtNode).
30+
stmtNodes, _, err := p.Parse("select * from tbl where id = 1", "", "")
31+
32+
// 3. Use AST to do cool things.
33+
fmt.Println(stmtNodes[0], err)
34+
}
35+
```
36+
1037
See [https://godoc.org/github.com/pingcap/parser](https://godoc.org/github.com/pingcap/parser)
1138

1239
## How to update parser for TiDB

ast/expressions_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
. "github.com/pingcap/check"
1818
. "github.com/pingcap/parser/ast"
1919
"github.com/pingcap/parser/format"
20-
_ "github.com/pingcap/tidb/types/parser_driver"
2120
)
2221

2322
var _ = Suite(&testExpressionsSuite{})

ast/functions_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ package ast_test
1616
import (
1717
. "github.com/pingcap/check"
1818
"github.com/pingcap/parser"
19-
"github.com/pingcap/parser/ast"
2019
. "github.com/pingcap/parser/ast"
21-
driver "github.com/pingcap/tidb/types/parser_driver"
20+
"github.com/pingcap/parser/test_driver"
2221
)
2322

2423
var _ = Suite(&testFunctionsSuite{})
@@ -146,9 +145,9 @@ func (ts *testFunctionsSuite) TestConvert(c *C) {
146145
}
147146
c.Assert(err, IsNil)
148147

149-
st := stmt.(*ast.SelectStmt)
148+
st := stmt.(*SelectStmt)
150149
expr := st.Fields.Fields[0].Expr.(*FuncCallExpr)
151-
charsetArg := expr.Args[1].(*driver.ValueExpr)
150+
charsetArg := expr.Args[1].(*test_driver.ValueExpr)
152151
c.Assert(charsetArg.GetString(), Equals, testCase.CharsetName)
153152
}
154153
}
@@ -175,9 +174,9 @@ func (ts *testFunctionsSuite) TestChar(c *C) {
175174
}
176175
c.Assert(err, IsNil)
177176

178-
st := stmt.(*ast.SelectStmt)
177+
st := stmt.(*SelectStmt)
179178
expr := st.Fields.Fields[0].Expr.(*FuncCallExpr)
180-
charsetArg := expr.Args[1].(*driver.ValueExpr)
179+
charsetArg := expr.Args[1].(*test_driver.ValueExpr)
181180
c.Assert(charsetArg.GetString(), Equals, testCase.CharsetName)
182181
}
183182
}

ast/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/pingcap/parser"
2222
. "github.com/pingcap/parser/ast"
2323
. "github.com/pingcap/parser/format"
24-
"github.com/pingcap/tidb/types/parser_driver"
24+
"github.com/pingcap/parser/test_driver"
2525
)
2626

2727
var _ = Suite(&testCacheableSuite{})
@@ -99,7 +99,7 @@ func (checker *nodeTextCleaner) Enter(in Node) (out Node, skipChildren bool) {
9999
node.FnName.O = strings.ToLower(node.FnName.O)
100100
switch node.FnName.L {
101101
case "convert":
102-
node.Args[1].(*driver.ValueExpr).Datum.SetBytes(nil)
102+
node.Args[1].(*test_driver.ValueExpr).Datum.SetBytes(nil)
103103
}
104104
case *AggregateFuncExpr:
105105
node.F = strings.ToLower(node.F)

go.mod1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ require (
77
github.com/cznic/sortutil v0.0.0-20181122101858-f5f958428db8
88
github.com/cznic/strutil v0.0.0-20171016134553-529a34b1c186
99
github.com/cznic/y v0.0.0-20170802143616-045f81c6662a
10+
github.com/golang/protobuf v1.3.2 // indirect
1011
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8
1112
github.com/pingcap/errors v0.11.4
12-
github.com/pingcap/tidb v0.0.0-20190703092821-755875aacb5a
1313
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330
14+
github.com/pkg/errors v0.8.1 // indirect
15+
github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237 // indirect
1416
github.com/sirupsen/logrus v1.3.0
1517
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2
16-
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
1718
)
1819

1920
go 1.13

go.sum1

Lines changed: 6 additions & 292 deletions
Large diffs are not rendered by default.

parser_example_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

parser_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import (
2929
"github.com/pingcap/parser/model"
3030
"github.com/pingcap/parser/mysql"
3131
"github.com/pingcap/parser/terror"
32-
"github.com/pingcap/tidb/types"
33-
driver "github.com/pingcap/tidb/types/parser_driver"
32+
"github.com/pingcap/parser/test_driver"
3433
)
3534

3635
func TestT(t *testing.T) {
@@ -251,16 +250,16 @@ func (s *testParserSuite) TestSimple(c *C) {
251250
sel, ok := st.(*ast.SelectStmt)
252251
c.Assert(ok, IsTrue)
253252
expr := sel.Fields.Fields[0]
254-
vExpr := expr.Expr.(*driver.ValueExpr)
255-
c.Assert(vExpr.Kind(), Equals, types.KindInt64)
253+
vExpr := expr.Expr.(*test_driver.ValueExpr)
254+
c.Assert(vExpr.Kind(), Equals, test_driver.KindInt64)
256255
src = "SELECT 9223372036854775808;"
257256
st, err = parser.ParseOneStmt(src, "", "")
258257
c.Assert(err, IsNil)
259258
sel, ok = st.(*ast.SelectStmt)
260259
c.Assert(ok, IsTrue)
261260
expr = sel.Fields.Fields[0]
262-
vExpr = expr.Expr.(*driver.ValueExpr)
263-
c.Assert(vExpr.Kind(), Equals, types.KindUint64)
261+
vExpr = expr.Expr.(*test_driver.ValueExpr)
262+
c.Assert(vExpr.Kind(), Equals, test_driver.KindUint64)
264263
}
265264

266265
func (s *testParserSuite) TestSpecialComments(c *C) {
@@ -4827,8 +4826,8 @@ func (checker *nodeTextCleaner) Enter(in ast.Node) (out ast.Node, skipChildren b
48274826
node.F = strings.ToLower(node.F)
48284827
case *ast.SelectField:
48294828
node.Offset = 0
4830-
case *driver.ValueExpr:
4831-
if node.Kind() == types.KindMysqlDecimal {
4829+
case *test_driver.ValueExpr:
4830+
if node.Kind() == test_driver.KindMysqlDecimal {
48324831
node.GetMysqlDecimal().FromString(node.GetMysqlDecimal().ToString())
48334832
}
48344833
case *ast.GrantStmt:

0 commit comments

Comments
 (0)