-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
83 lines (75 loc) · 1.43 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
77
78
79
80
81
82
83
package gosqlcache
import (
"bytes"
"database/sql/driver"
"encoding/gob"
"io"
)
// Implements driver.Rows
type cachedRows struct {
cols []string
pointer int
data [][]driver.Value
closed bool
}
func newCachedRows(dr driver.Rows) (r *cachedRows, err error) {
r = &cachedRows{}
r.cols = dr.Columns()
for {
var cols = make([]driver.Value, len(r.cols))
err = dr.Next(cols)
if err != nil {
if err == io.EOF {
err = nil
}
return
}
r.data = append(r.data, cols)
}
}
func (r *cachedRows) Columns() []string {
return r.cols
}
func (r *cachedRows) Close() error {
r.data = nil
r.closed = true
return nil
}
func (r *cachedRows) Next(dest []driver.Value) error {
if r.closed {
return driver.ErrBadConn
}
if len(r.data) <= r.pointer {
return io.EOF
}
for i, v := range r.data[r.pointer] {
dest[i] = v
}
r.pointer += 1
return nil
}
func (r *cachedRows) GobEncode() ([]byte, error) {
var buf bytes.Buffer
var enc = gob.NewEncoder(&buf)
err := enc.Encode(map[string]interface{}{
"columns": r.cols,
"data": r.data,
})
return buf.Bytes(), err
}
func (r *cachedRows) GobDecode(b []byte) (err error) {
var buf = bytes.NewBuffer(b)
var dec = gob.NewDecoder(buf)
var m = map[string]interface{}{}
err = dec.Decode(&m)
var ok bool
r.cols, ok = m["columns"].([]string)
if !ok {
r.cols = []string{}
}
r.data, ok = m["data"].([][]driver.Value)
if !ok {
r.data = [][]driver.Value{}
}
return nil
}