Announcing sqlc: Compile SQL queries to type-safe Go

188 views
Skip to first unread message

ky...@conroy.org

unread,
Dec 11, 2019, 2:49:03 PM12/11/19
to golang-nuts
Hey gophers,

I'm excited to announce sqlc, my project for turning SQL into type-safe Go. With sqlc, the following SQL:


CREATE TABLE authors
(
  id BIGSERIAL PRIMARY KEY
,
  name text NOT NULL
, bio text
);


-- name: GetAuthor :one
SELECT
* FROM authors WHERE id = $1 LIMIT 1;

gets turned into this Go

package db

import (
 
"context"
 
"database/sql"
)

type
Author struct {
  ID   int64
 
Name string
 
Bio  sql.NullString
}

const getAuthor = `-- name: GetAuthor :one
SELECT id, name, bio FROM authors
WHERE id = $1 LIMIT 1
`


type
Queries struct {
  db
*sql.DB
}

func
(q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) {
  row
:= q.db.QueryRowContext(ctx, getAuthor, id)
 
var i Author
  err
:= row.Scan(&i.ID, &i.Name, &i.Bio)
 
return i, err
}

I wrote up a larger piece about why I started the project here: https://conroy.org/introducing-sqlc. It includes a guided walk through and more example code.

Happy to answer any questions you have about the project.

Cheers,
Kyle

Vasiliy Tolstov

unread,
Dec 11, 2019, 3:18:20 PM12/11/19
to ky...@conroy.org, golang-nuts
Thanks! But how about real world cases like joining 5 tables and using
having and other stuff?

ср, 11 дек. 2019 г. в 22:48, <ky...@conroy.org>:
> --
> 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/2d87986d-fc00-4fc2-afa3-352dedc2f9b7%40googlegroups.com.



--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

ky...@conroy.org

unread,
Dec 11, 2019, 3:39:53 PM12/11/19
to golang-nuts
Vasiliy,

Take a look at the supported examples. sqlc has no problem handling five joins and a having clause.

Kyle
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

nanmu42

unread,
Dec 11, 2019, 9:03:44 PM12/11/19
to golang-nuts
I think this is brilliant!

Hoping to use it in my next project.

Reply all
Reply to author
Forward
0 new messages