Skip to content

Commit 530182f

Browse files
authored
examples: Add MySQL authors example (sqlc-dev#682)
* examples: Add MySQL authors example * test: remove -v
1 parent ea268b2 commit 530182f

File tree

14 files changed

+203
-4
lines changed

14 files changed

+203
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
working-directory: internal/endtoend/testdata
3838

3939
- name: Test sqlc
40-
run: go test --tags=examples -v ./...
40+
run: go test --tags=examples ./...
4141
env:
4242
PG_USER: postgres
4343
PG_HOST: localhost

examples/authors/mysql/db_test.go

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

examples/authors/mysql/query.sql

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

examples/authors/mysql/query.sql.go

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

examples/authors/mysql/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 BIGINT PRIMARY KEY AUTO_INCREMENT,
3+
name text NOT NULL,
4+
bio text
5+
);

examples/authors/postgresql/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/postgresql/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.

0 commit comments

Comments
 (0)