[ANN] sqlc: Generate type-safe Go from SQL

143 views
Skip to first unread message

ky...@kyleconroy.com

unread,
Sep 3, 2019, 10:43:58 AM9/3/19
to golang-nuts
Hey folks,

I'd like to introduce sqlc[0], a tool that compiles SQL queries into type-safe, idiomatic Go. It also catches common errors in SQL such as mistyped column names and incorrect parameter numbering.

If this sounds like something that would be useful to you, take a read through the Getting Started[1] guide.

Cheers,
Kyle

[0] https://github.com/kyleconroy/sqlc
[1] https://github.com/kyleconroy/sqlc/blob/master/README.md#getting-started

Tyler Compton

unread,
Sep 3, 2019, 12:58:05 PM9/3/19
to ky...@kyleconroy.com, golang-nuts
This is very cool! It's an interesting compromise between using a fully featured ORM and interacting with SQL queries directly, and may be a good sweet spot.

Have you considered supporting documentation in generated code? Maybe by allowing for syntax like this for queries:

-- name: ListAuthors :many
--
-- ListAuthors retrieves all authors from the database blah blah.
SELECT * FROM authors
ORDER BY name;


... that would generate code like this:

// ListAuthors retrieves all authors from the database blah blah.
func (q *Queries) ListAuthors(ctx context.Context) ([]Author, error) {
    ...
}


I'm not too familiar with Postgres, but in MySQL you can create tables with comments. It looks like there's a similar feature in Postgres.

CREATE TABLE authors (
    id int,
    name VARCHAR(255) COMMENT "The name of the author who wrote the book",
    bio VARCHAR(512) COMMENT "A short description of the author"
) COMMENT "Author is the author of a piece of literature.";

It might be nice if those comments propagated to the struct definition automatically.

// Author is the author of a piece of literature.
type Author struct {
    ID   int64
    // The name of the author who wrote the book
    Name string
    // A short description of the author
    Bio  sql.NullString
}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/399e7595-1f79-4fc5-a6bd-498b312168d5%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages