Skip to content

Commit dfa42b5

Browse files
committed
update booktest example for SQLite
use RETURNING and sqlc.slice.
1 parent 883ec29 commit dfa42b5

File tree

5 files changed

+85
-67
lines changed

5 files changed

+85
-67
lines changed

examples/booktest/sqlite/db_test.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ func TestBooks(t *testing.T) {
2525
dq := New(db)
2626

2727
// create an author
28-
result, err := dq.CreateAuthor(ctx, "Unknown Master")
29-
if err != nil {
30-
t.Fatal(err)
31-
}
32-
authorID, err := result.LastInsertId()
28+
a, err := dq.CreateAuthor(ctx, "Unknown Master")
3329
if err != nil {
3430
t.Fatal(err)
3531
}
@@ -45,76 +41,69 @@ func TestBooks(t *testing.T) {
4541
// save first book
4642
now := time.Now()
4743
_, err = tq.CreateBook(ctx, CreateBookParams{
48-
AuthorID: int64(authorID),
44+
AuthorID: a.AuthorID,
4945
Isbn: "1",
5046
Title: "my book title",
5147
BookType: BooksBookTypeFICTION,
5248
Yr: 2016,
5349
Available: now,
50+
Tag: "",
5451
})
5552
if err != nil {
5653
t.Fatal(err)
5754
}
5855

5956
// save second book
60-
result, err = tq.CreateBook(ctx, CreateBookParams{
61-
AuthorID: int64(authorID),
57+
b1, err := tq.CreateBook(ctx, CreateBookParams{
58+
AuthorID: a.AuthorID,
6259
Isbn: "2",
6360
Title: "the second book",
6461
BookType: BooksBookTypeFICTION,
6562
Yr: 2016,
6663
Available: now,
67-
Tags: "cool,unique",
64+
Tag: "unique",
6865
})
6966
if err != nil {
7067
t.Fatal(err)
7168
}
72-
bookOneID, err := result.LastInsertId()
73-
if err != nil {
74-
t.Fatal(err)
75-
}
7669

7770
// update the title and tags
7871
err = tq.UpdateBook(ctx, UpdateBookParams{
79-
BookID: int64(bookOneID),
72+
BookID: b1.BookID,
8073
Title: "changed second title",
81-
Tags: "cool,disastor",
74+
Tag: "disastor",
8275
})
8376
if err != nil {
8477
t.Fatal(err)
8578
}
8679

8780
// save third book
8881
_, err = tq.CreateBook(ctx, CreateBookParams{
89-
AuthorID: int64(authorID),
82+
AuthorID: a.AuthorID,
9083
Isbn: "3",
9184
Title: "the third book",
9285
BookType: BooksBookTypeFICTION,
9386
Yr: 2001,
9487
Available: now,
95-
Tags: "cool",
88+
Tag: "cool",
9689
})
9790
if err != nil {
9891
t.Fatal(err)
9992
}
10093

10194
// save fourth book
102-
result, err = tq.CreateBook(ctx, CreateBookParams{
103-
AuthorID: int64(authorID),
95+
b3, err := tq.CreateBook(ctx, CreateBookParams{
96+
AuthorID: a.AuthorID,
10497
Isbn: "4",
10598
Title: "4th place finisher",
10699
BookType: BooksBookTypeFICTION,
107100
Yr: 2011,
108101
Available: now,
109-
Tags: "other",
102+
Tag: "other",
110103
})
111104
if err != nil {
112105
t.Fatal(err)
113106
}
114-
bookThreeID, err := result.LastInsertId()
115-
if err != nil {
116-
t.Fatal(err)
117-
}
118107

119108
// tx commit
120109
err = tx.Commit()
@@ -124,10 +113,10 @@ func TestBooks(t *testing.T) {
124113

125114
// upsert, changing ISBN and title
126115
err = dq.UpdateBookISBN(ctx, UpdateBookISBNParams{
127-
BookID: int64(bookThreeID),
116+
BookID: b3.BookID,
128117
Isbn: "NEW ISBN",
129118
Title: "never ever gonna finish, a quatrain",
130-
Tags: "someother",
119+
Tag: "someother",
131120
})
132121
if err != nil {
133122
t.Fatal(err)
@@ -150,20 +139,20 @@ func TestBooks(t *testing.T) {
150139
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
151140
}
152141

153-
// find a book with either "cool" or "other" tag
142+
// find a book with either "cool" or "other" or "someother" tag
154143
t.Logf("---------\nTag search results:\n")
155-
res, err := dq.BooksByTags(ctx, "cool")
144+
res, err := dq.BooksByTags(ctx, []string{"cool", "other", "someother"})
156145
if err != nil {
157146
t.Fatal(err)
158147
}
159148
for _, ab := range res {
160-
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
149+
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tag: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tag)
161150
}
162151

163152
// TODO: call say_hello(varchar)
164153

165154
// get book 4 and delete
166-
b5, err := dq.GetBook(ctx, int64(bookThreeID))
155+
b5, err := dq.GetBook(ctx, b3.BookID)
167156
if err != nil {
168157
t.Fatal(err)
169158
}

examples/booktest/sqlite/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/booktest/sqlite/query.sql

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ SELECT
2020
title,
2121
name,
2222
isbn,
23-
tags
23+
tag
2424
FROM books
2525
LEFT JOIN authors ON books.author_id = authors.author_id
26-
WHERE tags = ?;
26+
WHERE tag IN (sqlc.slice(tags));
2727

28-
/* name: CreateAuthor :execresult */
29-
INSERT INTO authors (name) VALUES (?);
28+
/* name: CreateAuthor :one */
29+
INSERT INTO authors (name) VALUES (?)
30+
RETURNING *;
3031

31-
/* name: CreateBook :execresult */
32+
/* name: CreateBook :one */
3233
INSERT INTO books (
3334
author_id,
3435
isbn,
3536
book_type,
3637
title,
3738
yr,
3839
available,
39-
tags
40+
tag
4041
) VALUES (
4142
?,
4243
?,
@@ -45,16 +46,17 @@ INSERT INTO books (
4546
?,
4647
?,
4748
?
48-
);
49+
)
50+
RETURNING *;
4951

5052
/* name: UpdateBook :exec */
5153
UPDATE books
52-
SET title = ?, tags = ?
54+
SET title = ?, tag = ?
5355
WHERE book_id = ?;
5456

5557
/* name: UpdateBookISBN :exec */
5658
UPDATE books
57-
SET title = ?, tags = ?, isbn = ?
59+
SET title = ?, tag = ?, isbn = ?
5860
WHERE book_id = ?;
5961

6062
/* name: DeleteAuthorBeforeYear :exec */

0 commit comments

Comments
 (0)