go-sqlgen

command module
v0.0.0-...-8458982 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 10 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL