forked from acoshift/pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
40 lines (32 loc) · 856 Bytes
/
iter.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
package pgsql
import (
"context"
"database/sql"
)
type Scanner func(dest ...any) error
type Iterator func(scan Scanner) error
// QueryContext interface
type QueryContext interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}
func Iter(q interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}, iter Iterator, query string, args ...any) error {
return IterContext(context.Background(), q, iter, query, args...)
}
func IterContext(ctx context.Context, q interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}, iter Iterator, query string, args ...any) error {
rows, err := q.QueryContext(ctx, query, args...)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
err := iter(Scan(rows.Scan))
if err != nil {
return err
}
}
return rows.Err()
}