Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f86fb4c
add sqlc.embed
nickjackson Nov 30, 2022
54207db
only allow alias or table name in sqlc.embed()
nickjackson Nov 30, 2022
deca8fd
add tests
nickjackson Nov 30, 2022
14923ff
regenerate other tests
nickjackson Nov 30, 2022
238479b
setup bufbuild to generate proto (#1974)
nickjackson Dec 2, 2022
eaa6e07
build(deps): bump github.com/go-sql-driver/mysql from 1.6.0 to 1.7.0 …
dependabot[bot] Dec 7, 2022
a6d77b0
tools: regenerate scripts skips dirs that contains diff exec command …
mcdoker18 Dec 7, 2022
f156138
build(deps): bump github.com/jackc/pgtype from 1.12.0 to 1.13.0 (#1978)
dependabot[bot] Dec 7, 2022
c10dea0
build(deps): bump golang from 1.19.3 to 1.19.4 (#1992)
dependabot[bot] Dec 9, 2022
37cb1cb
build(deps): bump certifi from 2020.12.5 to 2022.12.7 in /docs (#1993)
dependabot[bot] Dec 10, 2022
299c3fc
plugins: Upgrade to wasmtime 3.0.1 (#2009)
kyleconroy Dec 28, 2022
9b0e8d5
add version to bug template (#2000)
davideimola Jan 2, 2023
8cca045
fix: the enum Value method returns correct type (#1996)
mcdoker18 Jan 2, 2023
532481a
build(deps): bump golang from 1.19.4 to 1.19.5 (#2016)
dependabot[bot] Jan 17, 2023
b19027c
update uuid docs for mysql (#2019)
mikemackintosh Jan 17, 2023
0e12a9d
Manual fix for incorrect handling of "calories" (#2018)
mpyw Jan 17, 2023
f4420ea
docs: update samples for HOW-TO GUIDES (#1953)
d-tsuji Jan 17, 2023
6509c0d
cmd: Generate packages in parallel (#2026)
kyleconroy Jan 19, 2023
ccfe922
internal/codegen: cache pattern matching compilations (#2028)
inconshreveable Jan 19, 2023
9a7ecb3
fix: Documentation for Inserting Rows (#2034)
tergel-996 Jan 30, 2023
f9ca81c
Changed documentation for json datatype. (#1997)
f1forhelp Jan 30, 2023
52e603d
build(deps): bump golang from 1.19.5 to 1.20.0 (#2045)
dependabot[bot] Feb 3, 2023
fb75b41
fix: Add import statements even if only pointer types exist (#2046)
zaneli Feb 3, 2023
fdc3f40
add sqlc.embed
nickjackson Nov 30, 2022
3b99e6b
only allow alias or table name in sqlc.embed()
nickjackson Nov 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/BUG_REPORT.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ body:
description: What version of sqlc are you running? If you don't know, run `sqlc version`.
multiple: false
options:
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# STEP 1: Build sqlc
FROM golang:1.19.3 AS builder
FROM golang:1.20.0 AS builder

COPY . /workspace
WORKDIR /workspace
Expand Down
17 changes: 3 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build build-endtoend test test-ci test-examples test-endtoend regen start psql mysqlsh
.PHONY: build build-endtoend test test-ci test-examples test-endtoend regen start psql mysqlsh proto

build:
go build ./...
Expand Down Expand Up @@ -44,16 +44,5 @@ psql:
mysqlsh:
mysqlsh --sql --user root --password mysecretpassword --database dinotest 127.0.0.1:3306

# $ protoc --version
# libprotoc 3.19.1
# $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# $ go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto
proto: internal/plugin/codegen.pb.go

internal/plugin/codegen.pb.go: protos/plugin/codegen.proto
protoc -I ./protos \
--go_out=. \
--go_opt=module=github.com/kyleconroy/sqlc \
--go-vtproto_out=. \
--go-vtproto_opt=module=github.com/kyleconroy/sqlc,features=marshal+unmarshal+size \
./protos/plugin/codegen.proto
proto:
buf generate
10 changes: 10 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1
managed:
enabled: true
plugins:
- remote: buf.build/protocolbuffers/plugins/go:v1.28.1-1
out: internal
opt: paths=source_relative
- remote: buf.build/planetscale/plugins/vtproto:v0.3.0
out: internal
opt: paths=source_relative
3 changes: 3 additions & 0 deletions buf.work.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
directories:
- protos
2 changes: 1 addition & 1 deletion docs/howto/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand Down
111 changes: 47 additions & 64 deletions docs/howto/insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand All @@ -45,24 +45,28 @@ func (q *Queries) CreateAuthor(ctx context.Context, bio string) error {
sqlc has full support for the `RETURNING` statement.

```sql
-- Example queries for sqlc
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
bio text NOT NULL
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

-- name: Delete :exec
DELETE FROM authors WHERE id = $1;

-- name: DeleteAffected :execrows
DELETE FROM authors WHERE id = $1;
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;

-- name: DeleteID :one
DELETE FROM authors WHERE id = $1
-- name: CreateAuthorAndReturnId :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING id;

-- name: DeleteAuthor :one
DELETE FROM authors WHERE id = $1
RETURNING *;
```

```go
Expand All @@ -73,67 +77,46 @@ import (
"database/sql"
)

type Author struct {
ID int
Bio string
}

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
QueryRowContext(context.Context, string, ...interface{}) error
}

func New(db DBTX) *Queries {
return &Queries{db: db}
}

type Queries struct {
db DBTX
}

const delete = `-- name: Delete :exec
DELETE FROM authors WHERE id = $1
const createAuthor = `-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING id, name, bio
`

func (q *Queries) Delete(ctx context.Context, id int) error {
_, err := q.db.ExecContext(ctx, delete, id)
return err
type CreateAuthorParams struct {
Name string
Bio sql.NullString
}

const deleteAffected = `-- name: DeleteAffected :execrows
DELETE FROM authors WHERE id = $1
`

func (q *Queries) DeleteAffected(ctx context.Context, id int) (int64, error) {
result, err := q.db.ExecContext(ctx, deleteAffected, id)
if err != nil {
return 0, err
}
return result.RowsAffected()
func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) {
row := q.db.QueryRowContext(ctx, createAuthor, arg.Name, arg.Bio)
var i Author
err := row.Scan(&i.ID, &i.Name, &i.Bio)
return i, err
}

const deleteID = `-- name: DeleteID :one
DELETE FROM authors WHERE id = $1
const createAuthorAndReturnId = `-- name: CreateAuthorAndReturnId :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING id
`

func (q *Queries) DeleteID(ctx context.Context, id int) (int, error) {
row := q.db.QueryRowContext(ctx, deleteID, id)
var i int
err := row.Scan(&i)
return i, err
type CreateAuthorAndReturnIdParams struct {
Name string
Bio sql.NullString
}

const deleteAuthor = `-- name: DeleteAuthor :one
DELETE FROM authors WHERE id = $1
RETURNING id, bio
`

func (q *Queries) DeleteAuthor(ctx context.Context, id int) (Author, error) {
row := q.db.QueryRowContext(ctx, deleteAuthor, id)
var i Author
err := row.Scan(&i.ID, &i.Bio)
return i, err
func (q *Queries) CreateAuthorAndReturnId(ctx context.Context, arg CreateAuthorAndReturnIdParams) (int64, error) {
row := q.db.QueryRowContext(ctx, createAuthorAndReturnId, arg.Name, arg.Bio)
var id int64
err := row.Scan(&id)
return id, err
}
```

Expand Down
12 changes: 6 additions & 6 deletions docs/howto/prepared_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package db
import (
"context"
"database/sql"
"fmt"
)

type Record struct {
ID int
ID int32
}

type DBTX interface {
Expand All @@ -38,7 +39,7 @@ func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
q := Queries{db: db}
var err error
if q.getRecordStmt, err = db.PrepareContext(ctx, getRecord); err != nil {
return nil, err
return nil, fmt.Errorf("error preparing query GetRecord: %w", err)
}
return &q, nil
}
Expand Down Expand Up @@ -73,11 +74,10 @@ SELECT id FROM records
WHERE id = $1
`

func (q *Queries) GetRecord(ctx context.Context, id int) (Record, error) {
func (q *Queries) GetRecord(ctx context.Context, id int32) (int32, error) {
row := q.queryRow(ctx, q.getRecordStmt, getRecord, id)
var i Record
err := row.Scan(&i.ID)
return i, err
err := row.Scan(&id)
return id, err
}
```

10 changes: 8 additions & 2 deletions docs/howto/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand All @@ -52,6 +52,12 @@ func (q *Queries) UpdateAuthorBios(ctx context.Context, bio string) error {
If your query has more than one parameter, your Go method will accept a
`Params` struct.

```sql
-- name: UpdateAuthor :exec
UPDATE authors SET bio = $2
WHERE id = $1;
```

```go
package db

Expand All @@ -61,7 +67,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand Down
20 changes: 18 additions & 2 deletions docs/reference/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ type Author struct {
}
```

For MySQL, there is no native `uuid` data type. When using `UUID_TO_BIN` to store a `UUID()`, the underlying field type is `BINARY(16)` which by default sqlc would interpret this to `sql.NullString`. To have sqlc automatically convert these fields to a `uuid.UUID` type, use an overide on the column storing the `uuid`.
```json
{
"overrides": [
{
"column": "*.uuid",
"go_type": "github.com/google/uuid.UUID"
}
]
}
```

## JSON

By default, sqlc will generate the `[]byte`, `pgtype.JSON` or `json.RawMessage` for JSON column type.
Expand Down Expand Up @@ -160,7 +172,11 @@ CREATE TABLE books (
"overrides": [
{
"column": "books.data",
"go_type": "*example.com/db/dto.BookData"
"go_type": {
"import":"example/db",
"package": "dto",
"type":"BookData"
}
}
]
}
Expand All @@ -176,4 +192,4 @@ import (
type Book struct {
Data *dto.BookData
}
```
```
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alabaster==0.7.12
Babel==2.9.1
certifi==2020.12.5
certifi==2022.12.7
chardet==4.0.0
commonmark==0.9.1
docutils==0.16
Expand Down
2 changes: 1 addition & 1 deletion examples/batch/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/booktest/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/booktest/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/ondeck/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/ondeck/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading