Parse SQL statements, support mysql, oracle, sqlserver,sql92
The first open source project of a golang novice, welcome to star, fork, if you have any needs, comments and suggestions, please mention them in the issue, I can also use antlr's TokenStreamRewrite to make logical changes to specific nodes of sql, and the project is still temporarily This feature was not added, I can add it if needed.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Get it
go get github.com/jiunx/xsqlparser
You can use the test code in parser_test.go to test that the parser parses the sql statement correctly.
sql := "select id,name,address from t"
parserFactory := parser.GetParserFactoryInstance()
mysqlParser, err := parserFactory.GetParser(util.Mysql)
if err != nil {
fmt.Println(err)
return
}
statement, err2 := mysqlParser.Parse(context.Background(), sql)
if err2 != nil {
fmt.Println(err2)
}
fmt.Printf("%+v\n", statement)
You will get output like this
&{Sql:select id,name,address from t SqlType:select Language:mysql Tables:[t] Columns:[id name address] Results:[id name address] Ast:0xc000246c00}
The following describes the structure of the statement
statement.SqlType is the sql type, such as select, insert, update, delete, create, drop, etc.
statement.Tables is the table name, such as t.
statement.Columns is the column name, such as id, name, address.
statement.Results is the resultset, such as id, name, address.
statement.Ast This sql AST tree, if you are familiar with ANTLR or tree operations, you can also use listener to parse sql.
.
jiunx, I am the author of this project.
antlr, mainly rely on technology
shardingsphere,the grammer file of antlr is obtained from here
jiunx
- Antlr4