Go package for parsing MySQL SQL queries.
The backbone of this repo is extracted from vitessio/vitess.
Inside vitessio/vitess there is a very nicely written sql parser. However as it's not a self-contained application, I created this one. It applies the same LICENSE as vitessio/vitess.
import (
"github.com/xqbumu/sqlparser"
)
Then use:
sql := "SELECT * FROM table WHERE a = 'abc'"
stmt, err := sqlparser.Parse(sql)
if err != nil {
// Do something with the err
}
// Otherwise do something with stmt
switch stmt := stmt.(type) {
case *sqlparser.Select:
_ = stmt
case *sqlparser.Insert:
}
Alternative to read many queries from a io.Reader:
r := strings.NewReader("INSERT INTO table1 VALUES (1, 'a'); INSERT INTO table2 VALUES (3, 4);")
tokens := sqlparser.NewTokenizer(r)
for {
stmt, err := sqlparser.ParseNext(tokens)
if err == io.EOF {
break
}
// Do something with stmt or err.
}
See parse_test.go for more examples, or read the godoc.
You only need the below if you plan to try and keep this library up to date with vitessio/vitess.
./scirpts/tools.sh fetch
TODO: Change these instructions to use git to copy the files, that'll make later patching easier.
./scirpts/tools.sh install
./scirpts/tools.sh testing