Compare · 007

Enums as generated Go types, and your own types with go_type: PostgreSQL and CockroachDB

PostgreSQL and CockroachDB run the same files for this example: the SQL and the Go are identical.

Configuration

sgl.yamlidentical
PostgreSQL
CockroachDB
1version: 1
1version: 1
2
2
3databases:
3databases:
4 - name: harbour
4 - name: harbour
5 dialect: postgres
5 dialect: postgres
6 migrations: db/migrations
6 migrations: db/migrations
7 queries: db/queries
7 queries: db/queries
8 go:
8 go:
9 package: store
9 package: store
10 out: internal/store
10 out: internal/store
11 driver: database/sql
11 driver: database/sql

Migrations

db/migrations/0001_boats.up.sqlidentical
PostgreSQL
CockroachDB
1CREATE TYPE boat_status AS ENUM ('moored', 'at_sea', 'dry_dock');
1CREATE TYPE boat_status AS ENUM ('moored', 'at_sea', 'dry_dock');
2
2
3CREATE TABLE boats (
3CREATE TABLE boats (
4 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
4 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
5 name TEXT NOT NULL,
5 name TEXT NOT NULL,
6 status boat_status NOT NULL DEFAULT 'moored'
6 status boat_status NOT NULL DEFAULT 'moored'
7);
7);
db/migrations/0001_boats.down.sqlidentical
PostgreSQL
CockroachDB
1DROP TABLE boats;
1DROP TABLE boats;
2DROP TYPE boat_status;
2DROP TYPE boat_status;

Queries

AddBoatidentical
PostgreSQL · db/queries/boats.sql
CockroachDB · db/queries/boats.sql
1-- sgl.query(AddBoat, exec)
1-- sgl.query(AddBoat, exec)
2INSERT INTO boats (name, status) VALUES ($1, $2);
2INSERT INTO boats (name, status) VALUES ($1, $2);
SetStatusidentical
PostgreSQL · db/queries/boats.sql
CockroachDB · db/queries/boats.sql
1-- sgl.query(SetStatus, execrows)
1-- sgl.query(SetStatus, execrows)
2UPDATE boats SET status = $1 WHERE name = $2;
2UPDATE boats SET status = $1 WHERE name = $2;
ListBoatsidentical
PostgreSQL · db/queries/boats.sql
CockroachDB · db/queries/boats.sql
1-- sgl.query(ListBoats, many)
1-- sgl.query(ListBoats, many)
2SELECT name, status FROM boats ORDER BY id;
2SELECT name, status FROM boats ORDER BY id;
BoatsWithStatusidentical
PostgreSQL · db/queries/boats.sql
CockroachDB · db/queries/boats.sql
1-- sgl.query(BoatsWithStatus, many)
1-- sgl.query(BoatsWithStatus, many)
2-- sgl.column(status, go_type: "seagull.sh/seagull/examples/postgres/007_enums/internal/harbour.Status")
2-- sgl.column(status, go_type: "seagull.sh/seagull/examples/postgres/007_enums/internal/harbour.Status")
3SELECT name, status FROM boats WHERE status = $1 ORDER BY id;
3SELECT name, status FROM boats WHERE status = $1 ORDER BY id;

What sgl generates

