Debian Codegraph @

README.md

raw symbols SCIP
1 # sqlparser [![Build Status](https://img.shields.io/travis/xwb1989/sqlparser.svg)](https://travis-ci.org/xwb1989/sqlparser) [![Coverage](https://img.shields.io/coveralls/xwb1989/sqlparser.svg)](https://coveralls.io/github/xwb1989/sqlparser) [![Report card](https://goreportcard.com/badge/github.com/xwb1989/sqlparser)](https://goreportcard.com/report/github.com/xwb1989/sqlparser) [![GoDoc](https://godoc.org/github.com/xwb1989/sqlparser?status.svg)](https://godoc.org/github.com/xwb1989/sqlparser)
2
3 Go package for parsing MySQL SQL queries.
4
5 ## Notice
6
7 The backbone of this repo is extracted from [vitessio/vitess](https://github.com/vitessio/vitess).
8
9 Inside vitessio/vitess there is a very nicely written sql parser. However as it's not a self-contained application, I created this one.
10 It applies the same LICENSE as vitessio/vitess.
11
12 ## Usage
13
14 ```go
15 import (
16 "github.com/xwb1989/sqlparser"
17 )
18 ```
19
20 Then use:
21
22 ```go
23 sql := "SELECT * FROM table WHERE a = 'abc'"
24 stmt, err := sqlparser.Parse(sql)
25 if err != nil {
26 // Do something with the err
27 }
28
29 // Otherwise do something with stmt
30 switch stmt := stmt.(type) {
31 case *sqlparser.Select:
32 _ = stmt
33 case *sqlparser.Insert:
34 }
35 ```
36
37 Alternative to read many queries from a io.Reader:
38
39 ```go
40 r := strings.NewReader("INSERT INTO table1 VALUES (1, 'a'); INSERT INTO table2 VALUES (3, 4);")
41
42 tokens := sqlparser.NewTokenizer(r)
43 for {
44 stmt, err := sqlparser.ParseNext(tokens)
45 if err == io.EOF {
46 break
47 }
48 // Do something with stmt or err.
49 }
50 ```
51
52 See [parse_test.go](https://github.com/xwb1989/sqlparser/blob/master/parse_test.go) for more examples, or read the [godoc](https://godoc.org/github.com/xwb1989/sqlparser).
53
54
55 ## Porting Instructions
56
57 You only need the below if you plan to try and keep this library up to date with [vitessio/vitess](https://github.com/vitessio/vitess).
58
59 ### Keeping up to date
60
61 ```bash
62 shopt -s nullglob
63 VITESS=${GOPATH?}/src/vitess.io/vitess/go/
64 XWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/
65
66 # Create patches for everything that changed
67 LASTIMPORT=1b7879cb91f1dfe1a2dfa06fea96e951e3a7aec5
68 for path in ${VITESS?}/{vt/sqlparser,sqltypes,bytes2,hack}; do
69 cd ${path}
70 git format-patch ${LASTIMPORT?} .
71 done;
72
73 # Apply patches to the dependencies
74 cd ${XWB1989?}
75 git am --directory dependency -p2 ${VITESS?}/{sqltypes,bytes2,hack}/*.patch
76
77 # Apply the main patches to the repo
78 cd ${XWB1989?}
79 git am -p4 ${VITESS?}/vt/sqlparser/*.patch
80
81 # If you encounter diff failures, manually fix them with
82 patch -p4 < .git/rebase-apply/patch
83 ...
84 git add name_of_files
85 git am --continue
86
87 # Cleanup
88 rm ${VITESS?}/{sqltypes,bytes2,hack}/*.patch ${VITESS?}/*.patch
89
90 # and Finally update the LASTIMPORT in this README.
91 ```
92
93 ### Fresh install
94
95 TODO: Change these instructions to use git to copy the files, that'll make later patching easier.
96
97 ```bash
98 VITESS=${GOPATH?}/src/vitess.io/vitess/go/
99 XWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/
100
101 cd ${XWB1989?}
102
103 # Copy all the code
104 cp -pr ${VITESS?}/vt/sqlparser/ .
105 cp -pr ${VITESS?}/sqltypes dependency
106 cp -pr ${VITESS?}/bytes2 dependency
107 cp -pr ${VITESS?}/hack dependency
108
109 # Delete some code we haven't ported
110 rm dependency/sqltypes/arithmetic.go dependency/sqltypes/arithmetic_test.go dependency/sqltypes/event_token.go dependency/sqltypes/event_token_test.go dependency/sqltypes/proto3.go dependency/sqltypes/proto3_test.go dependency/sqltypes/query_response.go dependency/sqltypes/result.go dependency/sqltypes/result_test.go
111
112 # Some automated fixes
113
114 # Fix imports
115 sed -i '.bak' 's_vitess.io/vitess/go/vt/proto/query_github.com/xwb1989/sqlparser/dependency/querypb_g' *.go dependency/sqltypes/*.go
116 sed -i '.bak' 's_vitess.io/vitess/go/_github.com/xwb1989/sqlparser/dependency/_g' *.go dependency/sqltypes/*.go
117
118 # Copy the proto, but basically drop everything we don't want
119 cp -pr ${VITESS?}/vt/proto/query dependency/querypb
120
121 sed -i '.bak' 's_.*Descriptor.*__g' dependency/querypb/*.go
122 sed -i '.bak' 's_.*ProtoMessage.*__g' dependency/querypb/*.go
123
124 sed -i '.bak' 's/proto.CompactTextString(m)/"TODO"/g' dependency/querypb/*.go
125 sed -i '.bak' 's/proto.EnumName/EnumName/g' dependency/querypb/*.go
126
127 sed -i '.bak' 's/proto.Equal/reflect.DeepEqual/g' dependency/sqltypes/*.go
128
129 # Remove the error library
130 sed -i '.bak' 's/vterrors.Errorf([^,]*, /fmt.Errorf(/g' *.go dependency/sqltypes/*.go
131 sed -i '.bak' 's/vterrors.New([^,]*, /errors.New(/g' *.go dependency/sqltypes/*.go
132 ```
133
134 ### Testing
135
136 ```bash
137 VITESS=${GOPATH?}/src/vitess.io/vitess/go/
138 XWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/
139
140 cd ${XWB1989?}
141
142 # Test, fix and repeat
143 go test ./...
144
145 # Finally make some diffs (for later reference)
146 diff -u ${VITESS?}/sqltypes/ ${XWB1989?}/dependency/sqltypes/ > ${XWB1989?}/patches/sqltypes.patch
147 diff -u ${VITESS?}/bytes2/ ${XWB1989?}/dependency/bytes2/ > ${XWB1989?}/patches/bytes2.patch
148 diff -u ${VITESS?}/vt/proto/query/ ${XWB1989?}/dependency/querypb/ > ${XWB1989?}/patches/querypb.patch
149 diff -u ${VITESS?}/vt/sqlparser/ ${XWB1989?}/ > ${XWB1989?}/patches/sqlparser.patch
150 ```