/
numeric_fields.go
183 lines (156 loc) · 4.53 KB
/
numeric_fields.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
package binlog
import (
"github.com/dropbox/godropbox/errors"
mysql_proto "github.com/dropbox/godropbox/proto/mysql"
)
// This contains field descriptors for numeric types as defined by sql/field.h.
// In particular:
//
// Field (abstract)
// |
// ...
// |
// +--Field_num (abstract)
// | | +--Field_real (asbstract)
// | | +--Field_decimal
// | | +--Field_float
// | | +--Field_double
// | |
// | +--Field_new_decimal
// | +--Field_short
// | +--Field_medium
// | +--Field_long
// | +--Field_longlong
// | +--Field_tiny
// ...
//
// NOTE: Field_year is grouped with other temporal fields.
// This returns a field descriptor for FieldType_TINY (i.e., Field_tiny).
func NewTinyFieldDescriptor(nullable NullableColumn) FieldDescriptor {
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_TINY,
nullable,
1,
func(b []byte) interface{} { return uint64(b[0]) })
}
// This returns a field descriptor for FieldType_SHORT (i.e., Field_shart)
func NewShortFieldDescriptor(nullable NullableColumn) FieldDescriptor {
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_SHORT,
nullable,
2,
func(b []byte) interface{} { return uint64(LittleEndian.Uint16(b)) })
}
// This returns a field descriptor for FieldType_INT24 (i.e., Field_medium)
func NewInt24FieldDescriptor(nullable NullableColumn) FieldDescriptor {
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_INT24,
nullable,
3,
func(b []byte) interface{} { return uint64(LittleEndian.Uint24(b)) })
}
// This returns a field descriptor for FieldType_LONG (i.e., Field_long)
func NewLongFieldDescriptor(nullable NullableColumn) FieldDescriptor {
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_LONG,
nullable,
4,
func(b []byte) interface{} { return uint64(LittleEndian.Uint32(b)) })
}
// This returns a field descriptor for FieldType_LONGLONG (i.e., Field_longlong)
func NewLongLongFieldDescriptor(nullable NullableColumn) FieldDescriptor {
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_LONGLONG,
nullable,
8,
func(b []byte) interface{} { return LittleEndian.Uint64(b) })
}
// This returns a field descriptor for FieldType_FLOAT (i.e., Field_float)
func NewFloatFieldDescriptor(nullable NullableColumn, metadata []byte) (
fd FieldDescriptor,
remaining []byte,
err error) {
if len(metadata) < 1 {
return nil, nil, errors.New("metadata too short")
}
size := uint8(metadata[0])
if size != 4 {
return nil, nil, errors.New("invalid float size")
}
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_FLOAT,
nullable,
4,
func(b []byte) interface{} {
return float64(LittleEndian.Float32(b))
}),
metadata[1:],
nil
}
// This returns a field descriptor for FieldType_DOUBLE (i.e., Field_double)
func NewDoubleFieldDescriptor(nullable NullableColumn, metadata []byte) (
fd FieldDescriptor,
remaining []byte,
err error) {
if len(metadata) < 1 {
return nil, nil, errors.New("metadata too short")
}
size := uint8(metadata[0])
if size != 8 {
return nil, nil, errors.New("invalid double size")
}
return newFixedLengthFieldDescriptor(
mysql_proto.FieldType_DOUBLE,
nullable,
8,
func(b []byte) interface{} { return LittleEndian.Float64(b) }),
metadata[1:],
nil
}
type decimalFieldDescriptor struct {
baseFieldDescriptor
}
// This returns a field descriptor for FieldType_DECIMAL (i.e., Field_decimal)
func NewDecimalFieldDescriptor(nullable NullableColumn) FieldDescriptor {
return &decimalFieldDescriptor{
baseFieldDescriptor: baseFieldDescriptor{
fieldType: mysql_proto.FieldType_DECIMAL,
isNullable: nullable,
},
}
}
func (d *decimalFieldDescriptor) ParseValue(data []byte) (
value interface{},
remaining []byte,
err error) {
return nil, nil, errors.New("TODO")
}
type newDecimalFieldDescriptor struct {
baseFieldDescriptor
precision uint8
decimals uint8
}
// This returns a field descriptor for FieldType_NEWDECIMAL (i.e.,
// Field_newdecimal)
func NewNewDecimalFieldDescriptor(nullable NullableColumn, metadata []byte) (
fd FieldDescriptor,
remaining []byte,
err error) {
if len(metadata) < 2 {
return nil, nil, errors.New("Metadata has too few bytes")
}
return &newDecimalFieldDescriptor{
baseFieldDescriptor: baseFieldDescriptor{
fieldType: mysql_proto.FieldType_NEWDECIMAL,
isNullable: nullable,
},
precision: uint8(metadata[0]),
decimals: uint8(metadata[1]),
}, metadata[2:], nil
}
func (d *newDecimalFieldDescriptor) ParseValue(data []byte) (
value interface{},
remaining []byte,
err error) {
return nil, nil, errors.New("TODO")
}