-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows_bytes.go
141 lines (123 loc) · 2.93 KB
/
rows_bytes.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package rows
import (
"reflect"
)
// DataScanBytes fill in key and data to v
func DataScanBytes(key []string, data [][][]byte, v interface{}, fn func(reflect.StructField) string) (int, error) {
if len(data) == 0 || len(key) == 0 {
return 0, nil
}
val := reflect.ValueOf(v)
return rowsScanValuesBytes(key, data, val, fn)
}
// RowsLimitBytes
// if limit >= 0 Read maximum rows limit
// else < 0 Not limited
func RowsLimitBytes(rows Rows, limit int) ([]string, [][][]byte, error) {
ms := 1024
if limit > 0 {
ms = limit
}
data := make([][][]byte, 0, ms)
key, err := rowsLimit(rows, limit, false, func(d [][]byte) {
if d != nil {
data = append(data, d)
}
})
rows.Close()
if err != nil {
return nil, nil, err
}
return key, data, nil
}
// RowsScanBytes fill in rows to v
func RowsScanBytes(rows Rows, v interface{}, limit int,
fn func(reflect.StructField) string) (int, error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return 0, ErrNotPointer
}
val = val.Elem()
for val.Kind() == reflect.Ptr {
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
val = val.Elem()
}
l := 0
switch val.Kind() {
case reflect.Array:
l = val.Len()
case reflect.Slice:
l = -1
default:
l = 1
}
l = getLimit(l, limit)
return rowsScanBytes(rows, v, l, fn)
}
// rowsScanBytes fill in rows to v
func rowsScanBytes(rows Rows, v interface{}, limit int,
fn func(reflect.StructField) string) (int, error) {
key, data, err := RowsLimitBytes(rows, limit)
if err != nil {
return 0, err
}
return DataScanBytes(key, data, v, fn)
}
// rowsScanValue rows scan value
func rowsScanValueBytes(key []string, data [][][]byte, val reflect.Value, fn func(reflect.StructField) string) (int, error) {
tt := val.Type().Elem()
ps := 0
for tt.Kind() == reflect.Ptr {
tt = tt.Elem()
ps++
}
rs, err := rowsScanValueFunc(tt, key, fn)
if err != nil {
return 0, err
}
if val.Len() == 0 {
if val.Kind() == reflect.Slice {
val.Set(reflect.MakeSlice(val.Type(), len(data), len(data)))
} else {
return 0, nil
}
}
vl := val.Len()
if vl < len(data) {
data = data[:vl]
}
for k, v := range data {
d := reflect.New(tt).Elem()
if err := rs(key, v, d); err != nil {
return 0, err
}
for i := 0; i != ps; i++ {
d = d.Addr()
}
val.Index(k).Set(d)
}
return vl, nil
}
// rowsScanValuesBytes rows scan values
func rowsScanValuesBytes(key []string, data [][][]byte, val reflect.Value, fn func(reflect.StructField) string) (int, error) {
switch val.Kind() {
default:
return 0, ErrInvalidType
case reflect.Ptr:
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
return rowsScanValuesBytes(key, data, val.Elem(), fn)
case reflect.Slice:
fallthrough
case reflect.Array:
return rowsScanValueBytes(key, data, val, fn)
case reflect.Struct:
key0 := colAdjust(val.Type(), key, fn)
return 1, rowScanStruct(key0, data[0], val)
case reflect.Map:
return 1, rowScanMap(key, data[0], val)
}
}