Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yiplee/sqlc
sqlc: A simple dynamic query builer for [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)
https://github.com/yiplee/sqlc
Last synced: about 6 hours ago
JSON representation
sqlc: A simple dynamic query builer for [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)
- Host: GitHub
- URL: https://github.com/yiplee/sqlc
- Owner: yiplee
- License: mit
- Created: 2022-05-04T16:50:16.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-08-26T08:52:35.000Z (about 2 years ago)
- Last Synced: 2024-06-20T12:36:16.573Z (5 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 28
- Watchers: 1
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlc: A simple dynamic query builder for [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)
## How to use
```go
package sqlc_testimport (
"context"
"database/sql"
"log""github.com/yiplee/sqlc"
"github.com/yiplee/sqlc/example"
)func Example_build() {
ctx := context.Background()db, err := sql.Open("postgres", "dsn")
if err != nil {
panic(err)
}
defer db.Close()// Wrap the db in order to hook the query
query := example.New(sqlc.Wrap(db))/*
the original query must be simple and not contain any WHERE/ORDER/LIMIT/OFFSET clause-- name: ListAuthors :many
SELECT * FROM authors;
*/// customize the builder
authors, err := query.ListAuthors(sqlc.Build(ctx, func(b *sqlc.Builder) {
b.Where("age > $1", 10)
b.Where("name = $2", "foo")
b.Order("age,name DESC")
b.Limit(10)
}))if err != nil {
log.Fatalln("ListAuthors", err)
}log.Printf("list %d authors", len(authors))
}
```Not perfect, but enough for now.