~emersion/go-sqlgen

A tool to generate Go from SQL procedures
Add support for comments inside procedures
readme: update CLI flags
readme: drop unnecessary newline

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~emersion/go-sqlgen
read/write
git@git.sr.ht:~emersion/go-sqlgen

You can also use your local clone with git send-email.

#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