Compare · 005

From SQL types to Go types: SQLite and TimescaleDB

Every file of the SQLite and TimescaleDB versions side by side, with the lines that differ marked. 6 parameter or column types differ in Go.

Configuration

sgl.yamldiffers
SQLite
TimescaleDB
1version: 1
1version: 1
2
2
3databases:
3databases:
4 - name: landings
4 - name: landings
5 dialect: sqlite
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

Migrations

db/migrations/0001_landings.up.sqldiffers
SQLite
TimescaleDB
1CREATE TABLE landings (
1CREATE TABLE landings (
2 id INTEGER PRIMARY KEY,
2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3 species TEXT NOT NULL,
3 catch_id UUID NOT NULL,
4 boxes SMALLINT NOT NULL,
4 species TEXT NOT NULL,
5 fish INTEGER NOT NULL,
5 boxes SMALLINT NOT NULL,
6 weight_g BIGINT NOT NULL,
6 fish INTEGER NOT NULL,
7 length_cm REAL NOT NULL,
7 weight_g BIGINT NOT NULL,
8 on_ice BOOLEAN NOT NULL,
8 length_cm REAL NOT NULL,
9 landed_at DATETIME NOT NULL,
9 sea_temp DOUBLE PRECISION NOT NULL,
10 tag BLOB NOT NULL,
10 price NUMERIC(8, 2) NOT NULL,
11 on_ice BOOLEAN NOT NULL,
12 landed_on DATE NOT NULL,
13 landed_at TIMESTAMPTZ NOT NULL,
14 harbour_ip INET NOT NULL,
15 tag BYTEA NOT NULL,
11 notes TEXT
16 notes TEXT
12);
17);
db/migrations/0001_landings.down.sqlidentical
SQLite
TimescaleDB
1DROP TABLE landings;
1DROP TABLE landings;

Queries

RecordLandingdiffers
SQLite · db/queries/landings.sql
TimescaleDB · db/queries/landings.sql
1-- sgl.query(RecordLanding, one)
1-- sgl.query(RecordLanding, one)
2INSERT INTO landings (species, boxes, fish, weight_g, length_cm, on_ice, landed_at, tag, notes)
2INSERT INTO landings (catch_id, species, boxes, fish, weight_g, length_cm, sea_temp, price, on_ice, landed_on, landed_at, harbour_ip, tag, notes)
3VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
3VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
4RETURNING id;
4RETURNING id;
GetLandingdiffers
SQLite · db/queries/landings.sql
TimescaleDB · db/queries/landings.sql
1-- sgl.query(GetLanding, one)
1-- sgl.query(GetLanding, one)
2SELECT id, species, boxes, fish, weight_g, length_cm, on_ice, landed_at, tag, notes
2SELECT id, catch_id, species, boxes, fish, weight_g, length_cm, sea_temp, price, on_ice, landed_on, landed_at, harbour_ip, tag, notes
3FROM landings
3FROM landings
4WHERE id = ?;
4WHERE id = $1;

What sgl generates

RecordLandingdiffers
SQLite
TimescaleDB
1func (queries *Queries) RecordLanding(ctx context.Context, params RecordLandingParams) (RecordLandingRow, error)
1func (queries *Queries) RecordLanding(ctx context.Context, params RecordLandingParams) (RecordLandingRow, error)
2
2
3type RecordLandingParams struct {
3type RecordLandingParams struct {
4 Species string
4 CatchID uuid.UUID
5 Boxes int64
5 Species string
6 Fish int64
6 Boxes int16
7 WeightG int64
7 Fish int32
8 LengthCm float64
8 WeightG int64
9 OnIce bool
9 LengthCm float32
10 LandedAt time.Time
10 SeaTemp float64
11 Tag []byte
11 Price apd.Decimal
12 Notes *string
12 OnIce bool
13 LandedOn time.Time
14 LandedAt time.Time
15 HarbourIP any
16 Tag []byte
17 Notes *string
13}
18}
14
19
15type RecordLandingRow struct {
20type RecordLandingRow struct {
16 ID int64 `json:"id"`
21 ID int64 `json:"id"`
17}
22}
GetLandingdiffers
SQLite
TimescaleDB
1func (queries *Queries) GetLanding(ctx context.Context, id int64) (GetLandingRow, error)
1func (queries *Queries) GetLanding(ctx context.Context, id int64) (GetLandingRow, error)
2
2
3type GetLandingRow struct {
3type GetLandingRow struct {
4 ID int64 `json:"id"`
4 ID int64 `json:"id"`
5 Species string `json:"species"`
5 CatchID uuid.UUID `json:"catch_id"`
6 Boxes int64 `json:"boxes"`
6 Species string `json:"species"`
7 Fish int64 `json:"fish"`
7 Boxes int16 `json:"boxes"`
8 WeightG int64 `json:"weight_g"`
8 Fish int32 `json:"fish"`
9 LengthCm float64 `json:"length_cm"`
9 WeightG int64 `json:"weight_g"`
10 OnIce bool `json:"on_ice"`
10 LengthCm float32 `json:"length_cm"`
11 LandedAt time.Time `json:"landed_at"`
11 SeaTemp float64 `json:"sea_temp"`
12 Tag []byte `json:"tag"`
12 Price apd.Decimal `json:"price"`
13 Notes *string `json:"notes"`
13 OnIce bool `json:"on_ice"`
14 LandedOn time.Time `json:"landed_on"`
15 LandedAt time.Time `json:"landed_at"`
16 HarbourIP any `json:"harbour_ip"`
17 Tag []byte `json:"tag"`
18 Notes *string `json:"notes"`
14}
19}

