-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
75 lines (65 loc) · 1.68 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
package corroclient
import (
"errors"
"fmt"
"sync"
)
var ErrScan = errors.New("corroclient: scan error")
// Warning: Scan does not handle time.Time because of the various ways time can be stored
// in SQLite and JSON. You're responsible for converting time.Time yourself from numbers types or
// strings.
func (r *Row) Scan(dest ...any) error {
for i, value := range r.values {
if value == nil {
continue
}
switch v := value.(type) {
case float64:
if err := scanJSONNumber(v, dest[i]); err != nil {
return fmt.Errorf("%w, failed to scan JSON float64 %s", err, value)
}
continue
case string:
if err := scanJSONString(v, dest[i]); err != nil {
return fmt.Errorf("%w, failed to scan JSON string %s", err, value)
}
continue
case bool:
if err := scanJSONBool(v, dest[i]); err != nil {
return fmt.Errorf("%w, failed to scan JSON bool %s", err, value)
}
continue
}
}
return nil
}
type Rows struct {
columns []string
rows []*Row
currentIndex int
mutex sync.RWMutex
}
func (r *Rows) Columns() []string {
return r.columns
}
func (r *Rows) Next() bool {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.currentIndex == len(r.rows)-1 {
return false
}
r.currentIndex++
return true
}
// Warning: Scan does not handle time.Time because of the various ways time can be stored
// in SQLite and JSON. You're responsible for converting time.Time yourself from numbers types or
// strings.
func (r *Rows) Scan(dest ...any) error {
r.mutex.RLock()
defer r.mutex.RUnlock()
if r.currentIndex == -1 {
return fmt.Errorf("you must call Next at least once before calling Scan")
}
row := r.rows[r.currentIndex]
return row.Scan(dest...)
}