DB: add a sqlc database example
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
b9f59ddd81
commit
ac2ae9a4bc
|
@ -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)
|
||||
}
|
|
@ -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 = ?;
|
|
@ -0,0 +1,5 @@
|
|||
CREATE TABLE authors (
|
||||
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
||||
name text NOT NULL,
|
||||
bio text
|
||||
);
|
|
@ -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)
|
||||
}
|
|
@ -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;
|
|
@ -0,0 +1,5 @@
|
|||
CREATE TABLE authors (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
bio text
|
||||
);
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue