go-sqlgen
A tool to generate Go from SQL procedures.
Usage
Write procedures in a file:
CREATE PROCEDURE listUsers(role VARCHAR)
BEGIN
SELECT * FROM User WHERE role = ?
END
Run the generator:
go run 'git.sr.ht/~emersion/go-sqlgen' -q operations.sql -o generated.go
This will generate Go code with raw queries and a Prepare
method:
log.Printf("Raw SQL: %v", queries.Raw.listUsers)
stmts, err := queries.Prepare(ctx, db)
if err != nil {
log.Fatalf("failed to prepare queries: %v", err)
}
rows, err := stmts.listUsers.Query(ctx, "admin")
if err != nil {
log.Fatalf("failed to execute query: %v", err)
}
defer rows.Close()
// iterate over rows...
License
MIT