-
Notifications
You must be signed in to change notification settings - Fork 0
/
for.go
50 lines (41 loc) · 838 Bytes
/
for.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
package sqlz
type Rows interface {
Next() bool
Scan(...interface{}) error
Err() error
Close() error
}
type forFields []interface{}
type inRows struct {
forFields
rows Rows
err error
}
func For(fields ...interface{}) forFields {
return fields
}
func (f forFields) In(rows Rows, err error) inRows {
return inRows{f, rows, err}
}
func (r inRows) EachRow(f func() error) error {
if r.err != nil {
return r.err
}
return eachRow(r.rows, r.forFields, f)
}
func eachRow(rows Rows, fields []interface{}, f func() error) error {
defer rows.Close()
for rows.Next() {
if len(fields) > 0 {
if err := rows.Scan(fields...); err != nil {
return err
}
}
if err := f(); err != nil {
return err
}
}
return rows.Err()
}
func (r inRows) Scan() error { return r.EachRow(noop) }
func noop() error { return nil }