Skip to content
forked from vitessio/vitess

Vitess is a database clustering system for horizontal scaling of MySQL.

License

Notifications You must be signed in to change notification settings

kanzihuang/vitess

 
 

Repository files navigation

sqlparser

Go package for parsing MySQL SQL queries.

Notice

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 and does not support Windows, I created this one. It applies the same LICENSE as vitessio/vitess.

The README.md is extracted from xwb1989/sqlparser.

Inside xwb1989/sqlparser there is a very nicely written sql parser. However, it's not support current_timestamp().

Usage

import (
    "github.com/kanzihuang/go/vt/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.NewReaderTokenizer(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.

About

Vitess is a database clustering system for horizontal scaling of MySQL.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 83.9%
  • Yacc 16.1%