IN lists from Go slices: MySQL and SQLite
Every file of the MySQL and SQLite versions side by side, with the lines that differ marked.
Configuration
sgl.yamldiffers
MySQL
SQLite
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: huts4
- name: huts5
dialect: mysql5
dialect: sqlite6
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_huts.up.sqldiffers
MySQL
SQLite
1
CREATE TABLE huts (1
CREATE TABLE huts (2
number BIGINT NOT NULL PRIMARY KEY,2
number INTEGER PRIMARY KEY,3
beach VARCHAR(50) NOT NULL,3
beach TEXT NOT NULL,4
colour VARCHAR(20) NOT NULL,4
colour TEXT NOT NULL,5
booked BOOLEAN NOT NULL DEFAULT FALSE5
booked BOOLEAN NOT NULL DEFAULT 06
);6
);db/migrations/0001_huts.down.sqlidentical
MySQL
SQLite
1
DROP TABLE huts;1
DROP TABLE huts;Queries
AddHutidentical
MySQL · db/queries/huts.sql
SQLite · db/queries/huts.sql
1
-- sgl.query(AddHut, exec)1
-- sgl.query(AddHut, exec)2
INSERT INTO huts (number, beach, colour) VALUES (?, ?, ?);2
INSERT INTO huts (number, beach, colour) VALUES (?, ?, ?);HutsOnBeachesidentical
MySQL · db/queries/huts.sql
SQLite · db/queries/huts.sql
1
-- sgl.query(HutsOnBeaches, many)1
-- sgl.query(HutsOnBeaches, many)2
-- ?1 as sgl.param(beaches, kind: slice)2
-- ?1 as sgl.param(beaches, kind: slice)3
SELECT number, beach, colour, booked3
SELECT number, beach, colour, booked4
FROM huts4
FROM huts5
WHERE beach IN (?1)5
WHERE beach IN (?1)6
ORDER BY number;6
ORDER BY number;BookHutsdiffers
MySQL · db/queries/huts.sql
SQLite · db/queries/huts.sql
1
-- sgl.query(BookHuts, execrows)1
-- sgl.query(BookHuts, execrows)2
-- ?1 as sgl.param(numbers, kind: slice)2
-- ?1 as sgl.param(numbers, kind: slice)3
UPDATE huts SET booked = TRUE WHERE number IN (?1) AND booked = FALSE;3
UPDATE huts SET booked = 1 WHERE number IN (?1) AND booked = 0;FreeHutsInColoursdiffers
MySQL · db/queries/huts.sql
SQLite · db/queries/huts.sql
1
-- sgl.query(FreeHutsInColours, many)1
-- sgl.query(FreeHutsInColours, many)2
-- ?1 as sgl.param(beach)2
-- ?1 as sgl.param(beach)3
-- ?2 as sgl.param(colours, kind: slice)3
-- ?2 as sgl.param(colours, kind: slice)4
SELECT number, colour4
SELECT number, colour5
FROM huts5
FROM huts6
WHERE beach = ?1 AND colour IN (?2) AND booked = FALSE6
WHERE beach = ?1 AND colour IN (?2) AND booked = 07
ORDER BY number;7
ORDER BY number;What sgl generates
AddHutidentical
MySQL
SQLite
1
func (queries *Queries) AddHut(ctx context.Context, params AddHutParams) error1
func (queries *Queries) AddHut(ctx context.Context, params AddHutParams) error2
2
3
type AddHutParams struct {3
type AddHutParams struct {4
Number int644
Number int645
Beach string5
Beach string6
Colour string6
Colour string7
}7
}HutsOnBeachesidentical
MySQL
SQLite
1
func (queries *Queries) HutsOnBeaches(ctx context.Context, params HutsOnBeachesParams) ([]HutsOnBeachesRow, error)1
func (queries *Queries) HutsOnBeaches(ctx context.Context, params HutsOnBeachesParams) ([]HutsOnBeachesRow, error)2
2
3
type HutsOnBeachesParams struct {3
type HutsOnBeachesParams struct {4
Beaches []string4
Beaches []string5
}5
}6
6
7
type HutsOnBeachesRow struct {7
type HutsOnBeachesRow struct {8
Number int64 `json:"number"`8
Number int64 `json:"number"`9
Beach string `json:"beach"`9
Beach string `json:"beach"`10
Colour string `json:"colour"`10
Colour string `json:"colour"`11
Booked bool `json:"booked"`11
Booked bool `json:"booked"`12
}12
}BookHutsidentical
MySQL
SQLite
1
func (queries *Queries) BookHuts(ctx context.Context, params BookHutsParams) (int64, error)1
func (queries *Queries) BookHuts(ctx context.Context, params BookHutsParams) (int64, error)2
2
3
type BookHutsParams struct {3
type BookHutsParams struct {4
Numbers []int644
Numbers []int645
}5
}FreeHutsInColoursidentical
MySQL
SQLite
1
func (queries *Queries) FreeHutsInColours(ctx context.Context, params FreeHutsInColoursParams) ([]FreeHutsInColoursRow, error)1
func (queries *Queries) FreeHutsInColours(ctx context.Context, params FreeHutsInColoursParams) ([]FreeHutsInColoursRow, error)2
2
3
type FreeHutsInColoursParams struct {3
type FreeHutsInColoursParams struct {4
Beach string4
Beach string5
Colours []string5
Colours []string6
}6
}7
7
8
type FreeHutsInColoursRow struct {8
type FreeHutsInColoursRow struct {9
Number int64 `json:"number"`9
Number int64 `json:"number"`10
Colour string `json:"colour"`10
Colour string `json:"colour"`11
}11
}The program
example.godiffers
MySQL
SQLite
1
package main1
package main2
2
3
import (3
import (4
"context"4
"context"5
"database/sql"5
"database/sql"6
"fmt"6
"fmt"7
"io"7
"io"8
8
9
"seagull.sh/seagull/examples/mysql/010_slices/internal/store"9
"seagull.sh/seagull/examples/sqlite/010_slices/internal/store"10
)10
)11
11
12
// run lets out beach huts by passing Go slices where the SQL has an IN list. A binding12
// run lets out beach huts by passing Go slices where the SQL has an IN list. A binding13
// such as ?1 as sgl.param(beaches, kind: slice) makes the parameter a slice, and the13
// such as ?1 as sgl.param(beaches, kind: slice) makes the parameter a slice, and the14
// generated method rewrites IN (?1) to one ? per element at call time; the generated SQL14
// generated method rewrites IN (?1) to one placeholder per element at call time. A slice15
// already writes every other ?N as a plain ?, the only placeholder MySQL takes. An empty15
// is numbered after every other parameter, so no other placeholder moves. An empty slice16
// slice turns IN (?1) into IN (SELECT NULL FROM DUAL WHERE FALSE), which matches nothing.16
// turns IN (?1) into IN (SELECT NULL WHERE 0), which matches nothing.17
//17
//18
// The rewrite happens before the statement runs, so sgl vet refuses a slice parameter on18
// The rewrite happens before the statement runs, so sgl vet refuses a slice parameter on19
// a batch or copyfrom query (Q017) or a dynamic: runtime query (Q018).19
// a batch or copyfrom query (Q017) or a dynamic: runtime query (Q018).20
//20
//21
// Takes database (*sql.DB) which is a migrated database.21
// Takes database (*sql.DB) which is a migrated database.22
// Takes output (io.Writer) which receives each search and booking.22
// Takes output (io.Writer) which receives each search and booking.23
//23
//24
// Returns error when a query fails.24
// Returns error when a query fails.25
func run(ctx context.Context, database *sql.DB, output io.Writer) error {25
func run(ctx context.Context, database *sql.DB, output io.Writer) error {26
queries := store.New(database)26
queries := store.New(database)27
27
28
huts := []store.AddHutParams{28
huts := []store.AddHutParams{29
{Number: 1, Beach: "Polzeath", Colour: "blue"},29
{Number: 1, Beach: "Polzeath", Colour: "blue"},30
{Number: 2, Beach: "Polzeath", Colour: "yellow"},30
{Number: 2, Beach: "Polzeath", Colour: "yellow"},31
{Number: 3, Beach: "Polzeath", Colour: "red"},31
{Number: 3, Beach: "Polzeath", Colour: "red"},32
{Number: 4, Beach: "Watergate", Colour: "blue"},32
{Number: 4, Beach: "Watergate", Colour: "blue"},33
{Number: 5, Beach: "Watergate", Colour: "red"},33
{Number: 5, Beach: "Watergate", Colour: "red"},34
{Number: 6, Beach: "Fistral", Colour: "yellow"},34
{Number: 6, Beach: "Fistral", Colour: "yellow"},35
}35
}36
for _, hut := range huts {36
for _, hut := range huts {37
if err := queries.AddHut(ctx, hut); err != nil {37
if err := queries.AddHut(ctx, hut); err != nil {38
return fmt.Errorf("adding hut %d: %w", hut.Number, err)38
return fmt.Errorf("adding hut %d: %w", hut.Number, err)39
}39
}40
}40
}41
41
42
for _, beaches := range [][]string{{"Polzeath", "Fistral"}, {"Watergate"}, {}} {42
for _, beaches := range [][]string{{"Polzeath", "Fistral"}, {"Watergate"}, {}} {43
found, err := queries.HutsOnBeaches(ctx, store.HutsOnBeachesParams{Beaches: beaches})43
found, err := queries.HutsOnBeaches(ctx, store.HutsOnBeachesParams{Beaches: beaches})44
if err != nil {44
if err != nil {45
return fmt.Errorf("finding huts on %v: %w", beaches, err)45
return fmt.Errorf("finding huts on %v: %w", beaches, err)46
}46
}47
numbers := make([]int64, 0, len(found))47
numbers := make([]int64, 0, len(found))48
for _, hut := range found {48
for _, hut := range found {49
numbers = append(numbers, hut.Number)49
numbers = append(numbers, hut.Number)50
}50
}51
fmt.Fprintf(output, "huts on %v: %v\n", beaches, numbers)51
fmt.Fprintf(output, "huts on %v: %v\n", beaches, numbers)52
}52
}53
53
54
// Hut 3 is taken by the first booking, so the second books only hut 5.54
// Hut 3 is taken by the first booking, so the second books only hut 5.55
for _, numbers := range [][]int64{{1, 3, 4}, {3, 5}} {55
for _, numbers := range [][]int64{{1, 3, 4}, {3, 5}} {56
booked, err := queries.BookHuts(ctx, store.BookHutsParams{Numbers: numbers})56
booked, err := queries.BookHuts(ctx, store.BookHutsParams{Numbers: numbers})57
if err != nil {57
if err != nil {58
return fmt.Errorf("booking huts %v: %w", numbers, err)58
return fmt.Errorf("booking huts %v: %w", numbers, err)59
}59
}60
fmt.Fprintf(output, "booking huts %v booked %d\n", numbers, booked)60
fmt.Fprintf(output, "booking huts %v booked %d\n", numbers, booked)61
}61
}62
62
63
free, err := queries.FreeHutsInColours(ctx, store.FreeHutsInColoursParams{63
free, err := queries.FreeHutsInColours(ctx, store.FreeHutsInColoursParams{64
Beach: "Polzeath",64
Beach: "Polzeath",65
Colours: []string{"blue", "yellow"},65
Colours: []string{"blue", "yellow"},66
})66
})67
if err != nil {67
if err != nil {68
return fmt.Errorf("finding free huts: %w", err)68
return fmt.Errorf("finding free huts: %w", err)69
}69
}70
for _, hut := range free {70
for _, hut := range free {71
fmt.Fprintf(output, "hut %d at Polzeath is %s and free\n", hut.Number, hut.Colour)71
fmt.Fprintf(output, "hut %d at Polzeath is %s and free\n", hut.Number, hut.Colour)72
}72
}73
return nil73
return nil74
}74
}