-
Notifications
You must be signed in to change notification settings - Fork 4
/
datatypes.go
261 lines (228 loc) · 7.28 KB
/
datatypes.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
// mysqlx - MySQL driver for Go's database/sql package and MySQL X Protocol.
// Copyright (c) 2017-2018 Alexey Palazhchenko
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mysqlx
import (
"bytes"
"database/sql/driver"
"encoding/binary"
"fmt"
"math"
"reflect"
"time"
"github.com/golang/protobuf/proto"
"github.com/AlekSi/mysqlx/internal/proto/mysqlx_datatypes"
"github.com/AlekSi/mysqlx/internal/proto/mysqlx_resultset"
)
var btoa = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
func unmarshalDecimal(value []byte) (string, error) {
addToGoFuzzCorpus("unmarshalDecimal", value)
if len(value) < 2 {
return "", fmt.Errorf("unmarshalDecimal: failed to parse decimal %#v", value)
}
sign := value[len(value)-1]
var s string
for _, b := range value[1 : len(value)-1] {
h := (b >> 4) & 0x0f
l := b & 0x0f
if h > 9 || l > 9 {
return "", fmt.Errorf("unmarshalDecimal: failed to parse decimal %#v", value)
}
s += btoa[h] + btoa[l]
}
if sign != 0xd0 && sign != 0xc0 {
h := (sign >> 4) & 0x0f
if h > 9 {
return "", fmt.Errorf("unmarshalDecimal: failed to parse decimal %#v", value)
}
s += btoa[h]
sign <<= 4
}
if scale := int(value[0]); scale != 0 {
if scale >= len(s) {
return "", fmt.Errorf("unmarshalDecimal: failed to parse decimal %#v", value)
}
s = s[:len(s)-scale] + "." + s[len(s)-scale:]
}
switch sign {
case 0xd0:
return "-" + s, nil
case 0xc0:
return s, nil
default:
return "", fmt.Errorf("unmarshalDecimal: failed to parse decimal %#v", value)
}
}
func unmarshalValue(value []byte, column *mysqlx_resultset.ColumnMetaData) (driver.Value, error) {
// NULL -> nil, ignore type
if len(value) == 0 {
return nil, nil
}
// in order of ColumnMetaData_FieldType values
switch *column.Type {
case mysqlx_resultset.ColumnMetaData_SINT:
// TINY, SHORT, INT24, INT, LONGLONG
i64, n := binary.Varint(value)
if n != len(value) {
return nil, bugf("unmarshalValue: failed to decode %#v as SINT", value)
}
return i64, nil
case mysqlx_resultset.ColumnMetaData_UINT:
// TINY UNSIGNED, SHORT UNSIGNED, INT24 UNSIGNED, INT UNSIGNED, LONGLONG UNSIGNED, YEAR
u64, n := binary.Uvarint(value)
if n != len(value) {
return nil, bugf("unmarshalValue: failed to decode %#v as UINT", value)
}
return u64, nil
case mysqlx_resultset.ColumnMetaData_DOUBLE:
// DOUBLE
u64, err := proto.NewBuffer(value).DecodeFixed64()
if err != nil {
return nil, bugf("unmarshalValue: failed to decode %#v as DOUBLE: %s", value, err)
}
return math.Float64frombits(u64), nil
case mysqlx_resultset.ColumnMetaData_FLOAT:
// FLOAT
u64, err := proto.NewBuffer(value).DecodeFixed32()
if err != nil {
return nil, bugf("unmarshalValue: failed to decode %#v as FLOAT: %s", value, err)
}
return float64(math.Float32frombits(uint32(u64))), nil
case mysqlx_resultset.ColumnMetaData_BYTES:
// VARCHAR, CHAR, GEOMETRY (and also NULL, but we handle it separately)
return string(value[:len(value)-1]), nil // trim last 0x00
case mysqlx_resultset.ColumnMetaData_TIME:
// TIME
// FIXME convert to time.Duration? what about range?
// and time.Duration is not a driver.Value!
return nil, bugf("unmarshalValue: unhandled TIME %s, value %#v", column.Type, value)
case mysqlx_resultset.ColumnMetaData_DATETIME:
// DATE, DATETIME, TIMESTAMP
// year, month and day are mandatory, other parts are optional
r := bytes.NewReader(value)
year, _ := binary.ReadUvarint(r)
month, _ := binary.ReadUvarint(r)
day, err := binary.ReadUvarint(r)
if err != nil {
return nil, bugf("unmarshalValue: failed to decode %#v as DATETIME: %s", value, err)
}
hour, _ := binary.ReadUvarint(r)
min, _ := binary.ReadUvarint(r)
sec, _ := binary.ReadUvarint(r)
usec, _ := binary.ReadUvarint(r)
return time.Date(int(year), time.Month(month), int(day), int(hour), int(min), int(sec), int(usec)*1000, time.UTC), nil
case mysqlx_resultset.ColumnMetaData_SET:
// SET
return nil, bugf("unmarshalValue: unhandled SET %s, value %#v", column.Type, value)
case mysqlx_resultset.ColumnMetaData_ENUM:
// ENUM
return string(value[:len(value)-1]), nil // trim last 0x00
case mysqlx_resultset.ColumnMetaData_BIT:
// BIT
return nil, bugf("unmarshalValue: unhandled BIT %s, value %#v", column.Type, value)
case mysqlx_resultset.ColumnMetaData_DECIMAL:
// DECIMAL
return unmarshalDecimal(value)
default:
return nil, bugf("unmarshalValue: unhandled type %s, value %#v", column.Type, value)
}
}
func marshalValue(value driver.Value) (*mysqlx_datatypes.Any, error) {
// Due to conn.CheckNamedValue passing on everything, value there can be of any type, not only of the one of
// standard driver.Value types. We should handle everything ourselves.
v := reflect.ValueOf(value)
if v.Kind() == reflect.Ptr {
v = v.Elem()
if v.IsValid() {
value = v.Interface()
} else {
value = nil
}
}
// nil -> NULL
if value == nil {
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_NULL.Enum(),
},
}, nil
}
// in order of Scalar_Type values
switch value := value.(type) {
case int, int8, int16, int32, int64:
// SINT
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_SINT.Enum(),
VSignedInt: proto.Int64(reflect.ValueOf(value).Int()),
},
}, nil
case uint, uint8, uint16, uint32, uint64:
// UINT
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_UINT.Enum(),
VUnsignedInt: proto.Uint64(reflect.ValueOf(value).Uint()),
},
}, nil
case float64:
// DOUBLE
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_DOUBLE.Enum(),
VDouble: &value,
},
}, nil
case float32:
// FLOAT
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_FLOAT.Enum(),
VFloat: &value,
},
}, nil
case bool:
// BOOL
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_BOOL.Enum(),
VBool: &value,
},
}, nil
case string:
// STRING
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_STRING.Enum(),
VString: &mysqlx_datatypes.Scalar_String{
Value: []byte(value),
},
},
}, nil
case time.Time:
s := value.Format("2006-01-02 15:04:05.999999999")
return &mysqlx_datatypes.Any{
Type: mysqlx_datatypes.Any_SCALAR.Enum(),
Scalar: &mysqlx_datatypes.Scalar{
Type: mysqlx_datatypes.Scalar_V_OCTETS.Enum(),
VOctets: &mysqlx_datatypes.Scalar_Octets{
Value: []byte(s),
},
},
}, nil
case uintptr:
return nil, fmt.Errorf("marshalValue: unhandled type %T, value %#v", value, value)
default:
return nil, bugf("marshalValue: unhandled type %T, value %#v", value, value)
}
}