diff --git a/examples/booktest/sqlite/db_test.go b/examples/booktest/sqlite/db_test.go index 42a8b396b1..cb2e12aeca 100644 --- a/examples/booktest/sqlite/db_test.go +++ b/examples/booktest/sqlite/db_test.go @@ -160,4 +160,17 @@ func TestBooks(t *testing.T) { t.Fatal(err) } + // update book with isbn "1" to year 2024 + _, err = dq.SaveBook(ctx, SaveBookParams{ + AuthorID: a.AuthorID, + Isbn: "1", + Title: "my book title", + BookType: BooksBookTypeFICTION, + Yr: 2024, + Available: now, + Tag: "", + }) + if err != nil { + t.Fatal(err) + } } diff --git a/examples/booktest/sqlite/query.sql b/examples/booktest/sqlite/query.sql index 4022c7ea53..1b04e5300b 100644 --- a/examples/booktest/sqlite/query.sql +++ b/examples/booktest/sqlite/query.sql @@ -62,3 +62,25 @@ WHERE book_id = ?3; /* name: DeleteAuthorBeforeYear :exec */ DELETE FROM books WHERE yr < ? AND author_id = ?; + +/* name: SaveBook :one */ +INSERT INTO books ( + author_id, + isbn, + book_type, + title, + yr, + available, + tag +) VALUES ( + ?, + ?, + ?, + ?, + ?, + ?, + ? +) +ON CONFLICT (isbn) +DO UPDATE SET author_id = ?, book_type = ?, title = ?, yr = ?, available = ?, tag = ? +RETURNING *; diff --git a/examples/booktest/sqlite/query.sql.go b/examples/booktest/sqlite/query.sql.go index a346f14b99..17bd6b2755 100644 --- a/examples/booktest/sqlite/query.sql.go +++ b/examples/booktest/sqlite/query.sql.go @@ -238,6 +238,63 @@ func (q *Queries) GetBook(ctx context.Context, bookID int64) (Book, error) { return i, err } +const saveBook = `-- name: SaveBook :one +INSERT INTO books ( + author_id, + isbn, + book_type, + title, + yr, + available, + tag +) VALUES ( + ?, + ?, + ?, + ?, + ?, + ?, + ? +) +ON CONFLICT (isbn) +DO UPDATE SET author_id = ?, book_type = ?, title = ?, yr = ?, available = ?, tag = ? +RETURNING book_id, author_id, isbn, book_type, title, yr, available, tag +` + +type SaveBookParams struct { + AuthorID int64 + Isbn string + BookType string + Title string + Yr int64 + Available time.Time + Tag string +} + +func (q *Queries) SaveBook(ctx context.Context, arg SaveBookParams) (Book, error) { + row := q.db.QueryRowContext(ctx, saveBook, + arg.AuthorID, + arg.Isbn, + arg.BookType, + arg.Title, + arg.Yr, + arg.Available, + arg.Tag, + ) + var i Book + err := row.Scan( + &i.BookID, + &i.AuthorID, + &i.Isbn, + &i.BookType, + &i.Title, + &i.Yr, + &i.Available, + &i.Tag, + ) + return i, err +} + const updateBook = `-- name: UpdateBook :exec UPDATE books SET title = ?1, tag = ?2