Skip to content

Commit 75305f3

Browse files
authored
examples: Add the authors example (#241)
* examples: Add the authors example Taken from the existing code in the README * Use sql.NullString
1 parent bb4ead0 commit 75305f3

File tree

10 files changed

+208
-9
lines changed

10 files changed

+208
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ if err != nil {
9797
fmt.Println(authors)
9898

9999
// create an author
100-
insertedAuthor, err := db.CreateAuthor(ctx, &db.CreateAuthorParams{
100+
insertedAuthor, err := db.CreateAuthor(ctx, db.CreateAuthorParams{
101101
Name: "Brian Kernighan",
102-
Bio: "Co-author of The C Programming Language and The Go Programming Language",
102+
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
103103
})
104104
if err != nil {
105105
return err
106106
}
107107
fmt.Println(insertedAuthor)
108108

109109
// get the author we just inserted
110-
fetchedAuthor, err = db.GetAuthor(ctx, author.ID)
110+
fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID)
111111
if err != nil {
112112
return err
113113
}

examples/authors/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/db_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package authors
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"testing"
7+
8+
"github.com/kyleconroy/sqlc/internal/sqltest"
9+
)
10+
11+
func TestAuthors(t *testing.T) {
12+
sdb, cleanup := sqltest.PostgreSQL(t, "schema.sql")
13+
defer cleanup()
14+
15+
ctx := context.Background()
16+
db := New(sdb)
17+
18+
// list all authors
19+
authors, err := db.ListAuthors(ctx)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
t.Log(authors)
24+
25+
// create an author
26+
insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{
27+
Name: "Brian Kernighan",
28+
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
29+
})
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
t.Log(insertedAuthor)
34+
35+
// get the author we just inserted
36+
fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
t.Log(fetchedAuthor)
41+
}

examples/authors/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/query.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;

examples/authors/query.sql.go

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);

examples/ondeck/db_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/kyleconroy/sqlc/internal/sqltest"
1010

1111
"github.com/google/go-cmp/cmp"
12-
_ "github.com/lib/pq"
1312
)
1413

1514
func runOnDeckQueries(t *testing.T, q *Queries) {

examples/sqlc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"version": "1",
33
"packages": [
4+
{
5+
"path": "authors",
6+
"schema": "authors/schema.sql",
7+
"queries": "authors/query.sql",
8+
"engine": "postgresql"
9+
},
410
{
511
"path": "ondeck",
612
"schema": "ondeck/schema",

internal/sqltest/postgres.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ import (
88
"os"
99
"path/filepath"
1010
"testing"
11+
"time"
1112

1213
"github.com/kyleconroy/sqlc/internal/dinosql"
1314

1415
_ "github.com/lib/pq"
1516
)
1617

18+
func init() {
19+
rand.Seed(time.Now().UnixNano())
20+
}
21+
22+
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
23+
1724
func id() string {
18-
bytes := make([]byte, 10)
19-
for i := 0; i < 10; i++ {
20-
bytes[i] = byte(65 + rand.Intn(25)) // A=65 and Z = 65+25
25+
b := make([]rune, 10)
26+
for i := range b {
27+
b[i] = letterRunes[rand.Intn(len(letterRunes))]
2128
}
22-
return string(bytes)
29+
return string(b)
2330
}
2431

2532
func PostgreSQL(t *testing.T, migrations string) (*sql.DB, func()) {
@@ -55,7 +62,7 @@ func PostgreSQL(t *testing.T, migrations string) (*sql.DB, func()) {
5562
t.Fatal(err)
5663
}
5764

58-
schema := "dinotest_" + id()
65+
schema := "sqltest_" + id()
5966

6067
// For each test, pick a new schema name at random.
6168
// `foo` is used here only as an example

0 commit comments

Comments
 (0)