https://github.com/krasun/gosqlparser
Simple SQL parser
https://github.com/krasun/gosqlparser
go go-library golang lexer lexer-parser parser sql sqlparser
Last synced: 3 months ago
JSON representation
Simple SQL parser
- Host: GitHub
- URL: https://github.com/krasun/gosqlparser
- Owner: krasun
- License: mit
- Created: 2021-11-11T16:27:25.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-15T18:02:39.000Z (9 months ago)
- Last Synced: 2025-07-15T07:35:33.779Z (3 months ago)
- Topics: go, go-library, golang, lexer, lexer-parser, parser, sql, sqlparser
- Language: Go
- Homepage:
- Size: 60.5 KB
- Stars: 76
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gosqlparser
[](https://github.com/krasun/gosqlparser/actions/workflows/build.yml)
[](https://goreportcard.com/report/github.com/krasun/gosqlparser)
[](https://godoc.org/github.com/krasun/gosqlparser)`gosqlparser` is a simple SQL parser.
Use cases:
- as part of the database engine;
- as part of an application API instead of RPC or REST;
- to query data from CSV and other table-like files.## Installation
As simple as:
```
go get github.com/krasun/gosqlparser
```## Usage
```go
package gosqlparser_testimport (
"fmt""encoding/json"
sql "github.com/krasun/gosqlparser"
)func Example() {
query, err := sql.Parse("SELECT col1, col2 FROM table1 WHERE col1 == \"abc\" AND col3 == 5 LIMIT 10")
if err != nil {
fmt.Printf("unexpected error: %s", err)
return
}json, err := json.Marshal(query)
if err != nil {
fmt.Printf("unexpected error: %s", err)
return
}fmt.Println(string(json))
// Output:
// {"Table":"table1","Columns":["col1","col2"],"Where":{"Expr":{"Left":{"Left":{"Name":"col1"},"Operator":0,"Right":{"Value":"\"abc\""}},"Operator":1,"Right":{"Left":{"Name":"col3"},"Operator":0,"Right":{"Value":"5"}}}},"Limit":"10"}
}
```## Supported Statements
CREATE:
```
CREATE TABLE table1 (c1 INTEGER, c2 STRING)
```DROP:
```
DROP TABLE table1
```SELECT:
```
SELECT c1, c2 FROM table1 WHERE c3 == c4 AND c5 == c6
```INSERT:
```
INSERT INTO table1 (c1, c2, c3) VALUES (5, "some string", 10)
```UPDATE:
```
UPDATE table1 SET c1 = 10 WHERE c1 == 5 AND c3 == "quoted string"
```DELETE:
```
DELETE FROM table1 WHERE c1 == 5 AND c3 == "quoted string"
```## Placeholders
It is possible to use placeholders by including identifiers between curly brackets.
```
SELECT c1, c2 FROM table1 WHERE c3 == {0} AND c4 == {p}
```## Tests
To make sure that the code is fully tested and covered:
```
$ go test .
ok github.com/krasun/gosqlparser 0.470s
```## Known Usages
1. [krasun/gosqldb](https://github.com/krasun/gosqldb) - my experimental implementation of a simple database.
## License
**gosqlparser** is released under [the MIT license](LICENSE).