-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rows.go
76 lines (63 loc) · 1.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package elasticsearch
import (
"database/sql/driver"
"fmt"
"io"
)
// Rows is an iterator over an executed query's results.
type Rows struct {
dsn string
columns []string
types []esType
rows [][]driver.Value
cursor string
}
// Columns returns the names of the columns.
func (r *Rows) Columns() []string {
if debug {
fmt.Println("Rows Columns")
}
return r.columns
}
// Close closes the rows iterator.
func (r *Rows) Close() error {
if debug {
fmt.Println("Rows Close")
}
r.columns = nil
r.types = nil
r.rows = nil
r.cursor = ""
if r.dsn == "" {
return ErrClosedConnection
}
r.dsn = ""
return nil
}
// Next is called to populate the next row of data into the provided slice.
func (r *Rows) Next(dest []driver.Value) error {
if debug {
fmt.Println("Rows Next:", dest, r.rows)
}
if dest == nil {
return ErrInvalidArgs
} else if r.dsn == "" {
return ErrClosedConnection
} else if r.rows == nil {
return io.EOF
} else if len(r.rows) <= 0 {
if r.cursor == "" {
return io.EOF
}
rows, cursor, err := nextRows(r.dsn, r.cursor)
if err != nil {
return err
} else if len(rows) <= 0 {
return io.EOF
}
r.rows, r.cursor = rows, cursor
}
copy(dest, r.rows[0])
r.rows = r.rows[1:]
return nil
}