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:
88
examples/ondeck/mysql/city.sql.go
Normal file
88
examples/ondeck/mysql/city.sql.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: city.sql
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createCity = `-- name: CreateCity :exec
|
||||
INSERT INTO city (
|
||||
name,
|
||||
slug
|
||||
) VALUES (
|
||||
?,
|
||||
?
|
||||
)
|
||||
`
|
||||
|
||||
type CreateCityParams struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateCity(ctx context.Context, arg CreateCityParams) error {
|
||||
_, err := q.exec(ctx, q.createCityStmt, createCity, arg.Name, arg.Slug)
|
||||
return err
|
||||
}
|
||||
|
||||
const getCity = `-- name: GetCity :one
|
||||
SELECT slug, name
|
||||
FROM city
|
||||
WHERE slug = ?
|
||||
`
|
||||
|
||||
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 = ?
|
||||
WHERE slug = ?
|
||||
`
|
||||
|
||||
type UpdateCityNameParams struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateCityName(ctx context.Context, arg UpdateCityNameParams) error {
|
||||
_, err := q.exec(ctx, q.updateCityNameStmt, updateCityName, arg.Name, arg.Slug)
|
||||
return err
|
||||
}
|
||||
178
examples/ondeck/mysql/db.go
Normal file
178
examples/ondeck/mysql/db.go
Normal file
@@ -0,0 +1,178 @@
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
172
examples/ondeck/mysql/db_test.go
Normal file
172
examples/ondeck/mysql/db_test.go
Normal file
@@ -0,0 +1,172 @@
|
||||
//go:build examples
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
|
||||
)
|
||||
|
||||
func join(vals ...string) sql.NullString {
|
||||
if len(vals) == 0 {
|
||||
return sql.NullString{}
|
||||
}
|
||||
return sql.NullString{
|
||||
Valid: true,
|
||||
String: strings.Join(vals, ","),
|
||||
}
|
||||
}
|
||||
|
||||
func runOnDeckQueries(t *testing.T, q *Queries) {
|
||||
ctx := context.Background()
|
||||
|
||||
err := q.CreateCity(ctx, CreateCityParams{
|
||||
Slug: "san-francisco",
|
||||
Name: "San Francisco",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
city, err := q.GetCity(ctx, "san-francisco")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
venueResult, err := q.CreateVenue(ctx, CreateVenueParams{
|
||||
Slug: "the-fillmore",
|
||||
Name: "The Fillmore",
|
||||
City: city.Slug,
|
||||
SpotifyPlaylist: "spotify:uri",
|
||||
Status: VenueStatusOpen,
|
||||
Statuses: join(string(VenueStatusOpen), string(VenueStatusClosed)),
|
||||
Tags: join("rock", "punk"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
venueID, err := venueResult.LastInsertId()
|
||||
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, uint64(venueID)); diff != "" {
|
||||
t.Errorf("venue ID mismatch:\n%s", diff)
|
||||
}
|
||||
|
||||
{
|
||||
actual, err := q.VenueCountByCity(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if diff := cmp.Diff(actual, []VenueCountByCityRow{
|
||||
{city.Slug, int64(1)},
|
||||
}); diff != "" {
|
||||
t.Errorf("venue count 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)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
expected := "Fillmore"
|
||||
err := q.UpdateVenueName(ctx, UpdateVenueNameParams{
|
||||
Slug: venue.Slug,
|
||||
Name: expected,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
fresh, err := q.GetVenue(ctx, GetVenueParams{
|
||||
Slug: venue.Slug,
|
||||
City: city.Slug,
|
||||
})
|
||||
if diff := cmp.Diff(expected, fresh.Name); diff != "" {
|
||||
t.Errorf("update venue mismatch:\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
err := q.DeleteVenue(ctx, DeleteVenueParams{
|
||||
Slug: venue.Slug,
|
||||
Slug_2: venue.Slug,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepared(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
uri := local.MySQL(t, []string{"schema"})
|
||||
db, err := sql.Open("mysql", 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.MySQL(t, []string{"schema"})
|
||||
db, err := sql.Open("mysql", uri)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s", uri, err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
runOnDeckQueries(t, New(db))
|
||||
}
|
||||
75
examples/ondeck/mysql/models.go
Normal file
75
examples/ondeck/mysql/models.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type VenueStatus string
|
||||
|
||||
const (
|
||||
VenueStatusOpen VenueStatus = "open"
|
||||
VenueStatusClosed VenueStatus = "closed"
|
||||
)
|
||||
|
||||
func (e *VenueStatus) Scan(src any) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = VenueStatus(s)
|
||||
case string:
|
||||
*e = VenueStatus(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for VenueStatus: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullVenueStatus struct {
|
||||
VenueStatus VenueStatus `json:"venue_status"`
|
||||
Valid bool `json:"valid"` // Valid is true if VenueStatus is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullVenueStatus) Scan(value any) error {
|
||||
if value == nil {
|
||||
ns.VenueStatus, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.VenueStatus.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullVenueStatus) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.VenueStatus), nil
|
||||
}
|
||||
|
||||
type City struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Venues are places where muisc happens
|
||||
type Venue struct {
|
||||
ID uint64 `json:"id"`
|
||||
// Venues can be either open or closed
|
||||
Status VenueStatus `json:"status"`
|
||||
Statuses sql.NullString `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 sql.NullString `json:"tags"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
25
examples/ondeck/mysql/querier.go
Normal file
25
examples/ondeck/mysql/querier.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
CreateCity(ctx context.Context, arg CreateCityParams) error
|
||||
CreateVenue(ctx context.Context, arg CreateVenueParams) (sql.Result, error)
|
||||
DeleteVenue(ctx context.Context, arg DeleteVenueParams) 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) error
|
||||
VenueCountByCity(ctx context.Context) ([]VenueCountByCityRow, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
23
examples/ondeck/mysql/query/city.sql
Normal file
23
examples/ondeck/mysql/query/city.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
/* name: ListCities :many */
|
||||
SELECT *
|
||||
FROM city
|
||||
ORDER BY name;
|
||||
|
||||
/* name: GetCity :one */
|
||||
SELECT *
|
||||
FROM city
|
||||
WHERE slug = ?;
|
||||
|
||||
/* name: CreateCity :exec */
|
||||
INSERT INTO city (
|
||||
name,
|
||||
slug
|
||||
) VALUES (
|
||||
?,
|
||||
?
|
||||
);
|
||||
|
||||
/* name: UpdateCityName :exec */
|
||||
UPDATE city
|
||||
SET name = ?
|
||||
WHERE slug = ?;
|
||||
48
examples/ondeck/mysql/query/venue.sql
Normal file
48
examples/ondeck/mysql/query/venue.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
/* name: ListVenues :many */
|
||||
SELECT *
|
||||
FROM venue
|
||||
WHERE city = ?
|
||||
ORDER BY name;
|
||||
|
||||
/* name: DeleteVenue :exec */
|
||||
DELETE FROM venue
|
||||
WHERE slug = ? AND slug = ?;
|
||||
|
||||
/* name: GetVenue :one */
|
||||
SELECT *
|
||||
FROM venue
|
||||
WHERE slug = ? AND city = ?;
|
||||
|
||||
/* name: CreateVenue :execresult */
|
||||
INSERT INTO venue (
|
||||
slug,
|
||||
name,
|
||||
city,
|
||||
created_at,
|
||||
spotify_playlist,
|
||||
status,
|
||||
statuses,
|
||||
tags
|
||||
) VALUES (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
NOW(),
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
);
|
||||
|
||||
/* name: UpdateVenueName :exec */
|
||||
UPDATE venue
|
||||
SET name = ?
|
||||
WHERE slug = ?;
|
||||
|
||||
/* name: VenueCountByCity :many */
|
||||
SELECT
|
||||
city,
|
||||
count(*)
|
||||
FROM venue
|
||||
GROUP BY 1
|
||||
ORDER BY 1;
|
||||
4
examples/ondeck/mysql/schema/0001_city.sql
Normal file
4
examples/ondeck/mysql/schema/0001_city.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE city (
|
||||
slug varchar(255) PRIMARY KEY,
|
||||
name text NOT NULL
|
||||
);
|
||||
12
examples/ondeck/mysql/schema/0002_venue.sql
Normal file
12
examples/ondeck/mysql/schema/0002_venue.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
CREATE TABLE venues (
|
||||
id SERIAL primary key,
|
||||
dropped text,
|
||||
status ENUM('open', 'closed') not null COMMENT 'Venues can be either open or closed',
|
||||
statuses text, -- status[],
|
||||
slug text not null COMMENT 'This value appears in public URLs',
|
||||
name varchar(255) not null,
|
||||
city varchar(255) not null references city(slug),
|
||||
spotify_playlist varchar(255) not null,
|
||||
songkick_id text,
|
||||
tags text -- text[]
|
||||
) COMMENT='Venues are places where muisc happens';
|
||||
3
examples/ondeck/mysql/schema/0003_add_column.sql
Normal file
3
examples/ondeck/mysql/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;
|
||||
193
examples/ondeck/mysql/venue.sql.go
Normal file
193
examples/ondeck/mysql/venue.sql.go
Normal file
@@ -0,0 +1,193 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: venue.sql
|
||||
|
||||
package ondeck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const createVenue = `-- name: CreateVenue :execresult
|
||||
INSERT INTO venue (
|
||||
slug,
|
||||
name,
|
||||
city,
|
||||
created_at,
|
||||
spotify_playlist,
|
||||
status,
|
||||
statuses,
|
||||
tags
|
||||
) VALUES (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
NOW(),
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)
|
||||
`
|
||||
|
||||
type CreateVenueParams struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
City string `json:"city"`
|
||||
SpotifyPlaylist string `json:"spotify_playlist"`
|
||||
Status VenueStatus `json:"status"`
|
||||
Statuses sql.NullString `json:"statuses"`
|
||||
Tags sql.NullString `json:"tags"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateVenue(ctx context.Context, arg CreateVenueParams) (sql.Result, error) {
|
||||
return q.exec(ctx, q.createVenueStmt, createVenue,
|
||||
arg.Slug,
|
||||
arg.Name,
|
||||
arg.City,
|
||||
arg.SpotifyPlaylist,
|
||||
arg.Status,
|
||||
arg.Statuses,
|
||||
arg.Tags,
|
||||
)
|
||||
}
|
||||
|
||||
const deleteVenue = `-- name: DeleteVenue :exec
|
||||
DELETE FROM venue
|
||||
WHERE slug = ? AND slug = ?
|
||||
`
|
||||
|
||||
type DeleteVenueParams struct {
|
||||
Slug string `json:"slug"`
|
||||
Slug_2 string `json:"slug_2"`
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteVenue(ctx context.Context, arg DeleteVenueParams) error {
|
||||
_, err := q.exec(ctx, q.deleteVenueStmt, deleteVenue, arg.Slug, arg.Slug_2)
|
||||
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 = ? AND city = ?
|
||||
`
|
||||
|
||||
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,
|
||||
&i.Statuses,
|
||||
&i.Slug,
|
||||
&i.Name,
|
||||
&i.City,
|
||||
&i.SpotifyPlaylist,
|
||||
&i.SongkickID,
|
||||
&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 = ?
|
||||
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,
|
||||
&i.Statuses,
|
||||
&i.Slug,
|
||||
&i.Name,
|
||||
&i.City,
|
||||
&i.SpotifyPlaylist,
|
||||
&i.SongkickID,
|
||||
&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 :exec
|
||||
UPDATE venue
|
||||
SET name = ?
|
||||
WHERE slug = ?
|
||||
`
|
||||
|
||||
type UpdateVenueNameParams struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateVenueName(ctx context.Context, arg UpdateVenueNameParams) error {
|
||||
_, err := q.exec(ctx, q.updateVenueNameStmt, updateVenueName, arg.Name, arg.Slug)
|
||||
return 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