Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sqlite) add support sqlc functions #2274

Merged
merged 7 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/howto/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ func (q *Queries) ListAuthorsByIDs(ctx context.Context, ids []int) ([]Author, er
}
```

### MySQL
### MySQL and SQLite

MySQL differs from PostgreSQL in that placeholders must be generated based on
MySQL and SQLite differ from PostgreSQL in that placeholders must be generated based on
the number of elements in the slice you pass in. Though trivial it is still
something of a nuisance. The passed in slice must not be nil or empty or an
error will be returned (ie not a panic). The placeholder insertion location is
Expand Down
49 changes: 19 additions & 30 deletions examples/booktest/sqlite/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ func TestBooks(t *testing.T) {
dq := New(db)

// create an author
result, err := dq.CreateAuthor(ctx, "Unknown Master")
if err != nil {
t.Fatal(err)
}
authorID, err := result.LastInsertId()
a, err := dq.CreateAuthor(ctx, "Unknown Master")
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,76 +41,69 @@ func TestBooks(t *testing.T) {
// save first book
now := time.Now()
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int64(authorID),
AuthorID: a.AuthorID,
Isbn: "1",
Title: "my book title",
BookType: BooksBookTypeFICTION,
Yr: 2016,
Available: now,
Tag: "",
})
if err != nil {
t.Fatal(err)
}

// save second book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int64(authorID),
b1, err := tq.CreateBook(ctx, CreateBookParams{
AuthorID: a.AuthorID,
Isbn: "2",
Title: "the second book",
BookType: BooksBookTypeFICTION,
Yr: 2016,
Available: now,
Tags: "cool,unique",
Tag: "unique",
})
if err != nil {
t.Fatal(err)
}
bookOneID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}

// update the title and tags
err = tq.UpdateBook(ctx, UpdateBookParams{
BookID: int64(bookOneID),
BookID: b1.BookID,
Title: "changed second title",
Tags: "cool,disastor",
Tag: "disastor",
})
if err != nil {
t.Fatal(err)
}

// save third book
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int64(authorID),
AuthorID: a.AuthorID,
Isbn: "3",
Title: "the third book",
BookType: BooksBookTypeFICTION,
Yr: 2001,
Available: now,
Tags: "cool",
Tag: "cool",
})
if err != nil {
t.Fatal(err)
}

// save fourth book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int64(authorID),
b3, err := tq.CreateBook(ctx, CreateBookParams{
AuthorID: a.AuthorID,
Isbn: "4",
Title: "4th place finisher",
BookType: BooksBookTypeFICTION,
Yr: 2011,
Available: now,
Tags: "other",
Tag: "other",
})
if err != nil {
t.Fatal(err)
}
bookThreeID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}

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

// upsert, changing ISBN and title
err = dq.UpdateBookISBN(ctx, UpdateBookISBNParams{
BookID: int64(bookThreeID),
BookID: b3.BookID,
Isbn: "NEW ISBN",
Title: "never ever gonna finish, a quatrain",
Tags: "someother",
Tag: "someother",
})
if err != nil {
t.Fatal(err)
Expand All @@ -150,20 +139,20 @@ func TestBooks(t *testing.T) {
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}

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

// TODO: call say_hello(varchar)

// get book 4 and delete
b5, err := dq.GetBook(ctx, int64(bookThreeID))
b5, err := dq.GetBook(ctx, b3.BookID)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/booktest/sqlite/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions examples/booktest/sqlite/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ SELECT
title,
name,
isbn,
tags
tag
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE tags = ?;
WHERE tag IN (sqlc.slice(tags));

/* name: CreateAuthor :execresult */
INSERT INTO authors (name) VALUES (?);
/* name: CreateAuthor :one */
INSERT INTO authors (name) VALUES (?)
RETURNING *;

/* name: CreateBook :execresult */
/* name: CreateBook :one */
INSERT INTO books (
author_id,
isbn,
book_type,
title,
yr,
available,
tags
tag
) VALUES (
?,
?,
Expand All @@ -45,17 +46,18 @@ INSERT INTO books (
?,
?,
?
);
)
RETURNING *;

/* name: UpdateBook :exec */
UPDATE books
SET title = ?, tags = ?
WHERE book_id = ?;
SET title = ?1, tag = ?2
WHERE book_id = ?3;

/* name: UpdateBookISBN :exec */
UPDATE books
SET title = ?, tags = ?, isbn = ?
WHERE book_id = ?;
SET title = ?1, tag = ?2, isbn = ?4
WHERE book_id = ?3;

/* name: DeleteAuthorBeforeYear :exec */
DELETE FROM books
Expand Down
Loading