-
Notifications
You must be signed in to change notification settings - Fork 3
/
rows.go
167 lines (134 loc) · 4.52 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package db
import (
"fmt"
"reflect"
"strconv"
"strings"
)
type Row interface {
Scan(dest ...any) error
}
// Rows is a struct for storing DB rows (as a slice of Row) and current index
type Rows interface {
Next() bool
Err() error
Scan(dest ...interface{}) error
Close() error
Dump() string
}
// EmptyRows is a struct for storing DB rows (as a slice of dbRow) and current index
type EmptyRows struct{}
func (r *EmptyRows) Next() bool { return false }
func (r *EmptyRows) Err() error { return nil }
func (r *EmptyRows) Scan(dest ...interface{}) error { return nil }
func (r *EmptyRows) Close() error { return nil }
func (r *EmptyRows) Dump() string { return "" }
type surrogateRowsRow []interface{}
// SurrogateRows is a struct for storing DB rows (as a slice of dbRow) and current index
type SurrogateRows struct {
data []surrogateRowsRow
idx int
}
// Next implements sql.Rows interface for DBRows struct (used in tests)
func (r *SurrogateRows) Next() bool {
if r.idx < len(r.data) {
r.idx++
return true
}
return false
}
func (r *SurrogateRows) Err() error {
return nil
}
// Scan implements sql.Rows interface for DBRows struct (used in tests)
// and returns error if conversion is not implemented
func (r *SurrogateRows) Scan(dest ...interface{}) error {
row := r.data[r.idx-1]
for i := range row {
dv := reflect.ValueOf(dest[i])
if dv.Kind() != reflect.Ptr {
return fmt.Errorf("internal error: DBRows.Scan() - non-pointer passed to Scan: %v", dest)
}
sv := reflect.ValueOf(row[i])
if !sv.IsValid() {
switch dv.Elem().Kind() {
case reflect.String:
dv.Elem().SetString("")
continue
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dv.Elem().SetInt(0)
continue
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dv.Elem().SetUint(0)
continue
case reflect.Ptr, reflect.Interface:
dv.Elem().Set(reflect.Zero(dv.Elem().Type()))
continue
default:
return fmt.Errorf("unsupported type: %v", dv.Elem().Kind())
}
} else if sv.Kind() == reflect.Slice && sv.Type().Elem().Kind() == reflect.Uint8 {
s := string(sv.Interface().([]uint8))
switch dv.Elem().Kind() {
case reflect.String:
dv.Elem().SetString(s)
continue
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i64, err := strconv.ParseInt(s, 10, dv.Elem().Type().Bits())
if err != nil {
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", sv, s, dv.Kind(), err)
}
dv.Elem().SetInt(i64)
continue
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u64, err := strconv.ParseUint(s, 10, dv.Elem().Type().Bits())
if err != nil {
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", sv, s, dv.Kind(), err)
}
dv.Elem().SetUint(u64)
continue
case reflect.Interface:
dv.Elem().Set(sv)
continue
default:
return fmt.Errorf("unsupported type: %v", dv.Elem().Kind())
}
} else if sv.Kind() == reflect.String && dv.Elem().Kind() == reflect.String {
dv.Elem().SetString(sv.Interface().(string))
continue
} else if sv.Kind() == reflect.Int64 && dv.Elem().Kind() == reflect.Int {
dv.Elem().SetInt(sv.Int())
continue
} else if sv.Kind() == reflect.Int8 && dv.Elem().Kind() == reflect.Int {
dv.Elem().SetInt(sv.Int())
continue
// } else if sv.Kind() == reflect.TypeOf(TenantUUID("")).Kind() && dv.Elem().Kind() == reflect.String {
// dv.Elem().SetString(sv.Interface().(string))
// continue
} else if sv.Type().AssignableTo(dv.Elem().Type()) {
dv.Elem().Set(sv)
continue
}
PrintStack()
// fmt.Printf("xxx internal error: DBRows.Scan() - convertion of '%v' (%v) to '%v' is not implemented yet\n", sv.Kind(), reflect.TypeOf(TenantUUID("")).Kind(), dv.Elem().Kind())
return fmt.Errorf("internal error: DBRows.Scan() - convertion of '%v' to '%v' is not implemented yet", sv.Kind(), dv.Elem().Kind())
}
return nil
}
// Close implements sql.Rows interface for DBRows struct (used in tests)
func (r *SurrogateRows) Close() error {
return nil
}
// Dump returns string representation of DBRows struct (used in tests)
func (r *SurrogateRows) Dump() string {
ret := make([]string, 0, len(r.data))
for n, row := range r.data {
if n > 10 {
// do not flood the logs
ret = append(ret, "... truncated ...")
break
}
ret = append(ret, DumpRecursive(row, " "))
}
return "[" + strings.Join(ret, ", ") + "]"
}