sqlcodegen
Generate Go structs from SQL string literals.
Currently supports only PostgreSQL.
Usage
- Run init to generate sqlcodegen.yml
go run github.com/mtsmfm/sqlcodegen init
- Write a code which runs SQL with plain string literal and comment
// sqlcodegen <struct name>
just before the literal
var results []interface{}
// sqlcodegen Foo
err = db.Select(&results, "SELECT id FROM users")
- Run generate command
export DATABASE_URL=postgres://user:password@postgres/db?sslmode=disable
go run github.com/mtsmfm/sqlcodegen generate
Above command generates:
// Code generated by sqlcodegen, DO NOT EDIT.
package sqlstructs
/*
SELECT id FROM users LIMIT 5
*/
type Foo struct {
Id int `db:"id" json:"id"`
}
- Use struct
var results []sqlstructs.Foo
err = db.Select(&results, "SELECT id FROM users")
TODOs
- Support MySQL
- Add more default typemap