Your first query: SQLite and MariaDB
Every file of the SQLite and MariaDB versions side by side, with the lines that differ marked. 4 parameter or column types differ in Go.
Configuration
sgl.yamldiffers
SQLite
MariaDB
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: tasks4
- name: tasks5
dialect: sqlite5
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_init.up.sqldiffers
SQLite
MariaDB
1
CREATE TABLE tasks (1
CREATE TABLE tasks (2
id INTEGER PRIMARY KEY,2
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,3
title TEXT NOT NULL,3
title VARCHAR(200) NOT NULL,4
completed INTEGER NOT NULL DEFAULT 0,4
completed INT NOT NULL DEFAULT 0,5
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP5
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP6
);6
);db/migrations/0001_init.down.sqlidentical
SQLite
MariaDB
1
DROP TABLE tasks;1
DROP TABLE tasks;Queries
CreateTaskidentical
SQLite · db/queries/tasks.sql
MariaDB · db/queries/tasks.sql
1
-- sgl.query(CreateTask, one)1
-- sgl.query(CreateTask, one)2
INSERT INTO tasks (title) VALUES (?) RETURNING id, title, completed, created_at;2
INSERT INTO tasks (title) VALUES (?) RETURNING id, title, completed, created_at;ListTasksidentical
SQLite · db/queries/tasks.sql
MariaDB · db/queries/tasks.sql
1
-- sgl.query(ListTasks, many)1
-- sgl.query(ListTasks, many)2
SELECT id, title, completed, created_at FROM tasks ORDER BY id;2
SELECT id, title, completed, created_at FROM tasks ORDER BY id;CompleteTaskidentical
SQLite · db/queries/tasks.sql
MariaDB · db/queries/tasks.sql
1
-- sgl.query(CompleteTask, execrows)1
-- sgl.query(CompleteTask, execrows)2
UPDATE tasks SET completed = 1 WHERE id = ?;2
UPDATE tasks SET completed = 1 WHERE id = ?;What sgl generates
CreateTaskdiffers
SQLite
MariaDB
1
func (queries *Queries) CreateTask(ctx context.Context, title string) (CreateTaskRow, error)1
func (queries *Queries) CreateTask(ctx context.Context, title string) (CreateTaskRow, error)2
2
3
type CreateTaskRow struct {3
type CreateTaskRow struct {4
ID int64 `json:"id"`4
ID int64 `json:"id"`5
Title string `json:"title"`5
Title string `json:"title"`6
Completed int64 `json:"completed"`6
Completed int32 `json:"completed"`7
CreatedAt string `json:"created_at"`7
CreatedAt time.Time `json:"created_at"`8
}8
}ListTasksdiffers
SQLite
MariaDB
1
func (queries *Queries) ListTasks(ctx context.Context) ([]ListTasksRow, error)1
func (queries *Queries) ListTasks(ctx context.Context) ([]ListTasksRow, error)2
2
3
type ListTasksRow struct {3
type ListTasksRow struct {4
ID int64 `json:"id"`4
ID int64 `json:"id"`5
Title string `json:"title"`5
Title string `json:"title"`6
Completed int64 `json:"completed"`6
Completed int32 `json:"completed"`7
CreatedAt string `json:"created_at"`7
CreatedAt time.Time `json:"created_at"`8
}8
}CompleteTaskidentical
SQLite
MariaDB
1
func (queries *Queries) CompleteTask(ctx context.Context, id int64) (int64, error)1
func (queries *Queries) CompleteTask(ctx context.Context, id int64) (int64, error)The program
example.godiffers
SQLite
MariaDB
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/sqlite/001_first_query/internal/store"9
"seagull.sh/seagull/examples/mariadb/001_first_query/internal/store"10
)10
)11
11
12
// run adds two tasks, completes the first and lists them all, through the methods sgl12
// run adds two tasks, completes the first and lists them all, through the methods sgl13
// generated from db/queries/tasks.sql.13
// generated from db/queries/tasks.sql.14
//14
//15
// MariaDB, unlike MySQL, has INSERT ... RETURNING, so CreateTask is a one query that16
// hands back the new row, the AUTO_INCREMENT id and the column defaults included.17
//15
// Takes database (*sql.DB) which is a migrated database.18
// Takes database (*sql.DB) which is a migrated database.16
// Takes output (io.Writer) which receives the task list.19
// Takes output (io.Writer) which receives the task list.17
//20
//18
// Returns error when a query fails.21
// Returns error when a query fails.19
func run(ctx context.Context, database *sql.DB, output io.Writer) error {22
func run(ctx context.Context, database *sql.DB, output io.Writer) error {20
queries := store.New(database)23
queries := store.New(database)21
24
22
feed, err := queries.CreateTask(ctx, "Feed the seagulls")25
feed, err := queries.CreateTask(ctx, "Feed the seagulls")23
if err != nil {26
if err != nil {24
return fmt.Errorf("creating a task: %w", err)27
return fmt.Errorf("creating a task: %w", err)25
}28
}26
if _, err := queries.CreateTask(ctx, "Count the chips"); err != nil {29
if _, err := queries.CreateTask(ctx, "Count the chips"); err != nil {27
return fmt.Errorf("creating a task: %w", err)30
return fmt.Errorf("creating a task: %w", err)28
}31
}29
32
30
completed, err := queries.CompleteTask(ctx, feed.ID)33
completed, err := queries.CompleteTask(ctx, feed.ID)31
if err != nil {34
if err != nil {32
return fmt.Errorf("completing task %d: %w", feed.ID, err)35
return fmt.Errorf("completing task %d: %w", feed.ID, err)33
}36
}34
fmt.Fprintf(output, "completed %d task\n", completed)37
fmt.Fprintf(output, "completed %d task\n", completed)35
38
36
tasks, err := queries.ListTasks(ctx)39
tasks, err := queries.ListTasks(ctx)37
if err != nil {40
if err != nil {38
return fmt.Errorf("listing tasks: %w", err)41
return fmt.Errorf("listing tasks: %w", err)39
}42
}40
for _, task := range tasks {43
for _, task := range tasks {41
fmt.Fprintf(output, "%d\t%-18s completed=%d\n", task.ID, task.Title, task.Completed)44
fmt.Fprintf(output, "%d\t%-18s completed=%d\n", task.ID, task.Title, task.Completed)42
}45
}43
return nil46
return nil44
}47
}Go type differences
The same SQL types map to different Go types, because each database reports them differently.
| Query | Field | SQLite | MariaDB |
|---|---|---|---|
| CreateTask | column completed | int64 | int32 |
| CreateTask | column created_at | string | time.Time |
| ListTasks | column completed | int64 | int32 |
| ListTasks | column created_at | string | time.Time |