AddBoatidentical
PostgreSQL
CockroachDB
1func (queries *Queries) AddBoat(ctx context.Context, params AddBoatParams) error
1func (queries *Queries) AddBoat(ctx context.Context, params AddBoatParams) error
2
2
3type AddBoatParams struct {
3type AddBoatParams struct {
4 Name string
4 Name string
5 Status BoatStatus
5 Status BoatStatus
6}
6}
7
7
8type BoatStatus string
8type BoatStatus string
SetStatusidentical
PostgreSQL
CockroachDB
1func (queries *Queries) SetStatus(ctx context.Context, params SetStatusParams) (int64, error)
1func (queries *Queries) SetStatus(ctx context.Context, params SetStatusParams) (int64, error)
2
2
3type SetStatusParams struct {
3type SetStatusParams struct {
4 Status BoatStatus
4 Status BoatStatus
5 Name string
5 Name string
6}
6}
7
7
8type BoatStatus string
8type BoatStatus string
ListBoatsidentical
PostgreSQL
CockroachDB
1func (queries *Queries) ListBoats(ctx context.Context) ([]ListBoatsRow, error)
1func (queries *Queries) ListBoats(ctx context.Context) ([]ListBoatsRow, error)
2
2
3type ListBoatsRow struct {
3type ListBoatsRow struct {
4 Name string `json:"name"`
4 Name string `json:"name"`
5 Status BoatStatus `json:"status"`
5 Status BoatStatus `json:"status"`
6}
6}
7
7
8type BoatStatus string
8type BoatStatus string
BoatsWithStatusidentical
PostgreSQL
CockroachDB
1func (queries *Queries) BoatsWithStatus(ctx context.Context, status BoatStatus) ([]BoatsWithStatusRow, error)
1func (queries *Queries) BoatsWithStatus(ctx context.Context, status BoatStatus) ([]BoatsWithStatusRow, error)
2
2
3type BoatStatus string
3type BoatStatus string
4
4
5type BoatsWithStatusRow struct {
5type BoatsWithStatusRow struct {
6 Name string `json:"name"`
6 Name string `json:"name"`
7 Status harbour.Status `json:"status"`
7 Status harbour.Status `json:"status"`
8}
8}

The program

