1
2
3
4
5 package driver
6
7 import (
8 "fmt"
9 "reflect"
10 "strconv"
11 "time"
12 )
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 type ValueConverter interface {
31
32 ConvertValue(v interface{}) (Value, error)
33 }
34
35
36
37
38
39 type Valuer interface {
40
41 Value() (Value, error)
42 }
43
44
45
46
47
48
49
50
51
52
53
54 var Bool boolType
55
56 type boolType struct{}
57
58 var _ ValueConverter = boolType{}
59
60 func (boolType) String() string { return "Bool" }
61
62 func (boolType) ConvertValue(src interface{}) (Value, error) {
63 switch s := src.(type) {
64 case bool:
65 return s, nil
66 case string:
67 b, err := strconv.ParseBool(s)
68 if err != nil {
69 return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
70 }
71 return b, nil
72 case []byte:
73 b, err := strconv.ParseBool(string(s))
74 if err != nil {
75 return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
76 }
77 return b, nil
78 }
79
80 sv := reflect.ValueOf(src)
81 switch sv.Kind() {
82 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
83 iv := sv.Int()
84 if iv == 1 || iv == 0 {
85 return iv == 1, nil
86 }
87 return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv)
88 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
89 uv := sv.Uint()
90 if uv == 1 || uv == 0 {
91 return uv == 1, nil
92 }
93 return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv)
94 }
95
96 return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src)
97 }
98
99
100
101 var Int32 int32Type
102
103 type int32Type struct{}
104
105 var _ ValueConverter = int32Type{}
106
107 func (int32Type) ConvertValue(v interface{}) (Value, error) {
108 rv := reflect.ValueOf(v)
109 switch rv.Kind() {
110 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
111 i64 := rv.Int()
112 if i64 > (1<<31)-1 || i64 < -(1<<31) {
113 return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
114 }
115 return i64, nil
116 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
117 u64 := rv.Uint()
118 if u64 > (1<<31)-1 {
119 return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
120 }
121 return int64(u64), nil
122 case reflect.String:
123 i, err := strconv.Atoi(rv.String())
124 if err != nil {
125 return nil, fmt.Errorf("sql/driver: value %q can't be converted to int32", v)
126 }
127 return int64(i), nil
128 }
129 return nil, fmt.Errorf("sql/driver: unsupported value %v (type %T) converting to int32", v, v)
130 }
131
132
133
134
135
136 var String stringType
137
138 type stringType struct{}
139
140 func (stringType) ConvertValue(v interface{}) (Value, error) {
141 switch v.(type) {
142 case string, []byte:
143 return v, nil
144 }
145 return fmt.Sprintf("%v", v), nil
146 }
147
148
149
150 type Null struct {
151 Converter ValueConverter
152 }
153
154 func (n Null) ConvertValue(v interface{}) (Value, error) {
155 if v == nil {
156 return nil, nil
157 }
158 return n.Converter.ConvertValue(v)
159 }
160
161
162
163 type NotNull struct {
164 Converter ValueConverter
165 }
166
167 func (n NotNull) ConvertValue(v interface{}) (Value, error) {
168 if v == nil {
169 return nil, fmt.Errorf("nil value not allowed")
170 }
171 return n.Converter.ConvertValue(v)
172 }
173
174
175 func IsValue(v interface{}) bool {
176 if v == nil {
177 return true
178 }
179 switch v.(type) {
180 case []byte, bool, float64, int64, string, time.Time:
181 return true
182 }
183 return false
184 }
185
186
187
188 func IsScanValue(v interface{}) bool {
189 return IsValue(v)
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 var DefaultParameterConverter defaultConverter
206
207 type defaultConverter struct{}
208
209 var _ ValueConverter = defaultConverter{}
210
211 var valuerReflectType = reflect.TypeOf((*Valuer)(nil)).Elem()
212
213
214
215
216
217
218
219
220
221
222
223
224 func callValuerValue(vr Valuer) (v Value, err error) {
225 if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
226 rv.IsNil() &&
227 rv.Type().Elem().Implements(valuerReflectType) {
228 return nil, nil
229 }
230 return vr.Value()
231 }
232
233 func (defaultConverter) ConvertValue(v interface{}) (Value, error) {
234 if IsValue(v) {
235 return v, nil
236 }
237
238 if vr, ok := v.(Valuer); ok {
239 sv, err := callValuerValue(vr)
240 if err != nil {
241 return nil, err
242 }
243 if !IsValue(sv) {
244 return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
245 }
246 return sv, nil
247 }
248
249 rv := reflect.ValueOf(v)
250 switch rv.Kind() {
251 case reflect.Ptr:
252
253 if rv.IsNil() {
254 return nil, nil
255 } else {
256 return defaultConverter{}.ConvertValue(rv.Elem().Interface())
257 }
258 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
259 return rv.Int(), nil
260 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
261 return int64(rv.Uint()), nil
262 case reflect.Uint64:
263 u64 := rv.Uint()
264 if u64 >= 1<<63 {
265 return nil, fmt.Errorf("uint64 values with high bit set are not supported")
266 }
267 return int64(u64), nil
268 case reflect.Float32, reflect.Float64:
269 return rv.Float(), nil
270 case reflect.Bool:
271 return rv.Bool(), nil
272 case reflect.Slice:
273 ek := rv.Type().Elem().Kind()
274 if ek == reflect.Uint8 {
275 return rv.Bytes(), nil
276 }
277 return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
278 case reflect.String:
279 return rv.String(), nil
280 }
281 return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
282 }
283
View as plain text