https://github.com/lithdew/sqlutil
Utilities for working with SQL in Go.
https://github.com/lithdew/sqlutil
golang sql
Last synced: 6 months ago
JSON representation
Utilities for working with SQL in Go.
- Host: GitHub
- URL: https://github.com/lithdew/sqlutil
- Owner: lithdew
- License: mit
- Created: 2020-04-04T17:45:15.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-08T16:54:41.000Z (almost 6 years ago)
- Last Synced: 2025-02-06T07:24:36.400Z (about 1 year ago)
- Topics: golang, sql
- Language: Go
- Size: 15.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlutil
[](LICENSE)
[](https://pkg.go.dev/github.com/lithdew/sqlutil)
[](https://discord.gg/HZEbkeQ)
Utilities for working with SQL in Go.
- Append an opinionated UTF-8 string representation of an `interface{}` to a byte slice, with byte slices being encoded into RFC-4648 Base64.
- Convert `*sql.Rows` into JSON with minimal allocations.
- Convert `*sql.Rows` into CSV with minimal allocations.
- Parse/evaluate SQL statements with named parameters.
## Example
Given the SQL query:
```sqlite
CREATE TABLE test (id integer primary key autoincrement, name varchar);
INSERT INTO test (name) VALUES ('a');
INSERT INTO test (name) VALUES ('b');
INSERT INTO test (name) VALUES ('c');
INSERT INTO test (name) VALUES ('d');
SELECT * FROM test;
```
`sqlutil.RowsToJSON` would yield:
```json
[
{
"id": 1,
"name": "a"
},
{
"id": 2,
"name": "b"
},
{
"id": 3,
"name": "c"
},
{
"id": 4,
"name": "d"
}
]
```
`sqlutil.RowsToCSV` would yield:
```csv
id,name
1,"a"
2,"b"
3,"c"
4,"d"
```
## Benchmarks
```
go test -bench=. -benchmem -benchtime=10s
goos: linux
goarch: amd64
pkg: github.com/lithdew/sqlutil
BenchmarkRowsToJSON-8 1213502 9531 ns/op 584 B/op 27 allocs/op
BenchmarkRowsToCSV-8 1268085 9991 ns/op 584 B/op 27 allocs/op
```