example.goidentical
PostgreSQL
CockroachDB
1package main
1package main
2
2
3import (
3import (
4 "context"
4 "context"
5 "database/sql"
5 "database/sql"
6 "errors"
6 "errors"
7 "fmt"
7 "fmt"
8 "io"
8 "io"
9
9
10 "seagull.sh/seagull/examples/postgres/007_enums/internal/store"
10 "seagull.sh/seagull/examples/postgres/007_enums/internal/store"
11)
11)
12
12
13// run gives the harbour's boats a status from the boat_status enum and lists them twice.
13// run gives the harbour's boats a status from the boat_status enum and lists them twice.
14// The enum becomes a named Go type in the generated package: models.go declares
14// The enum becomes a named Go type in the generated package: models.go declares
15// store.BoatStatus, named after the enum type, with a constant for each label, such as
15// store.BoatStatus, named after the enum type, with a constant for each label, such as
16// store.BoatStatusMoored, and a Valid method. Every column and parameter of the enum
16// store.BoatStatusMoored, and a Valid method. Every column and parameter of the enum
17// takes it, in models.go, in Params structs and in Row structs, as ListBoats shows. Valid
17// takes it, in models.go, in Params structs and in Row structs, as ListBoats shows. Valid
18// reports whether a value is one of the labels, and the server still enforces them, so a
18// reports whether a value is one of the labels, and the server still enforces them, so a
19// status outside the enum is refused when the statement runs.
19// status outside the enum is refused when the statement runs.
20//
20//
21// For a type of your own, here harbour.Status with a method that describes each label,
21// For a type of your own, here harbour.Status with a method that describes each label,
22// name it with a go_type override. BoatsWithStatus does that for one query with
22// name it with a go_type override. BoatsWithStatus does that for one query with
23// sgl.column(status, go_type: ...); a types.overrides entry in sgl.yaml, naming the enum
23// sgl.column(status, go_type: ...); a types.overrides entry in sgl.yaml, naming the enum
24// type boat_status or the column boats.status, would do it for every query, and for the
24// type boat_status or the column boats.status, would do it for every query, and for the
25// parameters bound to the column too. A query's sgl.column override changes only that
25// parameters bound to the column too. A query's sgl.column override changes only that
26// query's returned column, so AddBoat, SetStatus and BoatsWithStatus's own parameter
26// query's returned column, so AddBoat, SetStatus and BoatsWithStatus's own parameter
27// still take the generated store.BoatStatus.
27// still take the generated store.BoatStatus.
28//
28//
29// Takes database (*sql.DB) which is a migrated database.
29// Takes database (*sql.DB) which is a migrated database.
30// Takes output (io.Writer) which receives both listings.
30// Takes output (io.Writer) which receives both listings.
31//
31//
32// Returns error when a query fails or a status outside the enum is accepted.
32// Returns error when a query fails or a status outside the enum is accepted.
33func run(ctx context.Context, database *sql.DB, output io.Writer) error {
33func run(ctx context.Context, database *sql.DB, output io.Writer) error {
34 queries := store.New(database)
34 queries := store.New(database)
35
35
36 boats := []store.AddBoatParams{
36 boats := []store.AddBoatParams{
37 {Name: "Kittiwake", Status: store.BoatStatusMoored},
37 {Name: "Kittiwake", Status: store.BoatStatusMoored},
38 {Name: "Salty Chip", Status: store.BoatStatusAtSea},
38 {Name: "Salty Chip", Status: store.BoatStatusAtSea},
39 {Name: "Gull Wing", Status: store.BoatStatusDryDock},
39 {Name: "Gull Wing", Status: store.BoatStatusDryDock},
40 {Name: "Driftwood", Status: store.BoatStatusMoored},
40 {Name: "Driftwood", Status: store.BoatStatusMoored},
41 }
41 }
42 for _, boat := range boats {
42 for _, boat := range boats {
43 if err := queries.AddBoat(ctx, boat); err != nil {
43 if err := queries.AddBoat(ctx, boat); err != nil {
44 return fmt.Errorf("adding %s: %w", boat.Name, err)
44 return fmt.Errorf("adding %s: %w", boat.Name, err)
45 }
45 }
46 }
46 }
47
47
48 sunk := store.BoatStatus("sunk")
48 sunk := store.BoatStatus("sunk")
49 if err := queries.AddBoat(ctx, store.AddBoatParams{Name: "Wreck", Status: sunk}); err == nil {
49 if err := queries.AddBoat(ctx, store.AddBoatParams{Name: "Wreck", Status: sunk}); err == nil {
50 return errors.New("adding a boat with status sunk: got no error, want the enum to refuse it")
50 return errors.New("adding a boat with status sunk: got no error, want the enum to refuse it")
51 }
51 }
52 fmt.Fprintf(output, "status %q: Valid() is %t, and the server refuses it, as it is not a boat_status label\n", sunk, sunk.Valid())
52 fmt.Fprintf(output, "status %q: Valid() is %t, and the server refuses it, as it is not a boat_status label\n", sunk, sunk.Valid())
53
53
54 if _, err := queries.SetStatus(ctx, store.SetStatusParams{Status: store.BoatStatusAtSea, Name: "Kittiwake"}); err != nil {
54 if _, err := queries.SetStatus(ctx, store.SetStatusParams{Status: store.BoatStatusAtSea, Name: "Kittiwake"}); err != nil {
55 return fmt.Errorf("sending Kittiwake to sea: %w", err)
55 return fmt.Errorf("sending Kittiwake to sea: %w", err)
56 }
56 }
57
57
58 listed, err := queries.ListBoats(ctx)
58 listed, err := queries.ListBoats(ctx)
59 if err != nil {
59 if err != nil {
60 return fmt.Errorf("listing boats: %w", err)
60 return fmt.Errorf("listing boats: %w", err)
61 }
61 }
62 fmt.Fprintln(output, "\nListBoats, where status is the generated store.BoatStatus:")
62 fmt.Fprintln(output, "\nListBoats, where status is the generated store.BoatStatus:")
63 for _, boat := range listed {
63 for _, boat := range listed {
64 fmt.Fprintf(output, " %-10s %-8s %T\n", boat.Name, boat.Status, boat.Status)
64 fmt.Fprintf(output, " %-10s %-8s %T\n", boat.Name, boat.Status, boat.Status)
65 }
65 }
66
66
67 atSea, err := queries.BoatsWithStatus(ctx, store.BoatStatusAtSea)
67 atSea, err := queries.BoatsWithStatus(ctx, store.BoatStatusAtSea)
68 if err != nil {
68 if err != nil {
69 return fmt.Errorf("listing boats at sea: %w", err)
69 return fmt.Errorf("listing boats at sea: %w", err)
70 }
70 }
71 fmt.Fprintln(output, "\nBoatsWithStatus, where a go_type override makes status a harbour.Status:")
71 fmt.Fprintln(output, "\nBoatsWithStatus, where a go_type override makes status a harbour.Status:")
72 for _, boat := range atSea {
72 for _, boat := range atSea {
73 fmt.Fprintf(output, " %-10s %-14s %T\n", boat.Name, boat.Status.Describe(), boat.Status)
73 fmt.Fprintf(output, " %-10s %-14s %T\n", boat.Name, boat.Status.Describe(), boat.Status)
74 }
74 }
75 return nil
75 return nil
76}
76}