{"id":13670155,"url":"https://github.com/xwb1989/sqlparser","last_synced_at":"2025-05-14T03:08:49.078Z","repository":{"id":23768141,"uuid":"27142938","full_name":"xwb1989/sqlparser","owner":"xwb1989","description":"SQL Parser implemented in Go","archived":false,"fork":false,"pushed_at":"2022-09-27T14:36:33.000Z","size":1112,"stargazers_count":1528,"open_issues_count":44,"forks_count":244,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-05-08T18:44:50.326Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xwb1989.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-25T19:45:26.000Z","updated_at":"2025-05-08T10:07:45.000Z","dependencies_parsed_at":"2022-07-10T10:00:28.178Z","dependency_job_id":null,"html_url":"https://github.com/xwb1989/sqlparser","commit_stats":null,"previous_names":["xwb1989/sql_parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwb1989%2Fsqlparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwb1989%2Fsqlparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwb1989%2Fsqlparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xwb1989%2Fsqlparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xwb1989","download_url":"https://codeload.github.com/xwb1989/sqlparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254059507,"owners_count":22007768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:00:34.154Z","updated_at":"2025-05-14T03:08:49.034Z","avatar_url":"https://github.com/xwb1989.png","language":"Go","readme":"# 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)\n\nGo package for parsing MySQL SQL queries.\n\n## Notice\n\nThe backbone of this repo is extracted from [vitessio/vitess](https://github.com/vitessio/vitess).\n\nInside vitessio/vitess there is a very nicely written sql parser. However as it's not a self-contained application, I created this one.\nIt applies the same LICENSE as vitessio/vitess.\n\n## Usage\n\n```go\nimport (\n    \"github.com/xwb1989/sqlparser\"\n)\n```\n\nThen use:\n\n```go\nsql := \"SELECT * FROM table WHERE a = 'abc'\"\nstmt, err := sqlparser.Parse(sql)\nif err != nil {\n\t// Do something with the err\n}\n\n// Otherwise do something with stmt\nswitch stmt := stmt.(type) {\ncase *sqlparser.Select:\n\t_ = stmt\ncase *sqlparser.Insert:\n}\n```\n\nAlternative to read many queries from a io.Reader:\n\n```go\nr := strings.NewReader(\"INSERT INTO table1 VALUES (1, 'a'); INSERT INTO table2 VALUES (3, 4);\")\n\ntokens := sqlparser.NewTokenizer(r)\nfor {\n\tstmt, err := sqlparser.ParseNext(tokens)\n\tif err == io.EOF {\n\t\tbreak\n\t}\n\t// Do something with stmt or err.\n}\n```\n\nSee [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).\n\n\n## Porting Instructions\n\nYou only need the below if you plan to try and keep this library up to date with [vitessio/vitess](https://github.com/vitessio/vitess).\n\n### Keeping up to date\n\n```bash\nshopt -s nullglob\nVITESS=${GOPATH?}/src/vitess.io/vitess/go/\nXWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/\n\n# Create patches for everything that changed\nLASTIMPORT=1b7879cb91f1dfe1a2dfa06fea96e951e3a7aec5\nfor path in ${VITESS?}/{vt/sqlparser,sqltypes,bytes2,hack}; do\n\tcd ${path}\n\tgit format-patch ${LASTIMPORT?} .\ndone;\n\n# Apply patches to the dependencies\ncd ${XWB1989?}\ngit am --directory dependency -p2 ${VITESS?}/{sqltypes,bytes2,hack}/*.patch\n\n# Apply the main patches to the repo\ncd ${XWB1989?}\ngit am -p4 ${VITESS?}/vt/sqlparser/*.patch\n\n# If you encounter diff failures, manually fix them with\npatch -p4 \u003c .git/rebase-apply/patch\n...\ngit add name_of_files\ngit am --continue\n\n# Cleanup\nrm ${VITESS?}/{sqltypes,bytes2,hack}/*.patch ${VITESS?}/*.patch\n\n# and Finally update the LASTIMPORT in this README.\n```\n\n### Fresh install\n\nTODO: Change these instructions to use git to copy the files, that'll make later patching easier.\n\n```bash\nVITESS=${GOPATH?}/src/vitess.io/vitess/go/\nXWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/\n\ncd ${XWB1989?}\n\n# Copy all the code\ncp -pr ${VITESS?}/vt/sqlparser/ .\ncp -pr ${VITESS?}/sqltypes dependency\ncp -pr ${VITESS?}/bytes2 dependency\ncp -pr ${VITESS?}/hack dependency\n\n# Delete some code we haven't ported\nrm 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\n\n# Some automated fixes\n\n# Fix imports\nsed -i '.bak' 's_vitess.io/vitess/go/vt/proto/query_github.com/xwb1989/sqlparser/dependency/querypb_g' *.go dependency/sqltypes/*.go\nsed -i '.bak' 's_vitess.io/vitess/go/_github.com/xwb1989/sqlparser/dependency/_g' *.go dependency/sqltypes/*.go\n\n# Copy the proto, but basically drop everything we don't want\ncp -pr ${VITESS?}/vt/proto/query dependency/querypb\n\nsed -i '.bak' 's_.*Descriptor.*__g' dependency/querypb/*.go\nsed -i '.bak' 's_.*ProtoMessage.*__g' dependency/querypb/*.go\n\nsed -i '.bak' 's/proto.CompactTextString(m)/\"TODO\"/g' dependency/querypb/*.go\nsed -i '.bak' 's/proto.EnumName/EnumName/g' dependency/querypb/*.go\n\nsed -i '.bak' 's/proto.Equal/reflect.DeepEqual/g' dependency/sqltypes/*.go\n\n# Remove the error library\nsed -i '.bak' 's/vterrors.Errorf([^,]*, /fmt.Errorf(/g' *.go dependency/sqltypes/*.go\nsed -i '.bak' 's/vterrors.New([^,]*, /errors.New(/g' *.go dependency/sqltypes/*.go\n```\n\n### Testing\n\n```bash\nVITESS=${GOPATH?}/src/vitess.io/vitess/go/\nXWB1989=${GOPATH?}/src/github.com/xwb1989/sqlparser/\n\ncd ${XWB1989?}\n\n# Test, fix and repeat\ngo test ./...\n\n# Finally make some diffs (for later reference)\ndiff -u ${VITESS?}/sqltypes/        ${XWB1989?}/dependency/sqltypes/ \u003e ${XWB1989?}/patches/sqltypes.patch\ndiff -u ${VITESS?}/bytes2/          ${XWB1989?}/dependency/bytes2/   \u003e ${XWB1989?}/patches/bytes2.patch\ndiff -u ${VITESS?}/vt/proto/query/  ${XWB1989?}/dependency/querypb/  \u003e ${XWB1989?}/patches/querypb.patch\ndiff -u ${VITESS?}/vt/sqlparser/    ${XWB1989?}/                     \u003e ${XWB1989?}/patches/sqlparser.patch\n```","funding_links":[],"categories":["开源类库","Open source library","Go"],"sub_categories":["数据库","Database"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxwb1989%2Fsqlparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxwb1989%2Fsqlparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxwb1989%2Fsqlparser/lists"}