/sqlc

Generate type safe Go from SQL with streaming support

Primary LanguageGoMIT LicenseMIT

sqlc: A SQL Compiler

go Go Report Card

sqlc generates type-safe code from SQL. Here's how it works:

  1. You write queries in SQL.
  2. You run sqlc to generate code with type-safe interfaces to those queries.
  3. You write application code that calls the generated code.

Check out an interactive example to see it in action, and the introductory blog post for the motivation behind sqlc.

Streaming support

This fork contains streaming support using an :iter keyword. The keyword is implemented for both Go and Kotlin generated code.

Example :iter query:

-- name: StreamAuthors :iter
SELECT * from authors;

Will produce query functions like:

StreamAuthors(ctx context.Context, iter func(author Author) error) error

The iter function will be called for each row in the sql result. You are responsible for the actual streaming within the passed function. Example:

w := initSomeWriter()
iter := func(author Author) error {
    err = w.Write(author)
    if err != nil {
        return err
    }
    return nil
}

err := queries.StreamAuthors(ctx, iter)

Also note that if the iter function return an error, the streaming query will close and return the error.

Building this fork

Clone and run CGO_ENABLED=0 go build ./cmd/sqlc/main.go from the project root.

Overview

Supported languages

Additional languages can be added via plugins.

Acknowledgments

sqlc was inspired by PugSQL and HugSQL.