Compare · 020

Rows back from INSERT, UPDATE and DELETE: DuckDB and MariaDB

Every file of the DuckDB and MariaDB versions side by side, with the lines that differ marked.

Configuration

sgl.yamldiffers
DuckDB
MariaDB
1version: 1
1version: 1
2
2
3databases:
3databases:
4 - name: orders
4 - name: orders
5 dialect: duckdb
5 dialect: mariadb
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

Migrations

db/migrations/0001_orders.up.sqldiffers
DuckDB
MariaDB
1CREATE SEQUENCE order_ids;
2
3CREATE TABLE orders (
1CREATE TABLE orders (
4 id BIGINT PRIMARY KEY DEFAULT nextval('order_ids'),
2 id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
5 item VARCHAR NOT NULL,
3 item VARCHAR(50) NOT NULL,
6 quantity INTEGER NOT NULL DEFAULT 1,
4 quantity INT NOT NULL DEFAULT 1,
7 status VARCHAR NOT NULL DEFAULT 'waiting'
5 status VARCHAR(20) NOT NULL DEFAULT 'waiting'
8);
6);
db/migrations/0001_orders.down.sqldiffers
DuckDB
MariaDB
1DROP TABLE orders;
1DROP TABLE orders;
2DROP SEQUENCE order_ids;

Queries

PlaceOrderdiffers
DuckDB · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1-- sgl.query(PlaceOrder, one)
1-- sgl.query(PlaceOrder, one)
2INSERT INTO orders (item, quantity) VALUES ($1, $2)
2INSERT INTO orders (item, quantity) VALUES (?, ?)
3RETURNING id, status, quantity * 250 AS price_pence;
3RETURNING id, status, quantity * 250 AS price_pence;
ServeWaitingOrdersdiffers
DuckDB · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1-- sgl.query(ServeWaitingOrders, many)
1-- sgl.query(ServeWaitingOrders, execrows)
2UPDATE orders SET status = 'served' WHERE status = 'waiting'
2UPDATE orders SET status = 'served' WHERE status = 'waiting';
3RETURNING id, item, quantity;
CancelOrderdiffers
DuckDB · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1-- sgl.query(CancelOrder, one)
1-- sgl.query(CancelOrder, one)
2DELETE FROM orders WHERE id = $1 AND status = 'waiting'
2DELETE FROM orders WHERE id = ? AND status = 'waiting'
3RETURNING item, quantity;
3RETURNING item, quantity;
ServedOrdersonly MariaDB
DuckDB · not in this version
MariaDB · db/queries/orders.sql
1-- sgl.query(ServedOrders, many)
2SELECT id, item, quantity FROM orders WHERE status = 'served' ORDER BY id;

What sgl generates

PlaceOrderidentical
DuckDB
MariaDB
1func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) (PlaceOrderRow, error)
1func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) (PlaceOrderRow, error)
2
2
3type PlaceOrderParams struct {
3type PlaceOrderParams struct {
4 Item string
4 Item string
5 Quantity int32
5 Quantity int32
6}
6}
7
7
8type PlaceOrderRow struct {
8type PlaceOrderRow struct {
9 ID int64 `json:"id"`
9 ID int64 `json:"id"`
10 Status string `json:"status"`
10 Status string `json:"status"`
11 PricePence int32 `json:"price_pence"`
11 PricePence int32 `json:"price_pence"`
12}
12}
ServeWaitingOrdersdiffers
DuckDB
MariaDB
1func (queries *Queries) ServeWaitingOrders(ctx context.Context) ([]ServeWaitingOrdersRow, error)
1func (queries *Queries) ServeWaitingOrders(ctx context.Context) (int64, error)
2
3type ServeWaitingOrdersRow struct {
4 ID int64 `json:"id"`
5 Item string `json:"item"`
6 Quantity int32 `json:"quantity"`
7}
CancelOrderidentical
DuckDB
MariaDB
1func (queries *Queries) CancelOrder(ctx context.Context, id int64) (CancelOrderRow, error)
1func (queries *Queries) CancelOrder(ctx context.Context, id int64) (CancelOrderRow, error)
2
2
3type CancelOrderRow struct {
3type CancelOrderRow struct {
4 Item string `json:"item"`
4 Item string `json:"item"`
5 Quantity int32 `json:"quantity"`
5 Quantity int32 `json:"quantity"`
6}
6}
ServedOrdersonly MariaDB
DuckDB · not in this version
MariaDB
1func (queries *Queries) ServedOrders(ctx context.Context) ([]ServedOrdersRow, error)
2
3type ServedOrdersRow struct {
4 ID int64 `json:"id"`
5 Item string `json:"item"`
6 Quantity int32 `json:"quantity"`
7}

The program

