From ac2ae9a4bcaf45e435372ad79ecf8fc078528650 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 9 Sep 2021 21:53:41 -0500 Subject: [PATCH] DB: add a sqlc database example Signed-off-by: Jeff Carr --- example-sqlc/authors/mysql/db_test.go | 47 ++++++++++++++++++++++ example-sqlc/authors/mysql/query.sql | 18 +++++++++ example-sqlc/authors/mysql/schema.sql | 5 +++ example-sqlc/authors/postgresql/db_test.go | 43 ++++++++++++++++++++ example-sqlc/authors/postgresql/query.sql | 19 +++++++++ example-sqlc/authors/postgresql/schema.sql | 5 +++ example-sqlc/authors/sqlc.json | 27 +++++++++++++ 7 files changed, 164 insertions(+) create mode 100644 example-sqlc/authors/mysql/db_test.go create mode 100644 example-sqlc/authors/mysql/query.sql create mode 100644 example-sqlc/authors/mysql/schema.sql create mode 100644 example-sqlc/authors/postgresql/db_test.go create mode 100644 example-sqlc/authors/postgresql/query.sql create mode 100644 example-sqlc/authors/postgresql/schema.sql create mode 100644 example-sqlc/authors/sqlc.json diff --git a/example-sqlc/authors/mysql/db_test.go b/example-sqlc/authors/mysql/db_test.go new file mode 100644 index 0000000..08c1122 --- /dev/null +++ b/example-sqlc/authors/mysql/db_test.go @@ -0,0 +1,47 @@ +// +build examples + +package authors + +import ( + "context" + "database/sql" + "testing" + + "github.com/kyleconroy/sqlc/internal/sqltest" +) + +func TestAuthors(t *testing.T) { + sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"}) + defer cleanup() + + ctx := context.Background() + db := New(sdb) + + // list all authors + authors, err := db.ListAuthors(ctx) + if err != nil { + t.Fatal(err) + } + t.Log(authors) + + // create an author + result, err := db.CreateAuthor(ctx, CreateAuthorParams{ + Name: "Brian Kernighan", + Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, + }) + if err != nil { + t.Fatal(err) + } + authorID, err := result.LastInsertId() + if err != nil { + t.Fatal(err) + } + t.Log(authorID) + + // get the author we just inserted + fetchedAuthor, err := db.GetAuthor(ctx, authorID) + if err != nil { + t.Fatal(err) + } + t.Log(fetchedAuthor) +} diff --git a/example-sqlc/authors/mysql/query.sql b/example-sqlc/authors/mysql/query.sql new file mode 100644 index 0000000..c3b5866 --- /dev/null +++ b/example-sqlc/authors/mysql/query.sql @@ -0,0 +1,18 @@ +/* name: GetAuthor :one */ +SELECT * FROM authors +WHERE id = ? LIMIT 1; + +/* name: ListAuthors :many */ +SELECT * FROM authors +ORDER BY name; + +/* name: CreateAuthor :execresult */ +INSERT INTO authors ( + name, bio +) VALUES ( + ?, ? +); + +/* name: DeleteAuthor :exec */ +DELETE FROM authors +WHERE id = ?; diff --git a/example-sqlc/authors/mysql/schema.sql b/example-sqlc/authors/mysql/schema.sql new file mode 100644 index 0000000..581ecfe --- /dev/null +++ b/example-sqlc/authors/mysql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + name text NOT NULL, + bio text +); diff --git a/example-sqlc/authors/postgresql/db_test.go b/example-sqlc/authors/postgresql/db_test.go new file mode 100644 index 0000000..45f3188 --- /dev/null +++ b/example-sqlc/authors/postgresql/db_test.go @@ -0,0 +1,43 @@ +// +build examples + +package authors + +import ( + "context" + "database/sql" + "testing" + + "github.com/kyleconroy/sqlc/internal/sqltest" +) + +func TestAuthors(t *testing.T) { + sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema.sql"}) + defer cleanup() + + ctx := context.Background() + db := New(sdb) + + // list all authors + authors, err := db.ListAuthors(ctx) + if err != nil { + t.Fatal(err) + } + t.Log(authors) + + // create an author + insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{ + Name: "Brian Kernighan", + Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, + }) + if err != nil { + t.Fatal(err) + } + t.Log(insertedAuthor) + + // get the author we just inserted + fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID) + if err != nil { + t.Fatal(err) + } + t.Log(fetchedAuthor) +} diff --git a/example-sqlc/authors/postgresql/query.sql b/example-sqlc/authors/postgresql/query.sql new file mode 100644 index 0000000..75e38b2 --- /dev/null +++ b/example-sqlc/authors/postgresql/query.sql @@ -0,0 +1,19 @@ +-- name: GetAuthor :one +SELECT * FROM authors +WHERE id = $1 LIMIT 1; + +-- name: ListAuthors :many +SELECT * FROM authors +ORDER BY name; + +-- name: CreateAuthor :one +INSERT INTO authors ( + name, bio +) VALUES ( + $1, $2 +) +RETURNING *; + +-- name: DeleteAuthor :exec +DELETE FROM authors +WHERE id = $1; diff --git a/example-sqlc/authors/postgresql/schema.sql b/example-sqlc/authors/postgresql/schema.sql new file mode 100644 index 0000000..b4fad78 --- /dev/null +++ b/example-sqlc/authors/postgresql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/example-sqlc/authors/sqlc.json b/example-sqlc/authors/sqlc.json new file mode 100644 index 0000000..58feec2 --- /dev/null +++ b/example-sqlc/authors/sqlc.json @@ -0,0 +1,27 @@ +{ + "version": "2", + "sql": [ + { + "schema": "postgresql/schema.sql", + "queries": "postgresql/query.sql", + "engine": "postgresql", + "gen": { + "go": { + "package": "authors", + "out": "postgresql" + } + } + }, + { + "schema": "mysql/schema.sql", + "queries": "mysql/query.sql", + "engine": "mysql", + "gen": { + "go": { + "package": "authors", + "out": "mysql" + } + } + } + ] +}