The program

example.godiffers
SQLite
TimescaleDB
1package main
1package main
2
2
3import (
3import (
4 "context"
4 "context"
5 "database/sql"
5 "database/sql"
6 "fmt"
6 "fmt"
7 "io"
7 "io"
8 "time"
8 "time"
9
9
10 "seagull.sh/seagull/examples/sqlite/005_type_mapping/internal/store"
10 "github.com/cockroachdb/apd/v3"
11 "github.com/google/uuid"
12
13 "seagull.sh/seagull/examples/postgres/005_type_mapping/internal/store"
11)
14)
12
15
13// run records a landing of fish and reads it back, printing the Go type sgl chose for
16// run records a landing of fish and reads it back, printing the Go type sgl chose for
14// each column. SQLite stores every value as one of five storage classes, so Seagull maps
17// each column of db/migrations. PostgreSQL's types are exact, so each maps by name:
15// the type each column declares in db/migrations. SMALLINT, INTEGER and BIGINT all have
16// INTEGER affinity, which SQLite stores in up to eight bytes whatever width the column
17// declares, so each gives int64, as the INTEGER PRIMARY KEY rowid does. REAL gives
18// float64, and DATETIME gives time.Time, which the driver parses back from the text it
19// stored. SQLite has no exact decimal type, so money is best kept as INTEGER pence; a
20// DECIMAL column maps to apd.Decimal from github.com/cockroachdb/apd/v3.
21//
18//
19// - SMALLINT, INTEGER and BIGINT give int16, int32 and int64;
20// - REAL is single precision and gives float32, while DOUBLE PRECISION gives float64;
21// - NUMERIC gives apd.Decimal from github.com/cockroachdb/apd/v3, which keeps money
22// exact, and UUID gives uuid.UUID from github.com/google/uuid;
23// - DATE and TIMESTAMPTZ give time.Time, and BYTEA gives []byte;
24// - a type Seagull has no mapping for, such as INET, gives any, and the driver decides
25// what it holds: pgx's database/sql driver hands back the address as a string. Give
26// such a column a go_type override to pin it down.
27//
22// Takes database (*sql.DB) which is a migrated database.
28// Takes database (*sql.DB) which is a migrated database.
23// Takes output (io.Writer) which receives one line per column.
29// Takes output (io.Writer) which receives one line per column.
24//
30//
25// Returns error when a query fails.
31// Returns error when a query fails.
26func run(ctx context.Context, database *sql.DB, output io.Writer) error {
32func run(ctx context.Context, database *sql.DB, output io.Writer) error {
27 queries := store.New(database)
33 queries := store.New(database)
28
34
29 recorded, err := queries.RecordLanding(ctx, store.RecordLandingParams{
35 recorded, err := queries.RecordLanding(ctx, store.RecordLandingParams{
30 Species: "mackerel",
36 CatchID: uuid.MustParse("6f1c0b8e-4b2a-4c55-9d7e-2a3f5e8c1d40"),
31 Boxes: 12,
37 Species: "mackerel",
32 Fish: 480,
38 Boxes: 12,
33 WeightG: 163_200,
39 Fish: 480,
34 LengthCm: 31.5,
40 WeightG: 163_200,
35 OnIce: true,
41 LengthCm: 31.5,
36 LandedAt: time.Date(2026, time.May, 1, 5, 30, 0, 0, time.UTC),
42 SeaTemp: 12.25,
37 Tag: []byte{0x04, 0xa2, 0x19, 0xf3},
43 Price: *apd.New(41_280, -2),
38 Notes: new("landed at Newlyn"),
44 OnIce: true,
45 LandedOn: time.Date(2026, time.May, 1, 0, 0, 0, 0, time.UTC),
46 LandedAt: time.Date(2026, time.May, 1, 5, 30, 0, 0, time.UTC),
47 HarbourIP: "192.0.2.17",
48 Tag: []byte{0x04, 0xa2, 0x19, 0xf3},
49 Notes: new("landed at Newlyn"),
39 })
50 })
40 if err != nil {
51 if err != nil {
41 return fmt.Errorf("recording a landing: %w", err)
52 return fmt.Errorf("recording a landing: %w", err)
42 }
53 }
43
54
44 landing, err := queries.GetLanding(ctx, recorded.ID)
55 landing, err := queries.GetLanding(ctx, recorded.ID)
45 if err != nil {
56 if err != nil {
46 return fmt.Errorf("getting landing %d: %w", recorded.ID, err)
57 return fmt.Errorf("getting landing %d: %w", recorded.ID, err)
47 }
58 }
48 fmt.Fprintf(output, "id %-10T %d\n", landing.ID, landing.ID)
59 fmt.Fprintf(output, "id %-12T %d\n", landing.ID, landing.ID)
49 fmt.Fprintf(output, "species %-10T %s\n", landing.Species, landing.Species)
60 fmt.Fprintf(output, "catch_id %-12T %s\n", landing.CatchID, landing.CatchID)
50 fmt.Fprintf(output, "boxes %-10T %d\n", landing.Boxes, landing.Boxes)
61 fmt.Fprintf(output, "species %-12T %s\n", landing.Species, landing.Species)
51 fmt.Fprintf(output, "fish %-10T %d\n", landing.Fish, landing.Fish)
62 fmt.Fprintf(output, "boxes %-12T %d\n", landing.Boxes, landing.Boxes)
52 fmt.Fprintf(output, "weight_g %-10T %d\n", landing.WeightG, landing.WeightG)
63 fmt.Fprintf(output, "fish %-12T %d\n", landing.Fish, landing.Fish)
53 fmt.Fprintf(output, "length_cm %-10T %.1f\n", landing.LengthCm, landing.LengthCm)
64 fmt.Fprintf(output, "weight_g %-12T %d\n", landing.WeightG, landing.WeightG)
54 fmt.Fprintf(output, "on_ice %-10T %t\n", landing.OnIce, landing.OnIce)
65 fmt.Fprintf(output, "length_cm %-12T %.1f\n", landing.LengthCm, landing.LengthCm)
55 fmt.Fprintf(output, "landed_at %-10T %s\n", landing.LandedAt, landing.LandedAt.Format(time.RFC3339))
66 fmt.Fprintf(output, "sea_temp %-12T %.2f\n", landing.SeaTemp, landing.SeaTemp)
67 fmt.Fprintf(output, "price %-12T %s\n", landing.Price, landing.Price.String())
68 fmt.Fprintf(output, "on_ice %-12T %t\n", landing.OnIce, landing.OnIce)
69 fmt.Fprintf(output, "landed_on %-12T %s\n", landing.LandedOn, landing.LandedOn.Format(time.DateOnly))
70 // TIMESTAMPTZ arrives in the connection's time zone, so print it in UTC.
71 fmt.Fprintf(output, "landed_at %-12T %s\n", landing.LandedAt, landing.LandedAt.UTC().Format(time.RFC3339))
72 fmt.Fprintf(output, "harbour_ip %-12T %v\n", landing.HarbourIP, landing.HarbourIP)
56 // %T prints []byte as []uint8, since byte is an alias for uint8.
73 // %T prints []byte as []uint8, since byte is an alias for uint8.
57 fmt.Fprintf(output, "tag %-10T % x\n", landing.Tag, landing.Tag)
74 fmt.Fprintf(output, "tag %-12T % x\n", landing.Tag, landing.Tag)
58 fmt.Fprintf(output, "notes %-10T %s\n", landing.Notes, *landing.Notes)
75 fmt.Fprintf(output, "notes %-12T %s\n", landing.Notes, *landing.Notes)
59 return nil
76 return nil
60}
77}

Go type differences

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

QueryFieldSQLiteTimescaleDB
RecordLandingparameter boxesint64int16
RecordLandingparameter fishint64int32
RecordLandingparameter length_cmfloat64float32
GetLandingcolumn boxesint64int16
GetLandingcolumn fishint64int32
GetLandingcolumn length_cmfloat64float32