snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
404
examples/batch/postgresql/batch.go
Normal file
404
examples/batch/postgresql/batch.go
Normal file
@@ -0,0 +1,404 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: batch.go
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBatchAlreadyClosed = errors.New("batch already closed")
|
||||
)
|
||||
|
||||
const booksByYear = `-- name: BooksByYear :batchmany
|
||||
SELECT book_id, author_id, isbn, book_type, title, year, available, tags FROM books
|
||||
WHERE year = $1
|
||||
`
|
||||
|
||||
type BooksByYearBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (q *Queries) BooksByYear(ctx context.Context, year []int32) *BooksByYearBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range year {
|
||||
vals := []any{
|
||||
a,
|
||||
}
|
||||
batch.Queue(booksByYear, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &BooksByYearBatchResults{br, len(year), false}
|
||||
}
|
||||
|
||||
func (b *BooksByYearBatchResults) Query(f func(int, []Book, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
var items []Book
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, items, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
err := func() error {
|
||||
rows, err := b.br.Query()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var i Book
|
||||
if err := rows.Scan(
|
||||
&i.BookID,
|
||||
&i.AuthorID,
|
||||
&i.Isbn,
|
||||
&i.BookType,
|
||||
&i.Title,
|
||||
&i.Year,
|
||||
&i.Available,
|
||||
&i.Tags,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
return rows.Err()
|
||||
}()
|
||||
if f != nil {
|
||||
f(t, items, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BooksByYearBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const createBook = `-- name: CreateBook :batchone
|
||||
INSERT INTO books (
|
||||
author_id,
|
||||
isbn,
|
||||
book_type,
|
||||
title,
|
||||
year,
|
||||
available,
|
||||
tags
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
)
|
||||
RETURNING book_id, author_id, isbn, book_type, title, year, available, tags
|
||||
`
|
||||
|
||||
type CreateBookBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
type CreateBookParams struct {
|
||||
AuthorID int32 `json:"author_id"`
|
||||
Isbn string `json:"isbn"`
|
||||
BookType BookType `json:"book_type"`
|
||||
Title string `json:"title"`
|
||||
Year int32 `json:"year"`
|
||||
Available pgtype.Timestamptz `json:"available"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateBook(ctx context.Context, arg []CreateBookParams) *CreateBookBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range arg {
|
||||
vals := []any{
|
||||
a.AuthorID,
|
||||
a.Isbn,
|
||||
a.BookType,
|
||||
a.Title,
|
||||
a.Year,
|
||||
a.Available,
|
||||
a.Tags,
|
||||
}
|
||||
batch.Queue(createBook, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &CreateBookBatchResults{br, len(arg), false}
|
||||
}
|
||||
|
||||
func (b *CreateBookBatchResults) QueryRow(f func(int, Book, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
var i Book
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, i, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
row := b.br.QueryRow()
|
||||
err := row.Scan(
|
||||
&i.BookID,
|
||||
&i.AuthorID,
|
||||
&i.Isbn,
|
||||
&i.BookType,
|
||||
&i.Title,
|
||||
&i.Year,
|
||||
&i.Available,
|
||||
&i.Tags,
|
||||
)
|
||||
if f != nil {
|
||||
f(t, i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *CreateBookBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const deleteBook = `-- name: DeleteBook :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1
|
||||
`
|
||||
|
||||
type DeleteBookBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteBook(ctx context.Context, bookID []int32) *DeleteBookBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range bookID {
|
||||
vals := []any{
|
||||
a,
|
||||
}
|
||||
batch.Queue(deleteBook, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &DeleteBookBatchResults{br, len(bookID), false}
|
||||
}
|
||||
|
||||
func (b *DeleteBookBatchResults) Exec(f func(int, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err := b.br.Exec()
|
||||
if f != nil {
|
||||
f(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DeleteBookBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const deleteBookNamedFunc = `-- name: DeleteBookNamedFunc :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1
|
||||
`
|
||||
|
||||
type DeleteBookNamedFuncBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteBookNamedFunc(ctx context.Context, bookID []int32) *DeleteBookNamedFuncBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range bookID {
|
||||
vals := []any{
|
||||
a,
|
||||
}
|
||||
batch.Queue(deleteBookNamedFunc, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &DeleteBookNamedFuncBatchResults{br, len(bookID), false}
|
||||
}
|
||||
|
||||
func (b *DeleteBookNamedFuncBatchResults) Exec(f func(int, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err := b.br.Exec()
|
||||
if f != nil {
|
||||
f(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DeleteBookNamedFuncBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const deleteBookNamedSign = `-- name: DeleteBookNamedSign :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1
|
||||
`
|
||||
|
||||
type DeleteBookNamedSignBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteBookNamedSign(ctx context.Context, bookID []int32) *DeleteBookNamedSignBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range bookID {
|
||||
vals := []any{
|
||||
a,
|
||||
}
|
||||
batch.Queue(deleteBookNamedSign, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &DeleteBookNamedSignBatchResults{br, len(bookID), false}
|
||||
}
|
||||
|
||||
func (b *DeleteBookNamedSignBatchResults) Exec(f func(int, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err := b.br.Exec()
|
||||
if f != nil {
|
||||
f(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DeleteBookNamedSignBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const getBiography = `-- name: GetBiography :batchone
|
||||
SELECT biography FROM authors
|
||||
WHERE author_id = $1
|
||||
`
|
||||
|
||||
type GetBiographyBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (q *Queries) GetBiography(ctx context.Context, authorID []int32) *GetBiographyBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range authorID {
|
||||
vals := []any{
|
||||
a,
|
||||
}
|
||||
batch.Queue(getBiography, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &GetBiographyBatchResults{br, len(authorID), false}
|
||||
}
|
||||
|
||||
func (b *GetBiographyBatchResults) QueryRow(f func(int, []byte, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
var biography []byte
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, biography, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
row := b.br.QueryRow()
|
||||
err := row.Scan(&biography)
|
||||
if f != nil {
|
||||
f(t, biography, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *GetBiographyBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
|
||||
const updateBook = `-- name: UpdateBook :batchexec
|
||||
UPDATE books
|
||||
SET title = $1, tags = $2
|
||||
WHERE book_id = $3
|
||||
`
|
||||
|
||||
type UpdateBookBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
type UpdateBookParams struct {
|
||||
Title string `json:"title"`
|
||||
Tags []string `json:"tags"`
|
||||
BookID int32 `json:"book_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateBook(ctx context.Context, arg []UpdateBookParams) *UpdateBookBatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range arg {
|
||||
vals := []any{
|
||||
a.Title,
|
||||
a.Tags,
|
||||
a.BookID,
|
||||
}
|
||||
batch.Queue(updateBook, vals...)
|
||||
}
|
||||
br := q.db.SendBatch(ctx, batch)
|
||||
return &UpdateBookBatchResults{br, len(arg), false}
|
||||
}
|
||||
|
||||
func (b *UpdateBookBatchResults) Exec(f func(int, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err := b.br.Exec()
|
||||
if f != nil {
|
||||
f(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *UpdateBookBatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
33
examples/batch/postgresql/db.go
Normal file
33
examples/batch/postgresql/db.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...any) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...any) pgx.Row
|
||||
SendBatch(context.Context, *pgx.Batch) pgx.BatchResults
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
153
examples/batch/postgresql/db_test.go
Normal file
153
examples/batch/postgresql/db_test.go
Normal file
@@ -0,0 +1,153 @@
|
||||
//go:build examples
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
|
||||
)
|
||||
|
||||
func TestBatchBooks(t *testing.T) {
|
||||
uri := local.PostgreSQL(t, []string{"schema.sql"})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
db, err := pgx.Connect(ctx, uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close(ctx)
|
||||
|
||||
dq := New(db)
|
||||
|
||||
// create an author
|
||||
a, err := dq.CreateAuthor(ctx, "Unknown Master")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
now := pgtype.Timestamptz{Time: time.Now(), Valid: true}
|
||||
|
||||
// batch insert new books
|
||||
newBooksParams := []CreateBookParams{
|
||||
{
|
||||
AuthorID: a.AuthorID,
|
||||
Isbn: "1",
|
||||
Title: "my book title",
|
||||
BookType: BookTypeFICTION,
|
||||
Year: 2016,
|
||||
Available: now,
|
||||
Tags: []string{},
|
||||
},
|
||||
{
|
||||
AuthorID: a.AuthorID,
|
||||
Isbn: "2",
|
||||
Title: "the second book",
|
||||
BookType: BookTypeFICTION,
|
||||
Year: 2016,
|
||||
Available: now,
|
||||
Tags: []string{"cool", "unique"},
|
||||
},
|
||||
{
|
||||
AuthorID: a.AuthorID,
|
||||
Isbn: "3",
|
||||
Title: "the third book",
|
||||
BookType: BookTypeFICTION,
|
||||
Year: 2001,
|
||||
Available: now,
|
||||
Tags: []string{"cool"},
|
||||
},
|
||||
{
|
||||
AuthorID: a.AuthorID,
|
||||
Isbn: "4",
|
||||
Title: "4th place finisher",
|
||||
BookType: BookTypeNONFICTION,
|
||||
Year: 2011,
|
||||
Available: now,
|
||||
Tags: []string{"other"},
|
||||
},
|
||||
}
|
||||
newBooks := make([]Book, len(newBooksParams))
|
||||
var cnt int
|
||||
dq.CreateBook(ctx, newBooksParams).QueryRow(func(i int, b Book, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed inserting book (%s): %s", b.Title, err)
|
||||
}
|
||||
newBooks[i] = b
|
||||
cnt = i
|
||||
})
|
||||
// first i was 0, so add 1
|
||||
cnt++
|
||||
numBooksExpected := len(newBooks)
|
||||
if cnt != numBooksExpected {
|
||||
t.Fatalf("expected to insert %d books; got %d", numBooksExpected, cnt)
|
||||
}
|
||||
|
||||
// batch update the title and tags
|
||||
updateBooksParams := []UpdateBookParams{
|
||||
{
|
||||
BookID: newBooks[1].BookID,
|
||||
Title: "changed second title",
|
||||
Tags: []string{"cool", "disastor"},
|
||||
},
|
||||
}
|
||||
dq.UpdateBook(ctx, updateBooksParams).Exec(func(i int, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("error updating book %d: %s", updateBooksParams[i].BookID, err)
|
||||
}
|
||||
})
|
||||
|
||||
// batch many to retrieve books by year
|
||||
selectBooksByTitleYearParams := []int32{2001, 2016}
|
||||
var books0 []Book
|
||||
dq.BooksByYear(ctx, selectBooksByTitleYearParams).Query(func(i int, books []Book, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("num books for %d: %d", selectBooksByTitleYearParams[i], len(books))
|
||||
books0 = append(books0, books...)
|
||||
})
|
||||
|
||||
for _, book := range books0 {
|
||||
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
|
||||
author, err := dq.GetAuthor(ctx, book.AuthorID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
|
||||
}
|
||||
|
||||
// batch delete books
|
||||
deleteBooksParams := make([]int32, len(newBooks))
|
||||
for i, book := range newBooks {
|
||||
deleteBooksParams[i] = book.BookID
|
||||
}
|
||||
batchDelete := dq.DeleteBook(ctx, deleteBooksParams)
|
||||
numDeletesProcessed := 0
|
||||
wantNumDeletesProcessed := 2
|
||||
batchDelete.Exec(func(i int, err error) {
|
||||
if err != nil && err.Error() != "batch already closed" {
|
||||
t.Fatalf("error deleting book %d: %s", deleteBooksParams[i], err)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
numDeletesProcessed++
|
||||
}
|
||||
|
||||
if i == wantNumDeletesProcessed-1 {
|
||||
// close batch operation before processing all errors from delete operation
|
||||
if err := batchDelete.Close(); err != nil {
|
||||
t.Fatalf("failed to close batch operation: %s", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
if numDeletesProcessed != wantNumDeletesProcessed {
|
||||
t.Fatalf("expected Close to short-circuit record processing (expected %d; got %d)", wantNumDeletesProcessed, numDeletesProcessed)
|
||||
}
|
||||
}
|
||||
71
examples/batch/postgresql/models.go
Normal file
71
examples/batch/postgresql/models.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type BookType string
|
||||
|
||||
const (
|
||||
BookTypeFICTION BookType = "FICTION"
|
||||
BookTypeNONFICTION BookType = "NONFICTION"
|
||||
)
|
||||
|
||||
func (e *BookType) Scan(src any) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = BookType(s)
|
||||
case string:
|
||||
*e = BookType(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for BookType: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullBookType struct {
|
||||
BookType BookType `json:"book_type"`
|
||||
Valid bool `json:"valid"` // Valid is true if BookType is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullBookType) Scan(value any) error {
|
||||
if value == nil {
|
||||
ns.BookType, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.BookType.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullBookType) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.BookType), nil
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
AuthorID int32 `json:"author_id"`
|
||||
Name string `json:"name"`
|
||||
Biography []byte `json:"biography"`
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
BookID int32 `json:"book_id"`
|
||||
AuthorID int32 `json:"author_id"`
|
||||
Isbn string `json:"isbn"`
|
||||
BookType BookType `json:"book_type"`
|
||||
Title string `json:"title"`
|
||||
Year int32 `json:"year"`
|
||||
Available pgtype.Timestamptz `json:"available"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
26
examples/batch/postgresql/querier.go
Normal file
26
examples/batch/postgresql/querier.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
BooksByYear(ctx context.Context, year []int32) *BooksByYearBatchResults
|
||||
CreateAuthor(ctx context.Context, name string) (Author, error)
|
||||
CreateBook(ctx context.Context, arg []CreateBookParams) *CreateBookBatchResults
|
||||
DeleteBook(ctx context.Context, bookID []int32) *DeleteBookBatchResults
|
||||
DeleteBookExecResult(ctx context.Context, bookID int32) (pgconn.CommandTag, error)
|
||||
DeleteBookNamedFunc(ctx context.Context, bookID []int32) *DeleteBookNamedFuncBatchResults
|
||||
DeleteBookNamedSign(ctx context.Context, bookID []int32) *DeleteBookNamedSignBatchResults
|
||||
GetAuthor(ctx context.Context, authorID int32) (Author, error)
|
||||
GetBiography(ctx context.Context, authorID []int32) *GetBiographyBatchResults
|
||||
UpdateBook(ctx context.Context, arg []UpdateBookParams) *UpdateBookBatchResults
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
56
examples/batch/postgresql/query.sql
Normal file
56
examples/batch/postgresql/query.sql
Normal file
@@ -0,0 +1,56 @@
|
||||
-- name: GetAuthor :one
|
||||
SELECT * FROM authors
|
||||
WHERE author_id = $1;
|
||||
|
||||
-- name: DeleteBookExecResult :execresult
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1;
|
||||
|
||||
-- name: DeleteBook :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1;
|
||||
|
||||
-- name: DeleteBookNamedFunc :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = sqlc.arg (book_id);
|
||||
|
||||
-- name: DeleteBookNamedSign :batchexec
|
||||
DELETE FROM books
|
||||
WHERE book_id = @book_id;
|
||||
|
||||
-- name: BooksByYear :batchmany
|
||||
SELECT * FROM books
|
||||
WHERE year = $1;
|
||||
|
||||
-- name: CreateAuthor :one
|
||||
INSERT INTO authors (name) VALUES ($1)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateBook :batchone
|
||||
INSERT INTO books (
|
||||
author_id,
|
||||
isbn,
|
||||
book_type,
|
||||
title,
|
||||
year,
|
||||
available,
|
||||
tags
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateBook :batchexec
|
||||
UPDATE books
|
||||
SET title = $1, tags = $2
|
||||
WHERE book_id = $3;
|
||||
|
||||
-- name: GetBiography :batchone
|
||||
SELECT biography FROM authors
|
||||
WHERE author_id = $1;
|
||||
45
examples/batch/postgresql/query.sql.go
Normal file
45
examples/batch/postgresql/query.sql.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: query.sql
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
const createAuthor = `-- name: CreateAuthor :one
|
||||
INSERT INTO authors (name) VALUES ($1)
|
||||
RETURNING author_id, name, biography
|
||||
`
|
||||
|
||||
func (q *Queries) CreateAuthor(ctx context.Context, name string) (Author, error) {
|
||||
row := q.db.QueryRow(ctx, createAuthor, name)
|
||||
var i Author
|
||||
err := row.Scan(&i.AuthorID, &i.Name, &i.Biography)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteBookExecResult = `-- name: DeleteBookExecResult :execresult
|
||||
DELETE FROM books
|
||||
WHERE book_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteBookExecResult(ctx context.Context, bookID int32) (pgconn.CommandTag, error) {
|
||||
return q.db.Exec(ctx, deleteBookExecResult, bookID)
|
||||
}
|
||||
|
||||
const getAuthor = `-- name: GetAuthor :one
|
||||
SELECT author_id, name, biography FROM authors
|
||||
WHERE author_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetAuthor(ctx context.Context, authorID int32) (Author, error) {
|
||||
row := q.db.QueryRow(ctx, getAuthor, authorID)
|
||||
var i Author
|
||||
err := row.Scan(&i.AuthorID, &i.Name, &i.Biography)
|
||||
return i, err
|
||||
}
|
||||
21
examples/batch/postgresql/schema.sql
Normal file
21
examples/batch/postgresql/schema.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
CREATE TABLE authors (
|
||||
author_id SERIAL PRIMARY KEY,
|
||||
name text NOT NULL DEFAULT '',
|
||||
biography JSONB
|
||||
);
|
||||
|
||||
CREATE TYPE book_type AS ENUM (
|
||||
'FICTION',
|
||||
'NONFICTION'
|
||||
);
|
||||
|
||||
CREATE TABLE books (
|
||||
book_id SERIAL PRIMARY KEY,
|
||||
author_id integer NOT NULL REFERENCES authors(author_id),
|
||||
isbn text NOT NULL DEFAULT '' UNIQUE,
|
||||
book_type book_type NOT NULL DEFAULT 'FICTION',
|
||||
title text NOT NULL DEFAULT '',
|
||||
year integer NOT NULL DEFAULT 2000,
|
||||
available timestamp with time zone NOT NULL DEFAULT 'NOW()',
|
||||
tags varchar[] NOT NULL DEFAULT '{}'
|
||||
);
|
||||
Reference in New Issue
Block a user