example.godiffers
DuckDB
MariaDB
1package main
1package main
2
2
3import (
3import (
4 "cmp"
5 "context"
4 "context"
6 "database/sql"
5 "database/sql"
7 "errors"
6 "errors"
8 "fmt"
7 "fmt"
9 "io"
8 "io"
10 "slices"
11
9
12 "seagull.sh/seagull/examples/duckdb/020_returning/internal/store"
10 "seagull.sh/seagull/examples/mariadb/020_returning/internal/store"
13)
11)
14
12
15// run places three orders, cancels one, serves the rest and then tries to cancel an order
13// run places three orders, cancels one, serves the rest and then tries to cancel an order
16// that has already been served. Every write uses RETURNING, so the rows it touched come
14// that has already been served. The INSERT and the DELETE use RETURNING, so the rows they
17// back from the same statement.
15// touched come back from the same statement.
18//
16//
19// Takes database (*sql.DB) which is a migrated database.
17// Takes database (*sql.DB) which is a migrated database.
20// Takes output (io.Writer) which receives what each write returned.
18// Takes output (io.Writer) which receives what each write returned.
21//
19//
22// Returns error when a query fails.
20// Returns error when a query fails.
23func run(ctx context.Context, database *sql.DB, output io.Writer) error {
21func run(ctx context.Context, database *sql.DB, output io.Writer) error {
24 queries := store.New(database)
22 queries := store.New(database)
25
23
26 // PlaceOrder is an INSERT declared as one: the id the sequence assigned, the default
24 // PlaceOrder is an INSERT declared as one: the id MariaDB assigned, the default status
27 // status and a computed price all come back in a PlaceOrderRow.
25 // and a computed price all come back in a PlaceOrderRow.
28 orders := []store.PlaceOrderParams{
26 orders := []store.PlaceOrderParams{
29 {Item: "chips", Quantity: 2},
27 {Item: "chips", Quantity: 2},
30 {Item: "battered cod", Quantity: 1},
28 {Item: "battered cod", Quantity: 1},
31 {Item: "mushy peas", Quantity: 1},
29 {Item: "mushy peas", Quantity: 1},
32 }
30 }
33 for _, order := range orders {
31 for _, order := range orders {
34 placed, err := queries.PlaceOrder(ctx, order)
32 placed, err := queries.PlaceOrder(ctx, order)
35 if err != nil {
33 if err != nil {
36 return fmt.Errorf("placing an order for %s: %w", order.Item, err)
34 return fmt.Errorf("placing an order for %s: %w", order.Item, err)
37 }
35 }
38 fmt.Fprintf(output, "placed #%d %-12s %s, %dp\n", placed.ID, order.Item, placed.Status, placed.PricePence)
36 fmt.Fprintf(output, "placed #%d %-12s %s, %dp\n", placed.ID, order.Item, placed.Status, placed.PricePence)
39 }
37 }
40
38
41 // CancelOrder is a DELETE declared as one, so it returns the row it removed.
39 // CancelOrder is a DELETE declared as one, so it returns the row it removed.
42 cancelled, err := queries.CancelOrder(ctx, 2)
40 cancelled, err := queries.CancelOrder(ctx, 2)
43 if err != nil {
41 if err != nil {
44 return fmt.Errorf("cancelling order 2: %w", err)
42 return fmt.Errorf("cancelling order 2: %w", err)
45 }
43 }
46 fmt.Fprintf(output, "cancelled #2 %-12s x%d\n", cancelled.Item, cancelled.Quantity)
44 fmt.Fprintf(output, "cancelled #2 %-12s x%d\n", cancelled.Item, cancelled.Quantity)
47
45
48 // ServeWaitingOrders is an UPDATE declared as many: one row for every order it changed.
46 // MariaDB has RETURNING on INSERT, REPLACE and DELETE, but not on UPDATE, so
49 // DuckDB does not promise an order for RETURNING rows, so sort them.
47 // ServeWaitingOrders is execrows and ServedOrders reads the changed rows back.
50 served, err := queries.ServeWaitingOrders(ctx)
48 if _, err := queries.ServeWaitingOrders(ctx); err != nil {
51 if err != nil {
52 return fmt.Errorf("serving orders: %w", err)
49 return fmt.Errorf("serving orders: %w", err)
53 }
50 }
54 slices.SortFunc(served, func(a, b store.ServeWaitingOrdersRow) int { return cmp.Compare(a.ID, b.ID) })
51 served, err := queries.ServedOrders(ctx)
52 if err != nil {
53 return fmt.Errorf("listing served orders: %w", err)
54 }
55 for _, order := range served {
55 for _, order := range served {
56 fmt.Fprintf(output, "served #%d %-12s x%d\n", order.ID, order.Item, order.Quantity)
56 fmt.Fprintf(output, "served #%d %-12s x%d\n", order.ID, order.Item, order.Quantity)
57 }
57 }
58
58
59 // A one query that matches no row returns sql.ErrNoRows, writes included: order 1 has
59 // A one query that matches no row returns sql.ErrNoRows, writes included: order 1 has
60 // been served, so the DELETE's WHERE clause no longer matches it.
60 // been served, so the DELETE's WHERE clause no longer matches it.
61 _, err = queries.CancelOrder(ctx, 1)
61 _, err = queries.CancelOrder(ctx, 1)
62 if errors.Is(err, sql.ErrNoRows) {
62 if errors.Is(err, sql.ErrNoRows) {
63 fmt.Fprintln(output, "cannot cancel #1: it is no longer waiting")
63 fmt.Fprintln(output, "cannot cancel #1: it is no longer waiting")
64 return nil
64 return nil
65 }
65 }
66 if err != nil {
66 if err != nil {
67 return fmt.Errorf("cancelling order 1: %w", err)
67 return fmt.Errorf("cancelling order 1: %w", err)
68 }
68 }
69 return errors.New("cancelled an order that had already been served")
69 return errors.New("cancelled an order that had already been served")
70}
70}