Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dynajoe/sql-gen-go
https://github.com/dynajoe/sql-gen-go
Last synced: 21 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/dynajoe/sql-gen-go
- Owner: dynajoe
- Created: 2019-12-16T18:33:15.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-21T13:28:04.000Z (over 3 years ago)
- Last Synced: 2024-04-15T02:15:11.563Z (9 months ago)
- Language: Go
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SQL Gen Go
This package takes a directory and builds go types to be used with generated sql query builder.
## Example:
Assume this directory structure
```
./sql
/authors
create.sql
fetch.sql
list.sql
```This tool will build an output like this (annotated in comments):
```go
// Code generated by go generate; DO NOT EDIT.
package sql// Based on the directory and file authors/create.sql
// parameters from the sql file e.g. :bio and :name are available as part of the struct.
// in the future these params will be able to be annotated with primitives or sql.* types.
type AuthorsCreate struct {
Bio interface{}
Name interface{}
}// authors/fetch.sql
type AuthorsFetch struct {
AuthorId interface{}
}// authors/list.sql (no parameters)
type AuthorsList struct {
}func (p AuthorsCreate) Build() (string, []interface{}) {
return "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING *;", []interface{}{
p.Name,
p.Bio,
}
}func (p AuthorsFetch) Build() (string, []interface{}) {
return "SELECT * FROM authors\nWHERE id = $1 LIMIT 1;", []interface{}{
p.AuthorId,
}
}func (p AuthorsList) Build() (string, []interface{}) {
return "SELECT * FROM authors\nORDER BY name;", []interface{}{}
}
```And usage:
```go
package mainimport "github.com/dynajoe/sql-gen-go/sql"
func main() {
query, params := (sql.AuthorsCreate{
Name: "Joe",
Bio: "I like databases",
}).Build()// Use query and params with your sql driver of choice
}
```