-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathiterator.go
More file actions
66 lines (55 loc) · 1.74 KB
/
iterator.go
File metadata and controls
66 lines (55 loc) · 1.74 KB
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
package godb
import "database/sql"
// Iterator is an interface to iterate over the result of a sql query
// and scan each row one at a time instead of getting all into one slice.
// The principe is similar to the standard sql.Rows type.
type Iterator interface {
Next() bool
Scan(interface{}) error
Scanx(...interface{}) error
Close() error
Err() error
}
// iteratorInternals is the Iterator implementation (hidden)
type iteratorInternals struct {
rows *sql.Rows
recordInfo *recordDescription
columns []string
}
// Next prepares the next result row for reading with the Scan method.
// It returns false is case of error or if there are not more data to fetch.
// If the end of the resultset is reached, it automaticaly free ressources like
// the Close method.
func (i *iteratorInternals) Next() bool {
return i.rows.Next()
}
// Scan fill the given struct with the current row.
func (i *iteratorInternals) Scan(record interface{}) error {
var err error
// First scan
if i.recordInfo == nil {
// Reflection part
i.recordInfo, err = buildRecordDescription(record)
if err != nil {
return err
}
}
pointers, err := i.recordInfo.structMapping.GetPointersForColumns(record, i.columns...)
if err != nil {
return err
}
return i.rows.Scan(pointers...)
}
//Scanx scans record values to destination columns
func (i *iteratorInternals) Scanx(dest ...interface{}) error {
return i.rows.Scan(dest...)
}
// Close frees ressources created by the request execution.
func (i *iteratorInternals) Close() error {
return i.rows.Close()
}
// Err returns the error that was encountered during iteration, or nil.
// Always check Err after an iteration, like with the standard sql.Err method.
func (i *iteratorInternals) Err() error {
return i.rows.Err()
}