-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
240 lines (223 loc) · 5.33 KB
/
convert.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
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocqldriver
import (
"database/sql/driver"
"encoding/binary"
"fmt"
"github.com/tux21b/gocql/uuid"
"math"
"reflect"
"strconv"
"time"
)
const (
typeCustom uint16 = 0x0000
typeAscii uint16 = 0x0001
typeBigInt uint16 = 0x0002
typeBlob uint16 = 0x0003
typeBool uint16 = 0x0004
typeCounter uint16 = 0x0005
typeDecimal uint16 = 0x0006
typeDouble uint16 = 0x0007
typeFloat uint16 = 0x0008
typeInt uint16 = 0x0009
typeText uint16 = 0x000A
typeTimestamp uint16 = 0x000B
typeUUID uint16 = 0x000C
typeVarchar uint16 = 0x000D
typeVarint uint16 = 0x000E
typeTimeUUID uint16 = 0x000F
typeList uint16 = 0x0020
typeMap uint16 = 0x0021
typeSet uint16 = 0x0022
)
func decode(b []byte, t uint16) driver.Value {
switch t {
case typeBool:
if len(b) >= 1 && b[0] != 0 {
return true
}
return false
case typeBlob:
return b
case typeVarchar, typeText, typeAscii:
return b
case typeInt:
return int64(int32(binary.BigEndian.Uint32(b)))
case typeBigInt, typeCounter:
return int64(binary.BigEndian.Uint64(b))
case typeFloat:
return float64(math.Float32frombits(binary.BigEndian.Uint32(b)))
case typeDouble:
return math.Float64frombits(binary.BigEndian.Uint64(b))
case typeTimestamp:
t := int64(binary.BigEndian.Uint64(b))
sec := t / 1000
nsec := (t - sec*1000) * 1000000
return time.Unix(sec, nsec)
case typeUUID, typeTimeUUID:
return uuid.FromBytes(b)
default:
panic("unsupported type")
}
return b
}
type columnEncoder struct {
columnTypes []uint16
}
func (e *columnEncoder) ColumnConverter(idx int) ValueConverter {
switch e.columnTypes[idx] {
case typeInt:
return ValueConverter(encInt)
case typeBigInt:
return ValueConverter(encBigInt)
case typeFloat:
return ValueConverter(encFloat)
case typeDouble:
return ValueConverter(encDouble)
case typeBool:
return ValueConverter(encBool)
case typeVarchar, typeText, typeAscii:
return ValueConverter(encVarchar)
case typeBlob:
return ValueConverter(encBlob)
case typeTimestamp:
return ValueConverter(encTimestamp)
case typeUUID, typeTimeUUID:
return ValueConverter(encUUID)
}
panic("not implemented")
}
type ValueConverter func(v interface{}) (driver.Value, error)
func (vc ValueConverter) ConvertValue(v interface{}) (driver.Value, error) {
return vc(v)
}
func encBool(v interface{}) (driver.Value, error) {
b, err := driver.Bool.ConvertValue(v)
if err != nil {
return nil, err
}
if b.(bool) {
return []byte{1}, nil
}
return []byte{0}, nil
}
func encInt(v interface{}) (driver.Value, error) {
x, err := driver.Int32.ConvertValue(v)
if err != nil {
return nil, err
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(x.(int64)))
return b, nil
}
func encBigInt(v interface{}) (driver.Value, error) {
x := reflect.Indirect(reflect.ValueOf(v)).Interface()
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(x.(int64)))
return b, nil
}
func encVarchar(v interface{}) (driver.Value, error) {
x, err := driver.String.ConvertValue(v)
if err != nil {
return nil, err
}
return []byte(x.(string)), nil
}
func encFloat(v interface{}) (driver.Value, error) {
x, err := driver.DefaultParameterConverter.ConvertValue(v)
if err != nil {
return nil, err
}
var f float64
switch x := x.(type) {
case float64:
f = x
case int64:
f = float64(x)
case []byte:
if f, err = strconv.ParseFloat(string(x), 64); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("can not convert %T to float64", x)
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, math.Float32bits(float32(f)))
return b, nil
}
func encDouble(v interface{}) (driver.Value, error) {
x, err := driver.DefaultParameterConverter.ConvertValue(v)
if err != nil {
return nil, err
}
var f float64
switch x := x.(type) {
case float64:
f = x
case int64:
f = float64(x)
case []byte:
if f, err = strconv.ParseFloat(string(x), 64); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("can not convert %T to float64", x)
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, math.Float64bits(f))
return b, nil
}
func encTimestamp(v interface{}) (driver.Value, error) {
x, err := driver.DefaultParameterConverter.ConvertValue(v)
if err != nil {
return nil, err
}
var millis int64
switch x := x.(type) {
case time.Time:
x = x.In(time.UTC)
millis = x.UnixNano() / 1000000
default:
return nil, fmt.Errorf("can not convert %T to a timestamp", x)
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(millis))
return b, nil
}
func encBlob(v interface{}) (driver.Value, error) {
x, err := driver.DefaultParameterConverter.ConvertValue(v)
if err != nil {
return nil, err
}
var b []byte
switch x := x.(type) {
case string:
b = []byte(x)
case []byte:
b = x
default:
return nil, fmt.Errorf("can not convert %T to a []byte", x)
}
return b, nil
}
func encUUID(v interface{}) (driver.Value, error) {
var u uuid.UUID
switch v := v.(type) {
case string:
var err error
u, err = uuid.ParseUUID(v)
if err != nil {
return nil, err
}
case []byte:
u = uuid.FromBytes(v)
case uuid.UUID:
u = v
default:
return nil, fmt.Errorf("can not convert %T to a UUID", v)
}
return u.Bytes(), nil
}