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:
95
examples/ondeck/postgresql/city.sql.go
Normal file
95
examples/ondeck/postgresql/city.sql.go
Normal file
@@ -0,0 +1,95 @@
|
||||
//go:build examples
|
||||
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: city.sql
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createCity = `-- name: CreateCity :one
|
||||
INSERT INTO city (
|
||||
name,
|
||||
slug
|
||||
) VALUES (
|
||||
$1,
|
||||
$2
|
||||
) RETURNING slug, name
|
||||
`
|
||||
|
||||
type CreateCityParams struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
// Create a new city. The slug must be unique.
|
||||
// This is the second line of the comment
|
||||
// This is the third line
|
||||
func (q *Queries) CreateCity(ctx context.Context, arg CreateCityParams) (City, error) {
|
||||
row := q.queryRow(ctx, q.createCityStmt, createCity, arg.Name, arg.Slug)
|
||||
var i City
|
||||
err := row.Scan(&i.Slug, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getCity = `-- name: GetCity :one
|
||||
SELECT slug, name
|
||||
FROM city
|
||||
WHERE slug = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetCity(ctx context.Context, slug string) (City, error) {
|
||||
row := q.queryRow(ctx, q.getCityStmt, getCity, slug)
|
||||
var i City
|
||||
err := row.Scan(&i.Slug, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listCities = `-- name: ListCities :many
|
||||
SELECT slug, name
|
||||
FROM city
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListCities(ctx context.Context) ([]City, error) {
|
||||
rows, err := q.query(ctx, q.listCitiesStmt, listCities)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []City
|
||||
for rows.Next() {
|
||||
var i City
|
||||
if err := rows.Scan(&i.Slug, &i.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateCityName = `-- name: UpdateCityName :exec
|
||||
UPDATE city
|
||||
SET name = $2
|
||||
WHERE slug = $1
|
||||
`
|
||||
|
||||
type UpdateCityNameParams struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateCityName(ctx context.Context, arg UpdateCityNameParams) error {
|
||||
_, err := q.exec(ctx, q.updateCityNameStmt, updateCityName, arg.Slug, arg.Name)
|
||||
return err
|
||||
}
|
||||
180
examples/ondeck/postgresql/db.go
Normal file
180
examples/ondeck/postgresql/db.go
Normal file
@@ -0,0 +1,180 @@
|
||||
//go:build examples
|
||||
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
|
||||
q := Queries{db: db}
|
||||
var err error
|
||||
if q.createCityStmt, err = db.PrepareContext(ctx, createCity); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query CreateCity: %w", err)
|
||||
}
|
||||
if q.createVenueStmt, err = db.PrepareContext(ctx, createVenue); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query CreateVenue: %w", err)
|
||||
}
|
||||
if q.deleteVenueStmt, err = db.PrepareContext(ctx, deleteVenue); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query DeleteVenue: %w", err)
|
||||
}
|
||||
if q.getCityStmt, err = db.PrepareContext(ctx, getCity); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query GetCity: %w", err)
|
||||
}
|
||||
if q.getVenueStmt, err = db.PrepareContext(ctx, getVenue); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query GetVenue: %w", err)
|
||||
}
|
||||
if q.listCitiesStmt, err = db.PrepareContext(ctx, listCities); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query ListCities: %w", err)
|
||||
}
|
||||
if q.listVenuesStmt, err = db.PrepareContext(ctx, listVenues); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query ListVenues: %w", err)
|
||||
}
|
||||
if q.updateCityNameStmt, err = db.PrepareContext(ctx, updateCityName); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query UpdateCityName: %w", err)
|
||||
}
|
||||
if q.updateVenueNameStmt, err = db.PrepareContext(ctx, updateVenueName); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query UpdateVenueName: %w", err)
|
||||
}
|
||||
if q.venueCountByCityStmt, err = db.PrepareContext(ctx, venueCountByCity); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query VenueCountByCity: %w", err)
|
||||
}
|
||||
return &q, nil
|
||||
}
|
||||
|
||||
func (q *Queries) Close() error {
|
||||
var err error
|
||||
if q.createCityStmt != nil {
|
||||
if cerr := q.createCityStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing createCityStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.createVenueStmt != nil {
|
||||
if cerr := q.createVenueStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing createVenueStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.deleteVenueStmt != nil {
|
||||
if cerr := q.deleteVenueStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing deleteVenueStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.getCityStmt != nil {
|
||||
if cerr := q.getCityStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing getCityStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.getVenueStmt != nil {
|
||||
if cerr := q.getVenueStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing getVenueStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.listCitiesStmt != nil {
|
||||
if cerr := q.listCitiesStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing listCitiesStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.listVenuesStmt != nil {
|
||||
if cerr := q.listVenuesStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing listVenuesStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.updateCityNameStmt != nil {
|
||||
if cerr := q.updateCityNameStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing updateCityNameStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.updateVenueNameStmt != nil {
|
||||
if cerr := q.updateVenueNameStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing updateVenueNameStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.venueCountByCityStmt != nil {
|
||||
if cerr := q.venueCountByCityStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing venueCountByCityStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (q *Queries) exec(ctx context.Context, stmt *sql.Stmt, query string, args ...any) (sql.Result, error) {
|
||||
switch {
|
||||
case stmt != nil && q.tx != nil:
|
||||
return q.tx.StmtContext(ctx, stmt).ExecContext(ctx, args...)
|
||||
case stmt != nil:
|
||||
return stmt.ExecContext(ctx, args...)
|
||||
default:
|
||||
return q.db.ExecContext(ctx, query, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queries) query(ctx context.Context, stmt *sql.Stmt, query string, args ...any) (*sql.Rows, error) {
|
||||
switch {
|
||||
case stmt != nil && q.tx != nil:
|
||||
return q.tx.StmtContext(ctx, stmt).QueryContext(ctx, args...)
|
||||
case stmt != nil:
|
||||
return stmt.QueryContext(ctx, args...)
|
||||
default:
|
||||
return q.db.QueryContext(ctx, query, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...any) *sql.Row {
|
||||
switch {
|
||||
case stmt != nil && q.tx != nil:
|
||||
return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...)
|
||||
case stmt != nil:
|
||||
return stmt.QueryRowContext(ctx, args...)
|
||||
default:
|
||||
return q.db.QueryRowContext(ctx, query, args...)
|
||||
}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
tx *sql.Tx
|
||||
createCityStmt *sql.Stmt
|
||||
createVenueStmt *sql.Stmt
|
||||
deleteVenueStmt *sql.Stmt
|
||||
getCityStmt *sql.Stmt
|
||||
getVenueStmt *sql.Stmt
|
||||
listCitiesStmt *sql.Stmt
|
||||
listVenuesStmt *sql.Stmt
|
||||
updateCityNameStmt *sql.Stmt
|
||||
updateVenueNameStmt *sql.Stmt
|
||||
venueCountByCityStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
tx: tx,
|
||||
createCityStmt: q.createCityStmt,
|
||||
createVenueStmt: q.createVenueStmt,
|
||||
deleteVenueStmt: q.deleteVenueStmt,
|
||||
getCityStmt: q.getCityStmt,
|
||||
getVenueStmt: q.getVenueStmt,
|
||||
listCitiesStmt: q.listCitiesStmt,
|
||||
listVenuesStmt: q.listVenuesStmt,
|
||||
updateCityNameStmt: q.updateCityNameStmt,
|
||||
updateVenueNameStmt: q.updateVenueNameStmt,
|
||||
venueCountByCityStmt: q.venueCountByCityStmt,
|
||||
}
|
||||
}
|
||||
154
examples/ondeck/postgresql/db_test.go
Normal file
154
examples/ondeck/postgresql/db_test.go
Normal file
@@ -0,0 +1,154 @@
|
||||
//go:build examples
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
|
||||
)
|
||||
|
||||
func runOnDeckQueries(t *testing.T, q *Queries) {
|
||||
ctx := context.Background()
|
||||
|
||||
city, err := q.CreateCity(ctx, CreateCityParams{
|
||||
Slug: "san-francisco",
|
||||
Name: "San Francisco",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
venueID, err := q.CreateVenue(ctx, CreateVenueParams{
|
||||
Slug: "the-fillmore",
|
||||
Name: "The Fillmore",
|
||||
City: city.Slug,
|
||||
SpotifyPlaylist: "spotify:uri",
|
||||
Status: StatusOpen,
|
||||
Statuses: []Status{StatusOpen, StatusClosed},
|
||||
Tags: []string{"rock", "punk"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
venue, err := q.GetVenue(ctx, GetVenueParams{
|
||||
Slug: "the-fillmore",
|
||||
City: city.Slug,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(venue.ID, venueID); diff != "" {
|
||||
t.Errorf("venue ID mismatch:\n%s", diff)
|
||||
}
|
||||
|
||||
{
|
||||
actual, err := q.GetCity(ctx, city.Slug)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(actual, city); diff != "" {
|
||||
t.Errorf("get city mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
actual, err := q.VenueCountByCity(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(actual, []VenueCountByCityRow{
|
||||
{city.Slug, 1},
|
||||
}); diff != "" {
|
||||
t.Errorf("venue county mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
actual, err := q.ListCities(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(actual, []City{city}); diff != "" {
|
||||
t.Errorf("list city mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
actual, err := q.ListVenues(ctx, city.Slug)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(actual, []Venue{venue}); diff != "" {
|
||||
t.Errorf("list venue mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
err := q.UpdateCityName(ctx, UpdateCityNameParams{
|
||||
Slug: city.Slug,
|
||||
Name: "SF",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
id, err := q.UpdateVenueName(ctx, UpdateVenueNameParams{
|
||||
Slug: venue.Slug,
|
||||
Name: "Fillmore",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(id, venue.ID); diff != "" {
|
||||
t.Errorf("update venue mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
err := q.DeleteVenue(ctx, venue.Slug)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepared(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
uri := local.PostgreSQL(t, []string{"schema"})
|
||||
db, err := sql.Open("postgres", uri)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s", uri, err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
q, err := Prepare(context.Background(), db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
runOnDeckQueries(t, q)
|
||||
}
|
||||
|
||||
func TestQueries(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
uri := local.PostgreSQL(t, []string{"schema"})
|
||||
db, err := sql.Open("postgres", uri)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s", uri, err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
runOnDeckQueries(t, New(db))
|
||||
}
|
||||
77
examples/ondeck/postgresql/models.go
Normal file
77
examples/ondeck/postgresql/models.go
Normal file
@@ -0,0 +1,77 @@
|
||||
//go:build examples
|
||||
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Venues can be either open or closed
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusOpen Status = "op!en"
|
||||
StatusClosed Status = "clo@sed"
|
||||
)
|
||||
|
||||
func (e *Status) Scan(src any) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = Status(s)
|
||||
case string:
|
||||
*e = Status(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for Status: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullStatus struct {
|
||||
Status Status `json:"status"`
|
||||
Valid bool `json:"valid"` // Valid is true if Status is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullStatus) Scan(value any) error {
|
||||
if value == nil {
|
||||
ns.Status, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.Status.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullStatus) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.Status), nil
|
||||
}
|
||||
|
||||
type City struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Venues are places where muisc happens
|
||||
type Venue struct {
|
||||
ID int32 `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Statuses []Status `json:"statuses"`
|
||||
// This value appears in public URLs
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
City string `json:"city"`
|
||||
SpotifyPlaylist string `json:"spotify_playlist"`
|
||||
SongkickID sql.NullString `json:"songkick_id"`
|
||||
Tags []string `json:"tags"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
29
examples/ondeck/postgresql/querier.go
Normal file
29
examples/ondeck/postgresql/querier.go
Normal file
@@ -0,0 +1,29 @@
|
||||
//go:build examples
|
||||
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
// Create a new city. The slug must be unique.
|
||||
// This is the second line of the comment
|
||||
// This is the third line
|
||||
CreateCity(ctx context.Context, arg CreateCityParams) (City, error)
|
||||
CreateVenue(ctx context.Context, arg CreateVenueParams) (int32, error)
|
||||
DeleteVenue(ctx context.Context, slug string) error
|
||||
GetCity(ctx context.Context, slug string) (City, error)
|
||||
GetVenue(ctx context.Context, arg GetVenueParams) (Venue, error)
|
||||
ListCities(ctx context.Context) ([]City, error)
|
||||
ListVenues(ctx context.Context, city string) ([]Venue, error)
|
||||
UpdateCityName(ctx context.Context, arg UpdateCityNameParams) error
|
||||
UpdateVenueName(ctx context.Context, arg UpdateVenueNameParams) (int32, error)
|
||||
VenueCountByCity(ctx context.Context) ([]VenueCountByCityRow, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
26
examples/ondeck/postgresql/query/city.sql
Normal file
26
examples/ondeck/postgresql/query/city.sql
Normal file
@@ -0,0 +1,26 @@
|
||||
-- name: ListCities :many
|
||||
SELECT *
|
||||
FROM city
|
||||
ORDER BY name;
|
||||
|
||||
-- name: GetCity :one
|
||||
SELECT *
|
||||
FROM city
|
||||
WHERE slug = $1;
|
||||
|
||||
-- name: CreateCity :one
|
||||
-- Create a new city. The slug must be unique.
|
||||
-- This is the second line of the comment
|
||||
-- This is the third line
|
||||
INSERT INTO city (
|
||||
name,
|
||||
slug
|
||||
) VALUES (
|
||||
$1,
|
||||
$2
|
||||
) RETURNING *;
|
||||
|
||||
-- name: UpdateCityName :exec
|
||||
UPDATE city
|
||||
SET name = $2
|
||||
WHERE slug = $1;
|
||||
49
examples/ondeck/postgresql/query/venue.sql
Normal file
49
examples/ondeck/postgresql/query/venue.sql
Normal file
@@ -0,0 +1,49 @@
|
||||
-- name: ListVenues :many
|
||||
SELECT *
|
||||
FROM venue
|
||||
WHERE city = $1
|
||||
ORDER BY name;
|
||||
|
||||
-- name: DeleteVenue :exec
|
||||
DELETE FROM venue
|
||||
WHERE slug = $1 AND slug = $1;
|
||||
|
||||
-- name: GetVenue :one
|
||||
SELECT *
|
||||
FROM venue
|
||||
WHERE slug = $1 AND city = $2;
|
||||
|
||||
-- name: CreateVenue :one
|
||||
INSERT INTO venue (
|
||||
slug,
|
||||
name,
|
||||
city,
|
||||
created_at,
|
||||
spotify_playlist,
|
||||
status,
|
||||
statuses,
|
||||
tags
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
NOW(),
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
) RETURNING id;
|
||||
|
||||
-- name: UpdateVenueName :one
|
||||
UPDATE venue
|
||||
SET name = $2
|
||||
WHERE slug = $1
|
||||
RETURNING id;
|
||||
|
||||
-- name: VenueCountByCity :many
|
||||
SELECT
|
||||
city,
|
||||
count(*)
|
||||
FROM venue
|
||||
GROUP BY 1
|
||||
ORDER BY 1;
|
||||
4
examples/ondeck/postgresql/schema/0001_city.sql
Normal file
4
examples/ondeck/postgresql/schema/0001_city.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE city (
|
||||
slug text PRIMARY KEY,
|
||||
name text NOT NULL
|
||||
)
|
||||
18
examples/ondeck/postgresql/schema/0002_venue.sql
Normal file
18
examples/ondeck/postgresql/schema/0002_venue.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
CREATE TYPE status AS ENUM ('op!en', 'clo@sed');
|
||||
COMMENT ON TYPE status IS 'Venues can be either open or closed';
|
||||
|
||||
CREATE TABLE venues (
|
||||
id SERIAL primary key,
|
||||
dropped text,
|
||||
status status not null,
|
||||
statuses status[],
|
||||
slug text not null,
|
||||
name varchar(255) not null,
|
||||
city text not null references city(slug),
|
||||
spotify_playlist varchar not null,
|
||||
songkick_id text,
|
||||
tags text[]
|
||||
);
|
||||
COMMENT ON TABLE venues IS 'Venues are places where muisc happens';
|
||||
COMMENT ON COLUMN venues.slug IS 'This value appears in public URLs';
|
||||
|
||||
3
examples/ondeck/postgresql/schema/0003_add_column.sql
Normal file
3
examples/ondeck/postgresql/schema/0003_add_column.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE venues RENAME TO venue;
|
||||
ALTER TABLE venue ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT NOW();
|
||||
ALTER TABLE venue DROP COLUMN dropped;
|
||||
197
examples/ondeck/postgresql/venue.sql.go
Normal file
197
examples/ondeck/postgresql/venue.sql.go
Normal file
@@ -0,0 +1,197 @@
|
||||
//go:build examples
|
||||
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: venue.sql
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
const createVenue = `-- name: CreateVenue :one
|
||||
INSERT INTO venue (
|
||||
slug,
|
||||
name,
|
||||
city,
|
||||
created_at,
|
||||
spotify_playlist,
|
||||
status,
|
||||
statuses,
|
||||
tags
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
NOW(),
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
) RETURNING id
|
||||
`
|
||||
|
||||
type CreateVenueParams struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
City string `json:"city"`
|
||||
SpotifyPlaylist string `json:"spotify_playlist"`
|
||||
Status Status `json:"status"`
|
||||
Statuses []Status `json:"statuses"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateVenue(ctx context.Context, arg CreateVenueParams) (int32, error) {
|
||||
row := q.queryRow(ctx, q.createVenueStmt, createVenue,
|
||||
arg.Slug,
|
||||
arg.Name,
|
||||
arg.City,
|
||||
arg.SpotifyPlaylist,
|
||||
arg.Status,
|
||||
pq.Array(arg.Statuses),
|
||||
pq.Array(arg.Tags),
|
||||
)
|
||||
var id int32
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const deleteVenue = `-- name: DeleteVenue :exec
|
||||
DELETE FROM venue
|
||||
WHERE slug = $1 AND slug = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteVenue(ctx context.Context, slug string) error {
|
||||
_, err := q.exec(ctx, q.deleteVenueStmt, deleteVenue, slug)
|
||||
return err
|
||||
}
|
||||
|
||||
const getVenue = `-- name: GetVenue :one
|
||||
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
|
||||
FROM venue
|
||||
WHERE slug = $1 AND city = $2
|
||||
`
|
||||
|
||||
type GetVenueParams struct {
|
||||
Slug string `json:"slug"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetVenue(ctx context.Context, arg GetVenueParams) (Venue, error) {
|
||||
row := q.queryRow(ctx, q.getVenueStmt, getVenue, arg.Slug, arg.City)
|
||||
var i Venue
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Status,
|
||||
pq.Array(&i.Statuses),
|
||||
&i.Slug,
|
||||
&i.Name,
|
||||
&i.City,
|
||||
&i.SpotifyPlaylist,
|
||||
&i.SongkickID,
|
||||
pq.Array(&i.Tags),
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listVenues = `-- name: ListVenues :many
|
||||
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
|
||||
FROM venue
|
||||
WHERE city = $1
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListVenues(ctx context.Context, city string) ([]Venue, error) {
|
||||
rows, err := q.query(ctx, q.listVenuesStmt, listVenues, city)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Venue
|
||||
for rows.Next() {
|
||||
var i Venue
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Status,
|
||||
pq.Array(&i.Statuses),
|
||||
&i.Slug,
|
||||
&i.Name,
|
||||
&i.City,
|
||||
&i.SpotifyPlaylist,
|
||||
&i.SongkickID,
|
||||
pq.Array(&i.Tags),
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateVenueName = `-- name: UpdateVenueName :one
|
||||
UPDATE venue
|
||||
SET name = $2
|
||||
WHERE slug = $1
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type UpdateVenueNameParams struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateVenueName(ctx context.Context, arg UpdateVenueNameParams) (int32, error) {
|
||||
row := q.queryRow(ctx, q.updateVenueNameStmt, updateVenueName, arg.Slug, arg.Name)
|
||||
var id int32
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const venueCountByCity = `-- name: VenueCountByCity :many
|
||||
SELECT
|
||||
city,
|
||||
count(*)
|
||||
FROM venue
|
||||
GROUP BY 1
|
||||
ORDER BY 1
|
||||
`
|
||||
|
||||
type VenueCountByCityRow struct {
|
||||
City string `json:"city"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
func (q *Queries) VenueCountByCity(ctx context.Context) ([]VenueCountByCityRow, error) {
|
||||
rows, err := q.query(ctx, q.venueCountByCityStmt, venueCountByCity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []VenueCountByCityRow
|
||||
for rows.Next() {
|
||||
var i VenueCountByCityRow
|
||||
if err := rows.Scan(&i.City, &i.Count); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user