-
Notifications
You must be signed in to change notification settings - Fork 17
/
rows.go
59 lines (52 loc) · 1.31 KB
/
rows.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
package ctlstore
import (
"database/sql"
"github.com/segmentio/ctlstore/pkg/scanfunc"
"github.com/segmentio/ctlstore/pkg/schema"
)
// Rows composes an *sql.Rows and allows scanning ctlstore table rows into
// structs or maps, similar to how the GetRowByKey reader method works.
//
// The contract around Next/Err/Close is the same was it is for
// *sql.Rows.
type Rows struct {
rows *sql.Rows
cols []schema.DBColumnMeta
}
// Next returns true if there's another row available.
func (r *Rows) Next() bool {
if r.rows == nil {
return false
}
return r.rows.Next()
}
// Err returns any error that could have been caused during
// the invocation of Next(). If Next() returns false, the caller
// must always check Err() to see if that's why iteration
// failed.
func (r *Rows) Err() error {
if r.rows == nil {
return nil
}
return r.rows.Err()
}
// Close closes the underlying *sql.Rows.
func (r *Rows) Close() error {
if r.rows == nil {
return nil
}
return r.rows.Close()
}
// Scan deserializes the current row into the specified target.
// The target must be either a pointer to a struct, or a
// map[string]interface{}.
func (r *Rows) Scan(target interface{}) error {
if r.rows == nil {
return sql.ErrNoRows
}
scanFunc, err := scanfunc.New(target, r.cols)
if err != nil {
return err
}
return scanFunc(r.rows)
}