-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathsqltypes.go
590 lines (520 loc) · 14.4 KB
/
sqltypes.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
// Package sqltypes implements interfaces and types that represent SQL values.
//
// DROPBOX NOTE: This is a modified version of vitess's sqltypes module.
// The original source can be found at https://code.google.com/p/vitess/
package sqltypes
import (
"bytes"
"encoding/base64"
"encoding/binary"
"reflect"
"strconv"
"time"
"github.com/dropbox/godropbox/encoding2"
"github.com/dropbox/godropbox/errors"
)
var (
NULL = Value{}
DONTESCAPE = byte(255)
nullstr = []byte("null")
)
type ValueType byte
const (
NullType = ValueType(0)
NumericType = ValueType(1)
FractionalType = ValueType(2)
StringType = ValueType(3)
UTF8StringType = ValueType(4)
)
// Value can store any SQL value. NULL is stored as nil.
type Value struct {
Inner InnerValue
}
// Numeric represents non-fractional SQL number.
type Numeric []byte
// Fractional represents fractional types like float and decimal
// It's functionally equivalent to Numeric other than how it's constructed
type Fractional []byte
// String represents any SQL type that needs to be represented using quotes.
// If isUtf8 is false, it will be hex encoded so it's safe for exception reporting, etc.
type String struct {
data []byte
isUtf8 bool
}
// MakeNumeric makes a Numeric from a []byte without validation.
func MakeNumeric(b []byte) Value {
return Value{Numeric(b)}
}
// MakeFractional makes a Fractional value from a []byte without validation.
func MakeFractional(b []byte) Value {
return Value{Fractional(b)}
}
// MakeString makes a String value from a []byte.
func MakeString(b []byte) Value {
return Value{String{b, false}}
}
// MakeUtf8String makes a String value from a []byte.
func MakeUtf8String(s string) Value {
return Value{String{[]byte(s), true}}
}
// Raw returns the raw bytes. All types are currently implemented as []byte.
func (v Value) Raw() []byte {
if v.Inner == nil {
return nil
}
return v.Inner.raw()
}
// String returns the raw value as a string
func (v Value) String() string {
if v.Inner == nil {
return ""
}
return string(v.Inner.raw())
}
// EncodeSql encodes the value into an SQL statement. Can be binary.
func (v Value) EncodeSql(b encoding2.BinaryWriter) {
if v.Inner == nil {
if _, err := b.Write(nullstr); err != nil {
panic(err)
}
} else {
v.Inner.encodeSql(b)
}
}
// EncodeAscii encodes the value using 7-bit clean ascii bytes.
func (v Value) EncodeAscii(b encoding2.BinaryWriter) {
if v.Inner == nil {
if _, err := b.Write(nullstr); err != nil {
panic(err)
}
} else {
v.Inner.encodeAscii(b)
}
}
// MarshalBinary helps implement BinaryMarshaler interface for Value.
func (v Value) MarshalBinary() ([]byte, error) {
if v.IsNull() {
return []byte{byte(NullType)}, nil
}
return v.Inner.MarshalBinary()
}
// UnmarshalBinary helps implement BinaryUnmarshaler interface for Value.
func (v *Value) UnmarshalBinary(data []byte) error {
reader := bytes.NewReader(data)
b, err := reader.ReadByte()
if err != nil {
return err
}
typ := ValueType(b)
if typ == NullType {
*v = Value{}
return nil
}
length, err := binary.ReadUvarint(reader)
if err != nil {
return err
}
raw := make([]byte, length)
n, err := reader.Read(raw)
if err != nil {
return err
}
if uint64(n) != length {
return errors.Newf("Not enough bytes to read Value")
}
switch typ {
case NumericType:
*v = Value{Numeric(raw)}
case FractionalType:
*v = Value{Fractional(raw)}
case StringType:
*v = Value{String{raw, false}}
case UTF8StringType:
*v = Value{String{raw, true}}
default:
return errors.Newf("Unknown type %d", int(typ))
}
return nil
}
func (v Value) IsNull() bool {
return v.Inner == nil
}
func (v Value) IsNumeric() (ok bool) {
_ = Numeric(nil) // compiler bug work-around
if v.Inner != nil {
_, ok = v.Inner.(Numeric)
}
return ok
}
func (v Value) IsFractional() (ok bool) {
_ = Fractional(nil) // compiler bug work-around
if v.Inner != nil {
_, ok = v.Inner.(Fractional)
}
return ok
}
func (v Value) IsString() (ok bool) {
_ = String{} // compiler bug work-around
if v.Inner != nil {
_, ok = v.Inner.(String)
}
return ok
}
func (v Value) IsUtf8String() (ok bool) {
_ = String{} // compiler bug work-around
if v.Inner != nil {
s, ok := v.Inner.(String)
ok = ok && s.isUtf8
}
return ok
}
// InnerValue defines methods that need to be supported by all non-null value types.
type InnerValue interface {
raw() []byte
encodeSql(encoding2.BinaryWriter)
encodeAscii(encoding2.BinaryWriter)
MarshalBinary() ([]byte, error)
}
func BuildValue(goval interface{}) (v Value, err error) {
switch bindVal := goval.(type) {
case nil:
// no op
case bool:
val := 0
if bindVal {
val = 1
}
v = Value{Numeric(strconv.AppendInt(nil, int64(val), 10))}
case int:
v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
case int32:
v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
case int64:
v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
case uint:
v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
case uint8:
v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
case uint32:
v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
case uint64:
v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
case float64:
v = Value{Fractional(strconv.AppendFloat(nil, bindVal, 'f', -1, 64))}
case string:
v = Value{String{[]byte(bindVal), true}}
case []byte:
v = Value{String{bindVal, false}}
case time.Time:
v = Value{String{[]byte(bindVal.Format("2006-01-02 15:04:05.000000")), true}}
case Numeric, Fractional, String:
v = Value{bindVal.(InnerValue)}
case Value:
v = bindVal
default:
// Check if v is a pointer.
rv := reflect.ValueOf(goval)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
return BuildValue(nil)
}
return BuildValue(reflect.Indirect(rv).Interface())
}
return Value{}, errors.Newf("Unsupported bind variable type %T: %v", goval, goval)
}
return v, nil
}
// ConverAssignRowNullable is the same as ConvertAssignRow except that it allows
// nil as a value for the row or any of the row values. In thoses cases, the
// corresponding values are ignored.
func ConvertAssignRowNullable(row []Value, dest ...interface{}) error {
if len(row) != len(dest) {
return errors.Newf(
"# of row entries %d does not match # of destinations %d",
len(row),
len(dest))
}
if row == nil {
return nil
}
for i := 0; i < len(row); i++ {
if row[i].IsNull() {
continue
}
err := ConvertAssign(row[i], dest[i])
if err != nil {
return err
}
}
return nil
}
// ConvertAssignRow copies a row of values in the list of destinations. An
// error is returned if any one of the row's element coping is done between
// incompatible value and dest types. The list of destinations must contain
// pointers.
// Note that for anything else than *[]byte the value is copied, however if
// the destination is of type *[]byte it will point the same []byte array as
// the source (no copying).
func ConvertAssignRow(row []Value, dest ...interface{}) error {
if len(row) != len(dest) {
return errors.Newf(
"# of row entries %d does not match # of destinations %d",
len(row),
len(dest))
}
for i := 0; i < len(row); i++ {
err := ConvertAssign(row[i], dest[i])
if err != nil {
return err
}
}
return nil
}
// ConvertAssign copies to the '*dest' the value in 'src'. An error is returned
// if the coping is done between incompatible Value and dest types. 'dest' must be
// a pointer type.
// Note that for anything else than *[]byte the value is copied, however if 'dest'
// is of type *[]byte it will point to same []byte array as 'src.Raw()' (no copying).
func ConvertAssign(src Value, dest interface{}) error {
// TODO(zviad): reflecting might be too slow so common cases
// can probably be handled without reflections
var s String
var n Numeric
var f Fractional
var ok bool
var err error
if src.Inner == nil {
return errors.Newf("source is null")
}
switch d := dest.(type) {
case *string:
if s, ok = src.Inner.(String); !ok {
return errors.Newf("source: '%v' is not String", src)
}
*d = string(s.raw())
return nil
case *[]byte:
if s, ok = src.Inner.(String); !ok {
return errors.Newf("source: '%v' is not String", src)
}
*d = s.raw()
return nil
// TODO(zviad): figure out how to do this without reflections
// because I think reflections are slow?
//case *int, *int8, *int16, *int32, *int64:
// if n, ok := src.Inner.(Numeric); !ok {
// return errors.Newf("source: %v is not Numeric", src)
// }
// if i64, err := strconv.ParseInt(string(n.raw()), 10, 64); err != nil {
// return err
// }
// *d = i64
// return nil
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
return errors.Newf("destination not a pointer")
}
if dpv.IsNil() {
return errors.Newf("destination pointer is Nil")
}
dv := reflect.Indirect(dpv)
switch dv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if n, ok = src.Inner.(Numeric); !ok {
return errors.Newf("source: '%v' is not Numeric", src)
}
var i64 int64
if i64, err = strconv.ParseInt(string(n.raw()), 10, dv.Type().Bits()); err != nil {
return err
}
dv.SetInt(i64)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if n, ok = src.Inner.(Numeric); !ok {
return errors.Newf("source: '%v' is not Numeric", src)
}
var u64 uint64
if u64, err = strconv.ParseUint(string(n.raw()), 10, dv.Type().Bits()); err != nil {
return err
}
dv.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
if f, ok = src.Inner.(Fractional); !ok {
return errors.Newf("source: '%v' is not Fractional", src)
}
var f64 float64
if f64, err = strconv.ParseFloat(string(f.raw()), dv.Type().Bits()); err != nil {
return err
}
dv.SetFloat(f64)
return nil
case reflect.Bool:
// treat bool as true if non-zero integer
if n, ok = src.Inner.(Numeric); !ok {
return errors.Newf("source: '%v' is not Numeric", src)
}
var i64 int64
if i64, err = strconv.ParseInt(string(n.raw()), 10, 64); err != nil {
return err
}
dv.SetBool(i64 != 0)
return nil
}
return errors.Newf("unsupported destination type: %v", dest)
}
// ConvertAssign, but with support for default values
func ConvertAssignDefault(src Value, dest interface{}, defaultValue interface{}) error {
if src.IsNull() {
// This is not the most efficient way of doing things, but it's certainly cleaner
v, err := BuildValue(defaultValue)
if err != nil {
return err
}
return ConvertAssign(v, dest)
}
return ConvertAssign(src, dest)
}
// BuildNumeric builds a Numeric type that represents any whole number.
// It normalizes the representation to ensure 1:1 mapping between the
// number and its representation.
func BuildNumeric(val string) (n Value, err error) {
if val[0] == '-' || val[0] == '+' {
signed, err := strconv.ParseInt(val, 0, 64)
if err != nil {
return Value{}, err
}
n = Value{Numeric(strconv.AppendInt(nil, signed, 10))}
} else {
unsigned, err := strconv.ParseUint(val, 0, 64)
if err != nil {
return Value{}, err
}
n = Value{Numeric(strconv.AppendUint(nil, unsigned, 10))}
}
return n, nil
}
func writeBinary(typ ValueType, data []byte) ([]byte, error) {
var scratch [binary.MaxVarintLen64]byte
n := binary.PutUvarint(scratch[:], uint64(len(data)))
var buf bytes.Buffer
buf.WriteByte(byte(typ))
buf.Write(scratch[:n])
buf.Write(data)
return buf.Bytes(), nil
}
func (n Numeric) raw() []byte {
return []byte(n)
}
func (n Numeric) encodeSql(b encoding2.BinaryWriter) {
if _, err := b.Write(n.raw()); err != nil {
panic(err)
}
}
func (n Numeric) encodeAscii(b encoding2.BinaryWriter) {
if _, err := b.Write(n.raw()); err != nil {
panic(err)
}
}
func (n Numeric) MarshalBinary() ([]byte, error) {
return writeBinary(NumericType, n.raw())
}
func (f Fractional) raw() []byte {
return []byte(f)
}
func (f Fractional) encodeSql(b encoding2.BinaryWriter) {
if _, err := b.Write(f.raw()); err != nil {
panic(err)
}
}
func (f Fractional) encodeAscii(b encoding2.BinaryWriter) {
if _, err := b.Write(f.raw()); err != nil {
panic(err)
}
}
func (f Fractional) MarshalBinary() ([]byte, error) {
return writeBinary(FractionalType, f.raw())
}
func (s String) raw() []byte {
return []byte(s.data)
}
func (s String) encodeSql(b encoding2.BinaryWriter) {
if s.isUtf8 {
writebyte(b, '\'')
rawBytes := s.raw()
for i, ch := range rawBytes {
if encodedChar := SqlEncodeMap[ch]; encodedChar == DONTESCAPE {
writebyte(b, ch)
} else if i < len(rawBytes)-1 && '\\' == ch && ('%' == rawBytes[i+1] || '_' == rawBytes[i+1]) {
// Don't escape '\' specifically in the constructions '\%' or
// '\_', because those are special to how the RHS of LIKE
// clauses are escaped. See the notes following table 9.1 in
// http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
writebyte(b, ch)
} else {
writebyte(b, '\\')
writebyte(b, encodedChar)
}
}
writebyte(b, '\'')
} else {
b.Write([]byte("X'"))
encoding2.HexEncodeToWriter(b, s.raw())
writebyte(b, '\'')
}
}
func (s String) encodeAscii(b encoding2.BinaryWriter) {
writebyte(b, '\'')
encoder := base64.NewEncoder(base64.StdEncoding, b)
encoder.Write(s.raw())
encoder.Close()
writebyte(b, '\'')
}
func (s String) MarshalBinary() ([]byte, error) {
if s.isUtf8 {
return writeBinary(UTF8StringType, s.raw())
}
return writeBinary(StringType, s.raw())
}
func writebyte(b encoding2.BinaryWriter, c byte) {
if err := b.WriteByte(c); err != nil {
panic(err)
}
}
// Helper function for converting a uint64 to a string suitable for SQL.
func Uint64EncodeSql(b encoding2.BinaryWriter, num uint64) {
numVal, _ := BuildValue(num)
numVal.EncodeSql(b)
}
// SqlEncodeMap specifies how to escape binary data with '\'.
// Complies to http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html
var SqlEncodeMap [256]byte
// SqlDecodeMap is the reverse of SqlEncodeMap
var SqlDecodeMap [256]byte
var encodeRef = map[byte]byte{
'\x00': '0',
'\'': '\'',
'"': '"',
'\b': 'b',
'\n': 'n',
'\r': 'r',
'\t': 't',
26: 'Z', // ctl-Z
'\\': '\\',
}
func init() {
for i, _ := range SqlEncodeMap {
SqlEncodeMap[i] = DONTESCAPE
SqlDecodeMap[i] = DONTESCAPE
}
for i, _ := range SqlEncodeMap {
if to, ok := encodeRef[byte(i)]; ok {
SqlEncodeMap[byte(i)] = to
SqlDecodeMap[to] = byte(i)
}
}
}