Compare · 007

Enums as generated Go types, and your own types with go_type: DuckDB and MariaDB

Every file of the DuckDB and MariaDB versions side by side, with the lines that differ marked. 3 parameter or column types differ in Go.

Configuration

sgl.yamldiffers
DuckDB
MariaDB
1version: 1
1version: 1
2
2
3databases:
3databases:
4 - name: menu
4 - name: menu
5 dialect: duckdb
5 dialect: mysql
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 types:
11 types:
12 overrides:
12 overrides:
13 - sql: item_status
13 - column: menu_items.status
14 go: seagull.sh/seagull/examples/duckdb/007_enums/internal/menu.Status
14 go: seagull.sh/seagull/examples/mysql/007_enums/internal/menu.Status

Migrations

db/migrations/0001_menu.up.sqldiffers
DuckDB
MariaDB
1CREATE TYPE section AS ENUM ('fish', 'sides', 'puddings');
2
3CREATE TYPE item_status AS ENUM ('available', 'sold_out');
4
5CREATE SEQUENCE menu_item_ids;
6
7CREATE TABLE menu_items (
1CREATE TABLE menu_items (
8 id BIGINT PRIMARY KEY DEFAULT nextval('menu_item_ids'),
2 id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
9 name VARCHAR NOT NULL UNIQUE,
3 name VARCHAR(50) NOT NULL UNIQUE,
10 section section NOT NULL,
4 section ENUM('fish', 'sides', 'puddings') NOT NULL,
11 status item_status NOT NULL DEFAULT 'available'
5 status ENUM('available', 'sold_out') NOT NULL DEFAULT 'available'
12);
6);
db/migrations/0001_menu.down.sqldiffers
DuckDB
MariaDB
1DROP TABLE menu_items;
1DROP TABLE menu_items;
2DROP SEQUENCE menu_item_ids;
3DROP TYPE item_status;
4DROP TYPE section;

Queries

AddItemdiffers
DuckDB · db/queries/menu.sql
MariaDB · db/queries/menu.sql
1-- sgl.query(AddItem, exec)
1-- sgl.query(AddItem, exec)
2INSERT INTO menu_items (name, section) VALUES ($1, $2);
2INSERT INTO menu_items (name, section) VALUES (?, ?);
SetStatusdiffers
DuckDB · db/queries/menu.sql
MariaDB · db/queries/menu.sql
1-- sgl.query(SetStatus, execrows)
1-- sgl.query(SetStatus, execrows)
2UPDATE menu_items SET status = $1 WHERE name = $2;
2UPDATE menu_items SET status = ? WHERE name = ?;
ListMenuidentical
DuckDB · db/queries/menu.sql
MariaDB · db/queries/menu.sql
1-- sgl.query(ListMenu, many)
1-- sgl.query(ListMenu, many)
2SELECT name, section, status FROM menu_items ORDER BY section, name;
2SELECT name, section, status FROM menu_items ORDER BY section, name;
ItemsInSectiondiffers
DuckDB · db/queries/menu.sql
MariaDB · db/queries/menu.sql
1-- sgl.query(ItemsInSection, many)
1-- sgl.query(ItemsInSection, many)
2-- sgl.column(section, go_type: "seagull.sh/seagull/examples/duckdb/007_enums/internal/menu.Section")
2-- sgl.column(section, go_type: "seagull.sh/seagull/examples/mysql/007_enums/internal/menu.Section")
3SELECT name, section, status FROM menu_items WHERE section = $1 ORDER BY name;
3SELECT name, section, status FROM menu_items WHERE section = ? ORDER BY name;

What sgl generates

AddItemdiffers
DuckDB
MariaDB
1func (queries *Queries) AddItem(ctx context.Context, params AddItemParams) error
1func (queries *Queries) AddItem(ctx context.Context, params AddItemParams) error
2
2
3type AddItemParams struct {
3type AddItemParams struct {
4 Name string
4 Name string
5 Section Section
5 Section MenuItemsSection
6}
6}
7
7
8type Section string
8type MenuItemsSection string
SetStatusidentical
DuckDB
MariaDB
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 menu.Status
4 Status menu.Status
5 Name string
5 Name string
6}
6}
ListMenudiffers
DuckDB
MariaDB
1func (queries *Queries) ListMenu(ctx context.Context) ([]ListMenuRow, error)
1func (queries *Queries) ListMenu(ctx context.Context) ([]ListMenuRow, error)
2
2
3type ListMenuRow struct {
3type ListMenuRow struct {
4 Name string `json:"name"`
4 Name string `json:"name"`
5 Section Section `json:"section"`
5 Section MenuItemsSection `json:"section"`
6 Status menu.Status `json:"status"`
6 Status menu.Status `json:"status"`
7}
7}
8
8
9type Section string
9type MenuItemsSection string
ItemsInSectiondiffers
DuckDB
MariaDB
1func (queries *Queries) ItemsInSection(ctx context.Context, section Section) ([]ItemsInSectionRow, error)
1func (queries *Queries) ItemsInSection(ctx context.Context, section MenuItemsSection) ([]ItemsInSectionRow, error)
2
2
3type Section string
3type MenuItemsSection string
4
4
5type ItemsInSectionRow struct {
5type ItemsInSectionRow struct {
6 Name string `json:"name"`
6 Name string `json:"name"`
7 Section menu.Section `json:"section"`
7 Section menu.Section `json:"section"`
8 Status menu.Status `json:"status"`
8 Status menu.Status `json:"status"`
9}
9}

