Skip to content

Commit 556d550

Browse files
committed
feat(RowIterator): add Count method
Useful when implementing Pagination with Total count.
1 parent d14e895 commit 556d550

File tree

10 files changed

+207
-0
lines changed

10 files changed

+207
-0
lines changed

internal/codegen/databasecodegen/rowiterator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
3131
f.P("Next() (*", row.Type(), ", error)")
3232
f.P("Do(f func(row *", row.Type(), ") error) error")
3333
f.P("Stop()")
34+
f.P("Count() int64")
3435
f.P("}")
3536
f.P()
3637
f.P("type ", g.StreamingType(), " struct {")
@@ -59,6 +60,10 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
5960
f.P("})")
6061
f.P("}")
6162
f.P()
63+
f.P("func (i *", g.StreamingType(), ") Count() int64 {")
64+
f.P("return i.RowCount")
65+
f.P("}")
66+
f.P()
6267
f.P("type ", g.BufferedType(), " struct {")
6368
f.P("rows []*", row.Type())
6469
f.P("err error")
@@ -76,6 +81,10 @@ func (g RowIteratorCodeGenerator) GenerateCode(f *codegen.File) {
7681
f.P("return next, nil")
7782
f.P("}")
7883
f.P()
84+
f.P("func (i *", g.BufferedType(), ") Count() int64 {")
85+
f.P("return int64(len(i.rows))")
86+
f.P("}")
87+
f.P()
7988
f.P("func (i *", g.BufferedType(), ") Do(f func(row *", row.Type(), ") error) error {")
8089
f.P("for {")
8190
f.P("row, err := i.Next()")

internal/codegen/databasecodegen/testdata/1.sql.database.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/codegen/databasecodegen/testdata/2.sql.database.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/codegen/databasecodegen/testdata/3.sql.database.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/codegen/databasecodegen/testdata/4.sql.database.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/codegen/databasecodegen/testdata/5.sql.database.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)