-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
81 lines (73 loc) · 1.84 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package pgtalk
import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type resultIterator[T any] struct {
queryError error
commandTag pgconn.CommandTag
rows pgx.Rows
selectors []ColumnAccessor
params []any
}
// 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
}
// happens when query is executed with Exec
if i.rows == nil {
return nil
}
return i.rows.Err()
}
// CommandTag is valid if the query is an Exec query, i.e. not returning rows.
func (i *resultIterator[T]) CommandTag() pgconn.CommandTag {
return i.commandTag
}
// HasNext returns true if there are more rows to scan.
// If none are left, it closes the rows.
func (i *resultIterator[T]) HasNext() bool {
if i.queryError != nil {
return false
}
// happens when query is executed with Exec
if i.rows == 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
}
// GetParams returns all the parameters used in the query. Can be used for debugging or logging
func (i *resultIterator[T]) GetParams() map[int]any {
ret := make(map[int]any, len(i.params))
for i, each := range i.params {
ret[i+1] = each
}
return ret
}