-
Notifications
You must be signed in to change notification settings - Fork 1
/
valuer.go
476 lines (449 loc) · 9.24 KB
/
valuer.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2014 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.
package rdb
import (
"fmt"
"io"
"math/big"
"time"
)
// TODO: Rename to DriverAssigner and document better.
// Assigner can be used by the driver to put special values directly into prepped
// value pointer.
type Assigner func(input, output interface{}) (handled bool, err error)
type DriverValuer interface {
Columns([]*Column) error
Done() error
RowScanned()
Message(*Message)
WriteField(c *Column, value *DriverValue, assign Assigner) error
RowsAffected(count uint64)
}
type valuer struct {
cmd *Command
errorList Errors
infoList []*Message
fields []*Field
eof bool
columns []*Column
columnLookup map[string]*Column
buffer []Nullable
prep []interface{}
convert []ColumnConverter
rowCount uint64
rowsAffected uint64
}
func (v *valuer) clearBuffer() {
for i := range v.buffer {
v.buffer[i] = Nullable{
Null: true,
}
}
}
func (v *valuer) clearPrep() {
for i := range v.prep {
v.prep[i] = nil
}
}
func (v *valuer) Columns(cc []*Column) error {
v.columns = cc
v.columnLookup = make(map[string]*Column, len(cc))
for _, col := range cc {
v.columnLookup[col.Name] = col
}
v.buffer = make([]Nullable, len(cc))
v.prep = make([]interface{}, len(cc))
// Prepare fields.
v.fields = make([]*Field, len(cc))
for i, field := range v.cmd.Fields {
if len(field.Name) == 0 {
if i >= len(v.columns) {
// Don't error. Some queries may return
// different number of columns.
continue
}
v.fields[i] = &field
} else {
col, found := v.columnLookup[field.Name]
if !found {
// Don't error. Some queries may return
// different number of columns.
continue
}
v.fields[col.Index] = &field
}
}
if v.cmd.Converter != nil {
v.convert = make([]ColumnConverter, len(cc))
for i, col := range cc {
v.convert[i] = v.cmd.Converter.ColumnConverter(col)
}
}
return nil
}
func (v *valuer) Message(msg *Message) {
if l := v.cmd.Log; l != nil {
l(msg)
}
switch msg.Type {
case SqlInfo:
v.infoList = append(v.infoList, msg)
case SqlError:
v.errorList = append(v.errorList, msg)
}
}
func (v *valuer) RowScanned() {
v.rowCount += 1
}
func (v *valuer) Done() error {
v.eof = true
for i := range v.prep {
v.prep[i] = nil
}
if len(v.errorList) != 0 {
return v.errorList
}
return nil
}
func (v *valuer) RowsAffected(count uint64) {
v.rowsAffected += count
}
/*
if (value is null) && (has default value) {
set value to default value
}
if no prepped value {
if chunked {
append to any existing value.
}
}
if prepped value {
if prepped value is Nullable
}
If there is a default value for the field, use the default value.
If there is no prepped value, put it in a buffer.
If using a buffer, append any value
*/
func (v *valuer) WriteField(c *Column, value *DriverValue, assign Assigner) error {
// TODO: Respect value.MustCopy.
var convert ColumnConverter
if v.convert != nil {
convert = v.convert[c.Index]
}
prep := v.prep[c.Index]
f := v.fields[c.Index]
if value.Null && f != nil && f.Null != nil {
value.Null = false
value.Value = f.Null
}
if prep == nil {
if value.Chunked {
bf := v.buffer[c.Index]
if bf.Value == nil {
outValue := Nullable{
Null: value.Null,
Value: value.Value,
}
if !value.More && convert != nil {
convert(c, &outValue)
}
v.buffer[c.Index] = outValue
return nil
}
switch in := value.Value.(type) {
case []byte:
v.buffer[c.Index].Value = append(bf.Value.([]byte), in...)
default:
return fmt.Errorf("Type not supported for chunked read: %T", in)
}
if !value.More && convert != nil {
nv := &v.buffer[c.Index]
convert(c, nv)
}
return nil
}
outValue := Nullable{
Null: value.Null,
Value: value.Value,
}
if convert != nil {
convert(c, &outValue)
}
v.buffer[c.Index] = outValue
return nil
}
outValue := Nullable{
Null: value.Null,
Value: value.Value,
}
if convert != nil {
convert(c, &outValue)
}
return AssignValue(c, outValue, prep, assign)
}
func AssignValue(c *Column, outValue Nullable, prep interface{}, assign Assigner) error {
if nullable, is := prep.(*Nullable); is {
*nullable = outValue
return nil
}
if outValue.Null || outValue.Value == nil {
// Can only scan a null value into a nullable type.
return ErrScanNull
}
var err error
var handled = false
if assign != nil {
handled, err = assign(outValue.Value, prep)
if handled {
return err
}
}
switch in := outValue.Value.(type) {
case string:
switch out := prep.(type) {
case io.Writer:
_, err = out.Write([]byte(in))
case *string:
*out = in
case *[]byte:
*out = []byte(in)
default:
return errorTypeNotSupported(in, out, c)
}
case []byte:
switch out := prep.(type) {
case io.Writer:
_, err = out.Write(in)
case *string:
*out = string(in)
case *[]byte:
*out = in
default:
return errorTypeNotSupported(in, out, c)
}
case bool:
switch out := prep.(type) {
case *bool:
*out = bool(in)
default:
return errorTypeNotSupported(in, out, c)
}
case uint8:
switch out := prep.(type) {
case *uint8:
*out = uint8(in)
case *int8:
*out = int8(in)
case *uint16:
*out = uint16(in)
case *int16:
*out = int16(in)
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case int8:
switch out := prep.(type) {
case *uint8:
*out = uint8(in)
case *int8:
*out = int8(in)
case *uint16:
*out = uint16(in)
case *int16:
*out = int16(in)
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case uint16:
switch out := prep.(type) {
case *uint16:
*out = uint16(in)
case *int16:
*out = int16(in)
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case int16:
switch out := prep.(type) {
case *uint16:
*out = uint16(in)
case *int16:
*out = int16(in)
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case uint32:
switch out := prep.(type) {
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case int32:
switch out := prep.(type) {
case *uint32:
*out = uint32(in)
case *int32:
*out = int32(in)
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case uint64:
switch out := prep.(type) {
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case int64:
switch out := prep.(type) {
case *uint64:
*out = uint64(in)
case *int64:
*out = int64(in)
case *uint:
*out = uint(in)
case *int:
*out = int(in)
default:
return errorTypeNotSupported(in, out, c)
}
case float32:
switch out := prep.(type) {
case **big.Rat:
(*out).SetFloat64(float64(in))
case *big.Rat:
out.SetFloat64(float64(in))
case *float64:
*out = float64(in)
case *float32:
*out = float32(in)
default:
return errorTypeNotSupported(in, out, c)
}
case float64:
switch out := prep.(type) {
case **big.Rat:
(*out).SetFloat64(float64(in))
case *big.Rat:
out.SetFloat64(float64(in))
case *float64:
*out = float64(in)
case *float32:
*out = float32(in)
default:
return errorTypeNotSupported(in, out, c)
}
case *big.Rat:
switch out := prep.(type) {
case **big.Rat:
*out = in
case *big.Rat:
out.Set(in)
case *float64:
fl, _ := in.Float64()
*out = float64(fl)
case *float32:
fl, _ := in.Float64()
*out = float32(fl)
default:
return errorTypeNotSupported(in, out, c)
}
case time.Time:
switch out := prep.(type) {
case *time.Time:
*out = in
default:
return errorTypeNotSupported(in, out, c)
}
case time.Duration:
switch out := prep.(type) {
case *time.Duration:
*out = in
default:
return errorTypeNotSupported(in, out, c)
}
default:
return errorTypeNotSupported(nil, nil, c)
}
return err
}
func errorTypeNotSupported(in, out interface{}, c *Column) error {
if out == nil && in == nil {
return fmt.Errorf("Unsupported column type: %s", c.Name)
}
return fmt.Errorf("Prep type (%T) cannot fit data type (%T) in %s", out, in, c.Name)
}