The program

example.godiffers
DuckDB
MariaDB
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/duckdb/007_enums/internal/menu"
10 "seagull.sh/seagull/examples/mysql/007_enums/internal/menu"
11 "seagull.sh/seagull/examples/duckdb/007_enums/internal/store"
11 "seagull.sh/seagull/examples/mysql/007_enums/internal/store"
12)
12)
13
13
14// run fills a chip shop menu whose section and status columns are DuckDB enums, marks an
14// run fills a chip shop menu whose section and status columns are MySQL ENUMs, marks an
15// item sold out and reads the menu back. Each enum becomes a named Go type in the
15// item sold out and reads the menu back. Each ENUM becomes a named Go type in the
16// generated package unless an override names a type of your own:
16// generated package unless an override names a type of your own:
17//
17//
18// - section has no override, so models.go declares store.Section, with a constant for
18// - section has no override, so models.go declares store.MenuItemsSection, named after
19// each value, such as store.SectionFish, and a Valid method; ListMenuRow.Section,
19// the table and column that hold the ENUM, with a constant for each value, such as
20// AddItemParams.Section and ItemsInSection's parameter are all a store.Section;
20// store.MenuItemsSectionFish, and a Valid method; ListMenuRow.Section,
21// - sgl.yaml's types.overrides names the enum type item_status, so every column of that
21// AddItemParams.Section and ItemsInSection's parameter are all a
22// enum gives menu.Status, as in ListMenuRow.Status, and so does every parameter
22// store.MenuItemsSection;
23// compared with or assigned to one, as in SetStatusParams.Status; the generated
23// - sgl.yaml's types.overrides names the column menu_items.status, so every query that
24// package then declares no type for item_status;
24// returns it as it is gives menu.Status, as in ListMenuRow.Status, and so does every
25// parameter compared with or assigned to it, as in SetStatusParams.Status; models.go
26// uses it too, and the generated package declares no type for that ENUM;
25// - sgl.column(section, go_type: ...) in ItemsInSection gives that one query's returned
27// - sgl.column(section, go_type: ...) in ItemsInSection gives that one query's returned
26// Section the type menu.Section; the parameter it compares with stays a
28// Section the type menu.Section; the parameter it compares with stays a
27// store.Section.
29// store.MenuItemsSection.
28//
30//
29// An enum sorts in the order its values are declared, not alphabetically, so ORDER BY
31// An ENUM sorts in the order its values are declared, not alphabetically, so ORDER BY
30// section lists fish, then sides, then puddings. Valid reports whether a value is one the
32// section lists fish, then sides, then puddings. Valid reports whether a value is one the
31// enum declares, so the shop can check a section before it binds it. DuckDB checks each
33// ENUM declares, so the shop can check a section before it binds it. In strict mode, the
32// value against the enum as it is stored as well, so a section the enum does not list is
34// default since MySQL 5.7, a value the ENUM does not list is refused as it is stored as
33// refused; DuckDB keeps an enum as a small integer, and its error names that type.
35// well; MySQL keeps an ENUM as the index of its value, and its error says the value was
36// truncated.
34//
37//
35// Takes database (*sql.DB) which is a migrated database.
38// Takes database (*sql.DB) which is a migrated database.
36// Takes output (io.Writer) which receives the menu, the fish section and the refusal.
39// Takes output (io.Writer) which receives the menu, the fish section and the refusal.
37//
40//
38// Returns error when a query fails other than as the example expects.
41// Returns error when a query fails other than as the example expects.
39func run(ctx context.Context, database *sql.DB, output io.Writer) error {
42func run(ctx context.Context, database *sql.DB, output io.Writer) error {
40 queries := store.New(database)
43 queries := store.New(database)
41
44
42 items := []store.AddItemParams{
45 items := []store.AddItemParams{
43 {Name: "cod", Section: store.SectionFish},
46 {Name: "cod", Section: store.MenuItemsSectionFish},
44 {Name: "haddock", Section: store.SectionFish},
47 {Name: "haddock", Section: store.MenuItemsSectionFish},
45 {Name: "chips", Section: store.SectionSides},
48 {Name: "chips", Section: store.MenuItemsSectionSides},
46 {Name: "mushy peas", Section: store.SectionSides},
49 {Name: "mushy peas", Section: store.MenuItemsSectionSides},
47 {Name: "sticky toffee pudding", Section: store.SectionPuddings},
50 {Name: "sticky toffee pudding", Section: store.MenuItemsSectionPuddings},
48 }
51 }
49 for _, item := range items {
52 for _, item := range items {
50 if err := queries.AddItem(ctx, item); err != nil {
53 if err := queries.AddItem(ctx, item); err != nil {
51 return fmt.Errorf("adding %s: %w", item.Name, err)
54 return fmt.Errorf("adding %s: %w", item.Name, err)
52 }
55 }
53 }
56 }
54 if _, err := queries.SetStatus(ctx, store.SetStatusParams{Status: menu.SoldOut, Name: "haddock"}); err != nil {
57 if _, err := queries.SetStatus(ctx, store.SetStatusParams{Status: menu.SoldOut, Name: "haddock"}); err != nil {
55 return fmt.Errorf("selling out of haddock: %w", err)
58 return fmt.Errorf("selling out of haddock: %w", err)
56 }
59 }
57
60
58 listed, err := queries.ListMenu(ctx)
61 listed, err := queries.ListMenu(ctx)
59 if err != nil {
62 if err != nil {
60 return fmt.Errorf("listing the menu: %w", err)
63 return fmt.Errorf("listing the menu: %w", err)
61 }
64 }
62 var menuRow store.ListMenuRow
65 var menuRow store.ListMenuRow
63 fmt.Fprintf(output, "ListMenu: section %T, status %T\n", menuRow.Section, menuRow.Status)
66 fmt.Fprintf(output, "ListMenu: section %T, status %T\n", menuRow.Section, menuRow.Status)
64 for _, item := range listed {
67 for _, item := range listed {
65 fmt.Fprintf(output, " %-9s %s%s\n", item.Section, item.Name, soldOut(item.Status))
68 fmt.Fprintf(output, " %-9s %s%s\n", item.Section, item.Name, soldOut(item.Status))
66 }
69 }
67
70
68 fish, err := queries.ItemsInSection(ctx, store.SectionFish)
71 fish, err := queries.ItemsInSection(ctx, store.MenuItemsSectionFish)
69 if err != nil {
72 if err != nil {
70 return fmt.Errorf("listing the fish: %w", err)
73 return fmt.Errorf("listing the fish: %w", err)
71 }
74 }
72 var sectionRow store.ItemsInSectionRow
75 var sectionRow store.ItemsInSectionRow
73 fmt.Fprintf(output, "ItemsInSection: section %T, status %T\n", sectionRow.Section, sectionRow.Status)
76 fmt.Fprintf(output, "ItemsInSection: section %T, status %T\n", sectionRow.Section, sectionRow.Status)
74 for _, item := range fish {
77 for _, item := range fish {
75 fmt.Fprintf(output, " %-9s %s%s\n", item.Section, item.Name, soldOut(item.Status))
78 fmt.Fprintf(output, " %-9s %s%s\n", item.Section, item.Name, soldOut(item.Status))
76 }
79 }
77
80
78 drinks := store.Section("drinks")
81 drinks := store.MenuItemsSection("drinks")
79 fmt.Fprintf(output, "section drinks is valid: %t\n", drinks.Valid())
82 fmt.Fprintf(output, "section drinks is valid: %t\n", drinks.Valid())
80 err = queries.AddItem(ctx, store.AddItemParams{Name: "lemonade", Section: drinks})
83 err = queries.AddItem(ctx, store.AddItemParams{Name: "lemonade", Section: drinks})
81 if err == nil {
84 if err == nil {
82 return errors.New("added an item to a section the enum does not list")
85 return errors.New("added an item to a section the enum does not list")
83 }
86 }
84 fmt.Fprintf(output, "refused section drinks: %v\n", err)
87 fmt.Fprintf(output, "refused section drinks: %v\n", err)
85 return nil
88 return nil
86}
89}
87
90
88// soldOut returns a note for an item that has sold out, comparing against the typed
91// soldOut returns a note for an item that has sold out, comparing against the typed
89// constant rather than a string.
92// constant rather than a string.
90//
93//
91// Takes status (menu.Status) which is the item's status.
94// Takes status (menu.Status) which is the item's status.
92//
95//
93// Returns string which is the note, or empty when the item is available.
96// Returns string which is the note, or empty when the item is available.
94func soldOut(status menu.Status) string {
97func soldOut(status menu.Status) string {
95 if status == menu.SoldOut {
98 if status == menu.SoldOut {
96 return " (sold out)"
99 return " (sold out)"
97 }
100 }
98 return ""
101 return ""
99}
102}

Go type differences

The same SQL types map to different Go types, because each database reports them differently.

QueryFieldDuckDBMariaDB
AddItemparameter sectionSectionMenuItemsSection
ListMenucolumn sectionSectionMenuItemsSection
ItemsInSectionparameter sectionSectionMenuItemsSection