JSON columns: MySQL and MariaDB
Every file of the MySQL and MariaDB versions side by side, with the lines that differ marked.
Configuration
sgl.yamldiffers
MySQL
MariaDB
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: orders4
- name: orders5
dialect: mysql5
dialect: mariadb6
migrations: db/migrations6
migrations: db/migrations7
queries: db/queries7
queries: db/queries8
go:8
go:9
package: store9
package: store10
out: internal/store10
out: internal/storeMigrations
db/migrations/0001_orders.up.sqlidentical
MySQL
MariaDB
1
CREATE TABLE orders (1
CREATE TABLE orders (2
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,2
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,3
customer VARCHAR(50) NOT NULL,3
customer VARCHAR(50) NOT NULL,4
basket JSON NOT NULL,4
basket JSON NOT NULL,5
delivery JSON5
delivery JSON6
);6
);db/migrations/0001_orders.down.sqlidentical
MySQL
MariaDB
1
DROP TABLE orders;1
DROP TABLE orders;Queries
PlaceOrderidentical
MySQL · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1
-- sgl.query(PlaceOrder, exec)1
-- sgl.query(PlaceOrder, exec)2
INSERT INTO orders (customer, basket, delivery) VALUES (?, ?, ?);2
INSERT INTO orders (customer, basket, delivery) VALUES (?, ?, ?);GetOrderidentical
MySQL · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1
-- sgl.query(GetOrder, one)1
-- sgl.query(GetOrder, one)2
SELECT id, customer, basket, delivery FROM orders WHERE id = ?;2
SELECT id, customer, basket, delivery FROM orders WHERE id = ?;OrdersWithFishdiffers
MySQL · db/queries/orders.sql
MariaDB · db/queries/orders.sql
1
-- sgl.query(OrdersWithFish, many)1
-- sgl.query(OrdersWithFish, many)2
-- ?1 as sgl.param(fish, type: text)2
-- ?1 as sgl.param(fish)3
SELECT3
SELECT4
id,4
id,5
customer,5
customer,6
basket -> '$.extras' AS extras,6
JSON_EXTRACT(basket, '$.extras') AS extras,7
delivery ->> '$.address' AS address7
JSON_UNQUOTE(JSON_EXTRACT(delivery, '$.address')) AS address8
FROM orders8
FROM orders9
WHERE basket ->> '$.fish' = ?19
WHERE JSON_UNQUOTE(JSON_EXTRACT(basket, '$.fish')) = ?110
ORDER BY id;10
ORDER BY id;What sgl generates
PlaceOrderidentical
MySQL
MariaDB
1
func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) error1
func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) error2
2
3
type PlaceOrderParams struct {3
type PlaceOrderParams struct {4
Customer string4
Customer string5
Basket JSON5
Basket JSON6
Delivery JSON6
Delivery JSON7
}7
}8
8
9
type JSON []byte9
type JSON []byteGetOrderidentical
MySQL
MariaDB
1
func (queries *Queries) GetOrder(ctx context.Context, id int64) (GetOrderRow, error)1
func (queries *Queries) GetOrder(ctx context.Context, id int64) (GetOrderRow, error)2
2
3
type GetOrderRow struct {3
type GetOrderRow struct {4
ID int64 `json:"id"`4
ID int64 `json:"id"`5
Customer string `json:"customer"`5
Customer string `json:"customer"`6
Basket JSON `json:"basket"`6
Basket JSON `json:"basket"`7
Delivery JSON `json:"delivery"`7
Delivery JSON `json:"delivery"`8
}8
}9
9
10
type JSON []byte10
type JSON []byteOrdersWithFishidentical
MySQL
MariaDB
1
func (queries *Queries) OrdersWithFish(ctx context.Context, fish string) ([]OrdersWithFishRow, error)1
func (queries *Queries) OrdersWithFish(ctx context.Context, fish string) ([]OrdersWithFishRow, error)2
2
3
type OrdersWithFishRow struct {3
type OrdersWithFishRow struct {4
ID int64 `json:"id"`4
ID int64 `json:"id"`5
Customer string `json:"customer"`5
Customer string `json:"customer"`6
Extras JSON `json:"extras"`6
Extras JSON `json:"extras"`7
Address *string `json:"address"`7
Address *string `json:"address"`8
}8
}9
9
10
type JSON []byte10
type JSON []byteThe program
example.godiffers
MySQL
MariaDB
1
package main1
package main2
2
3
import (3
import (4
"context"4
"context"5
"database/sql"5
"database/sql"6
"encoding/json"6
"encoding/json"7
"fmt"7
"fmt"8
"io"8
"io"9
9
10
"seagull.sh/seagull/examples/mysql/006_json/internal/store"10
"seagull.sh/seagull/examples/mariadb/006_json/internal/store"11
)11
)12
12
13
// basket is the document an order's basket column holds.13
// basket is the document an order's basket column holds.14
type basket struct {14
type basket struct {15
// Fish is the fish the order is for.15
// Fish is the fish the order is for.16
Fish string `json:"fish"`16
Fish string `json:"fish"`17
17
18
// Portions is how many portions of fish and chips.18
// Portions is how many portions of fish and chips.19
Portions int `json:"portions"`19
Portions int `json:"portions"`20
20
21
// Extras lists the sides.21
// Extras lists the sides.22
Extras []string `json:"extras"`22
Extras []string `json:"extras"`23
}23
}24
24
25
// delivery is the document an order's delivery column holds, when the order is delivered.25
// delivery is the document an order's delivery column holds, when the order is delivered.26
type delivery struct {26
type delivery struct {27
// Address is where the order goes.27
// Address is where the order goes.28
Address string `json:"address"`28
Address string `json:"address"`29
}29
}30
30
31
// run places three chip shop orders and reads them back. A JSON column becomes the31
// run places three chip shop orders and reads them back. A JSON column becomes the32
// generated store.JSON type, which holds the raw document as bytes: encode a value into32
// generated store.JSON type, which holds the raw document as bytes: encode a value into33
// it with json.Marshal and decode it with json.Unmarshal. A nil store.JSON is SQL NULL.33
// it with json.Marshal and decode it with json.Unmarshal. A nil store.JSON is SQL NULL.34
//34
//35
// MySQL checks each document as it is stored and keeps it in a binary form, so what comes35
// In MariaDB, JSON is another name for LONGTEXT with a JSON_VALID check. An invalid36
// back is MySQL's own text for it: keys reordered, with a space after each colon and36
// document is refused, and a valid one is kept as the text it was written as, so it comes37
// comma. Decoding gives the same values either way.37
// back byte for byte: here, in the field order and compact form json.Marshal gave it.38
//38
//39
// OrdersWithFish filters with ->>, which extracts a value as SQL text, and selects with39
// MariaDB has no -> or ->> operators, and sgl vet reports Q010 for them, naming the40
// ->, which extracts a JSON fragment, so Extras is store.JSON again. Address is *string40
// function to use instead. OrdersWithFish calls those functions. JSON_EXTRACT returns a41
// because ->> gives NULL when the delivery column or its address is missing. They are41
// JSON fragment, so Extras is store.JSON again, written in MariaDB's own style with a42
// MySQL's shorthand for JSON_EXTRACT and JSON_UNQUOTE(JSON_EXTRACT(...)).42
// space after each comma. JSON_UNQUOTE(JSON_EXTRACT(...)) returns SQL text, so Address is43
// *string, NULL when the delivery column or its address is missing. The fish parameter is44
// compared with that text, so it is inferred as a string, and its binding line only gives45
// it a name in place of p1. JSON_VALUE(basket, '$.fish') is a shorter spelling for a46
// single value.43
//47
//44
// Takes database (*sql.DB) which is a migrated database.48
// Takes database (*sql.DB) which is a migrated database.45
// Takes output (io.Writer) which receives the decoded orders and the search.49
// Takes output (io.Writer) which receives the decoded orders and the search.46
//50
//47
// Returns error when a query fails or a document does not encode or decode.51
// Returns error when a query fails or a document does not encode or decode.48
func run(ctx context.Context, database *sql.DB, output io.Writer) error {52
func run(ctx context.Context, database *sql.DB, output io.Writer) error {49
queries := store.New(database)53
queries := store.New(database)50
54
51
orders := []struct {55
orders := []struct {52
customer string56
customer string53
basket basket57
basket basket54
delivery *delivery58
delivery *delivery55
}{59
}{56
{customer: "Steven", basket: basket{Fish: "cod", Portions: 2, Extras: []string{"mushy peas", "curry sauce"}}},60
{customer: "Steven", basket: basket{Fish: "cod", Portions: 2, Extras: []string{"mushy peas", "curry sauce"}}},57
{customer: "Gwen", basket: basket{Fish: "haddock", Portions: 1, Extras: []string{}}},61
{customer: "Gwen", basket: basket{Fish: "haddock", Portions: 1, Extras: []string{}}},58
{customer: "Morwenna", basket: basket{Fish: "cod", Portions: 1, Extras: []string{"pickled egg"}}, delivery: &delivery{Address: "4 Harbour Row"}},62
{customer: "Morwenna", basket: basket{Fish: "cod", Portions: 1, Extras: []string{"pickled egg"}}, delivery: &delivery{Address: "4 Harbour Row"}},59
}63
}60
for _, order := range orders {64
for _, order := range orders {61
encodedBasket, err := json.Marshal(order.basket)65
encodedBasket, err := json.Marshal(order.basket)62
if err != nil {66
if err != nil {63
return fmt.Errorf("encoding %s's basket: %w", order.customer, err)67
return fmt.Errorf("encoding %s's basket: %w", order.customer, err)64
}68
}65
parameters := store.PlaceOrderParams{Customer: order.customer, Basket: encodedBasket, Delivery: nil}69
parameters := store.PlaceOrderParams{Customer: order.customer, Basket: encodedBasket, Delivery: nil}66
if order.delivery != nil {70
if order.delivery != nil {67
encodedDelivery, err := json.Marshal(order.delivery)71
encodedDelivery, err := json.Marshal(order.delivery)68
if err != nil {72
if err != nil {69
return fmt.Errorf("encoding %s's delivery: %w", order.customer, err)73
return fmt.Errorf("encoding %s's delivery: %w", order.customer, err)70
}74
}71
parameters.Delivery = encodedDelivery75
parameters.Delivery = encodedDelivery72
}76
}73
if err := queries.PlaceOrder(ctx, parameters); err != nil {77
if err := queries.PlaceOrder(ctx, parameters); err != nil {74
return fmt.Errorf("placing %s's order: %w", order.customer, err)78
return fmt.Errorf("placing %s's order: %w", order.customer, err)75
}79
}76
}80
}77
81
78
first, err := queries.GetOrder(ctx, 1)82
first, err := queries.GetOrder(ctx, 1)79
if err != nil {83
if err != nil {80
return fmt.Errorf("getting order 1: %w", err)84
return fmt.Errorf("getting order 1: %w", err)81
}85
}82
var decoded basket86
var decoded basket83
if err := json.Unmarshal(first.Basket, &decoded); err != nil {87
if err := json.Unmarshal(first.Basket, &decoded); err != nil {84
return fmt.Errorf("decoding order 1's basket: %w", err)88
return fmt.Errorf("decoding order 1's basket: %w", err)85
}89
}86
fmt.Fprintf(output, "order 1 stored %s\n", first.Basket)90
fmt.Fprintf(output, "order 1 stored %s\n", first.Basket)87
fmt.Fprintf(output, "decoded: %d x %s with %v, delivery is NULL: %t\n", decoded.Portions, decoded.Fish, decoded.Extras, first.Delivery == nil)91
fmt.Fprintf(output, "decoded: %d x %s with %v, delivery is NULL: %t\n", decoded.Portions, decoded.Fish, decoded.Extras, first.Delivery == nil)88
92
89
cod, err := queries.OrdersWithFish(ctx, "cod")93
cod, err := queries.OrdersWithFish(ctx, "cod")90
if err != nil {94
if err != nil {91
return fmt.Errorf("finding cod orders: %w", err)95
return fmt.Errorf("finding cod orders: %w", err)92
}96
}93
fmt.Fprintln(output, "\ncod orders:")97
fmt.Fprintln(output, "\ncod orders:")94
for _, order := range cod {98
for _, order := range cod {95
address := "collecting"99
address := "collecting"96
if order.Address != nil {100
if order.Address != nil {97
address = "to " + *order.Address101
address = "to " + *order.Address98
}102
}99
fmt.Fprintf(output, " order %d %-9s extras %-30s %s\n", order.ID, order.Customer, order.Extras, address)103
fmt.Fprintf(output, " order %d %-9s extras %-30s %s\n", order.ID, order.Customer, order.Extras, address)100
}104
}101
return nil105
return nil102
}106
}