-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
54 lines (48 loc) · 1.09 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package pgtalk
import (
"github.com/jackc/pgx/v5"
)
type resultIterator[T any] struct {
queryError error
rows pgx.Rows
selectors []ColumnAccessor
}
// Close closes the rows, making the connection ready for use again. It is safe
// to call Close after rows is already closed.
func (i *resultIterator[T]) Close() {
i.rows.Close()
}
func (i *resultIterator[T]) Err() error {
if i.queryError != nil {
return i.queryError
}
return i.rows.Err()
}
func (i *resultIterator[T]) HasNext() bool {
if i.queryError != nil {
return false
}
if i.rows.Next() {
return true
}
// if Next returns false we can close the rows
i.rows.Close()
return false
}
func (i *resultIterator[T]) Next() (*T, error) {
entity := new(T)
list := i.rows.FieldDescriptions()
// order of list is not the same as selectors?
toScan := []any{}
for _, each := range list {
for _, other := range i.selectors {
if other.Column().columnName == each.Name {
toScan = append(toScan, other.FieldValueToScan(entity))
}
}
}
if err := i.rows.Scan(toScan...); err != nil {
return nil, err
}
return entity, nil
}