Skip to content

Commit

Permalink
feat(RowIterator): add Count method
Browse files Browse the repository at this point in the history
Useful when implementing Pagination with Total count.
  • Loading branch information
ngalaiko committed Aug 20, 2024
1 parent d14e895 commit 556d550
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/codegen/databasecodegen/rowiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
f.P("Next() (*", row.Type(), ", error)")
f.P("Do(f func(row *", row.Type(), ") error) error")
f.P("Stop()")
f.P("Count() int64")
f.P("}")
f.P()
f.P("type ", g.StreamingType(), " struct {")
Expand Down Expand Up @@ -59,6 +60,10 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
f.P("})")
f.P("}")
f.P()
f.P("func (i *", g.StreamingType(), ") Count() int64 {")
f.P("return i.RowCount")
f.P("}")
f.P()
f.P("type ", g.BufferedType(), " struct {")
f.P("rows []*", row.Type())
f.P("err error")
Expand All @@ -76,6 +81,10 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
f.P("return next, nil")
f.P("}")
f.P()
f.P("func (i *", g.BufferedType(), ") Count() int64 {")
f.P("return int64(len(i.rows))")
f.P("}")
f.P()
f.P("func (i *", g.BufferedType(), ") Do(f func(row *", row.Type(), ") error) error {")
f.P("for {")
f.P("row, err := i.Next()")
Expand Down
9 changes: 9 additions & 0 deletions internal/codegen/databasecodegen/testdata/1.sql.database.go

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

18 changes: 18 additions & 0 deletions internal/codegen/databasecodegen/testdata/2.sql.database.go

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

27 changes: 27 additions & 0 deletions internal/codegen/databasecodegen/testdata/3.sql.database.go

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

36 changes: 36 additions & 0 deletions internal/codegen/databasecodegen/testdata/4.sql.database.go

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

9 changes: 9 additions & 0 deletions internal/codegen/databasecodegen/testdata/5.sql.database.go

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

Loading

0 comments on commit 556d550

Please sign in to comment.