-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfield.go
223 lines (179 loc) · 4.4 KB
/
field.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
// Copyright 2022-present The ZTDBP Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql
import (
"encoding/binary"
"github.com/ztalab/mysqlproxy/utils"
)
type FieldData []byte
type Field struct {
Data FieldData
Schema []byte
Table []byte
OrgTable []byte
Name []byte
OrgName []byte
Charset uint16
ColumnLength uint32
Type uint8
Flag uint16
Decimal uint8
DefaultValueLength uint64
DefaultValue []byte
}
type FieldValueType uint8
type FieldValue struct {
Type FieldValueType
value uint64 // Also for int64 and float64
str []byte
}
const (
FieldValueTypeNull = iota
FieldValueTypeUnsigned
FieldValueTypeSigned
FieldValueTypeFloat
FieldValueTypeString
)
func (f *Field) Parse(p FieldData) (err error) {
f.Data = p
var n int
pos := 0
// skip catelog, always def
n, err = SkipLengthEncodedString(p)
if err != nil {
return
}
pos += n
// schema
f.Schema, _, n, err = LengthEncodedString(p[pos:])
if err != nil {
return
}
pos += n
// table
f.Table, _, n, err = LengthEncodedString(p[pos:])
if err != nil {
return
}
pos += n
// org_table
f.OrgTable, _, n, err = LengthEncodedString(p[pos:])
if err != nil {
return
}
pos += n
// name
f.Name, _, n, err = LengthEncodedString(p[pos:])
if err != nil {
return
}
pos += n
// org_name
f.OrgName, _, n, err = LengthEncodedString(p[pos:])
if err != nil {
return
}
pos += n
// skip oc
pos += 1
// charset
f.Charset = binary.LittleEndian.Uint16(p[pos:])
pos += 2
// column length
f.ColumnLength = binary.LittleEndian.Uint32(p[pos:])
pos += 4
// type
f.Type = p[pos]
pos++
// flag
f.Flag = binary.LittleEndian.Uint16(p[pos:])
pos += 2
// decimals 1
f.Decimal = p[pos]
pos++
// filter [0x00][0x00]
pos += 2
f.DefaultValue = nil
// if more data, command was field list
if len(p) > pos {
// length of default value lenenc-int
f.DefaultValueLength, _, n = LengthEncodedInt(p[pos:])
pos += n
if pos+int(f.DefaultValueLength) > len(p) {
err = ErrMalformPacket
return
}
// default value string[$len]
f.DefaultValue = p[pos:(pos + int(f.DefaultValueLength))]
}
return nil
}
func (p FieldData) Parse() (f *Field, err error) {
f = new(Field)
if err = f.Parse(p); err != nil {
return nil, err
}
return f, nil
}
func (f *Field) Dump() []byte {
if f == nil {
f = &Field{}
}
if f.Data != nil {
return []byte(f.Data)
}
l := len(f.Schema) + len(f.Table) + len(f.OrgTable) + len(f.Name) + len(f.OrgName) + len(f.DefaultValue) + 48
data := make([]byte, 0, l)
data = append(data, PutLengthEncodedString([]byte("def"))...)
data = append(data, PutLengthEncodedString(f.Schema)...)
data = append(data, PutLengthEncodedString(f.Table)...)
data = append(data, PutLengthEncodedString(f.OrgTable)...)
data = append(data, PutLengthEncodedString(f.Name)...)
data = append(data, PutLengthEncodedString(f.OrgName)...)
data = append(data, 0x0c)
data = append(data, Uint16ToBytes(f.Charset)...)
data = append(data, Uint32ToBytes(f.ColumnLength)...)
data = append(data, f.Type)
data = append(data, Uint16ToBytes(f.Flag)...)
data = append(data, f.Decimal)
data = append(data, 0, 0)
if f.DefaultValue != nil {
data = append(data, Uint64ToBytes(f.DefaultValueLength)...)
data = append(data, f.DefaultValue...)
}
return data
}
func (fv *FieldValue) AsUint64() uint64 {
return fv.value
}
func (fv *FieldValue) AsInt64() int64 {
return utils.Uint64ToInt64(fv.value)
}
func (fv *FieldValue) AsFloat64() float64 {
return utils.Uint64ToFloat64(fv.value)
}
func (fv *FieldValue) AsString() []byte {
return fv.str
}
func (fv *FieldValue) Value() interface{} {
switch fv.Type {
case FieldValueTypeUnsigned:
return fv.AsUint64()
case FieldValueTypeSigned:
return fv.AsInt64()
case FieldValueTypeFloat:
return fv.AsFloat64()
case FieldValueTypeString:
return fv.AsString()
default: // FieldValueTypeNull
return nil
}
}