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

Ability to return an iterator on rows #3631

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.3'
go-version: '1.23.2'
- name: install ./...
run: go install ./...
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.3'
go-version: '1.23.2'
- name: install ./...
run: go install ./...
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.3'
go-version: '1.23.2'
- name: install ./...
run: go install ./...
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22.8'
go-version: '1.23.2'

- name: install gotestsum
run: go install gotest.tools/gotestsum@latest
Expand Down Expand Up @@ -78,6 +78,6 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22.8'
go-version: '1.23.2'
- run: go install golang.org/x/vuln/cmd/govulncheck@latest
- run: govulncheck ./...
2 changes: 1 addition & 1 deletion docs/overview/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sudo snap install sqlc

## go install

Installing recent versions of sqlc requires Go 1.21+.
Installing recent versions of sqlc requires Go 1.23+.

```
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
Expand Down
4 changes: 4 additions & 0 deletions examples/authors/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ WHERE id = ? LIMIT 1;
SELECT * FROM authors
ORDER BY name;

/* name: IterAuthors :iter */
SELECT * FROM authors
ORDER BY name;

/* name: CreateAuthor :execresult */
INSERT INTO authors (
name, bio
Expand Down
54 changes: 54 additions & 0 deletions examples/authors/mysql/query.sql.go

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

4 changes: 4 additions & 0 deletions examples/authors/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ WHERE id = $1 LIMIT 1;
SELECT * FROM authors
ORDER BY name;

-- name: IterAuthors :iter
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
Expand Down
53 changes: 53 additions & 0 deletions examples/authors/postgresql/query.sql.go

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

4 changes: 4 additions & 0 deletions examples/authors/sqlite/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ WHERE id = ? LIMIT 1;
SELECT * FROM authors
ORDER BY name;

/* name: IterAuthors :iter */
SELECT * FROM authors
ORDER BY name;

/* name: CreateAuthor :execresult */
INSERT INTO authors (
name, bio
Expand Down
54 changes: 54 additions & 0 deletions examples/authors/sqlite/query.sql.go

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

20 changes: 20 additions & 0 deletions examples/booktest/mysql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package booktest
import (
"context"
"database/sql"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -150,6 +151,25 @@ func TestBooks(t *testing.T) {
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}

// retrieve first book
rows := dq.IterBooksByTitleYear(ctx, IterBooksByTitleYearParams{
Title: "my book title",
Yr: 2016,
})

// We need to collect the iterator elements to avoid panic when we make other db queries in the loop.
for _, book := range slices.Collect(rows.Iterate()) {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
}
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}
if err = rows.Err(); err != nil {
t.Fatal(err)
}

// find a book with either "cool" or "other" tag
t.Logf("---------\nTag search results:\n")
res, err := dq.BooksByTags(ctx, "cool")
Expand Down
4 changes: 4 additions & 0 deletions examples/booktest/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ WHERE book_id = ?;
SELECT * FROM books
WHERE title = ? AND yr = ?;

/* name: IterBooksByTitleYear :iter */
SELECT * FROM books
WHERE title = ? AND yr = ?;

/* name: BooksByTags :many */
SELECT
book_id,
Expand Down
Loading