-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysql_data.go
227 lines (212 loc) · 4.94 KB
/
mysql_data.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
package mysql
import (
"bufio"
"encoding/binary"
"fmt"
"log"
"time"
)
type PacketHeader struct {
Len uint64
Seq uint8
}
type MySQLResultSet struct {
FieldCount uint64
Fields []*MySQLField
Rows []*MySQLRow
}
type MySQLResponse struct {
FieldCount uint64
AffectedRows uint64
InsertId uint64
ServerStatus uint16
WarningCount uint16
Message string
EOF bool
Prepared bool //Result from prapered statement
ResultSet *MySQLResultSet
mysql *MySQLInstance
}
func (s *MySQLStatement) String() string {
return fmt.Sprintf("Statement Id = %d, Columns = %d, Parameters = %d", s.StatementId, s.Columns, s.Parameters)
}
func (r *MySQLResponse) String() string {
var msg string
if r == nil {
return "nil"
}
if r.FieldCount == 0x00 {
msg = fmt.Sprintf("Response = OK")
} else if r.FieldCount == 0xff {
msg = fmt.Sprintf("Response = ERROR")
} else {
msg = fmt.Sprintf("Response = ResultSet")
}
if r.AffectedRows > 0 {
msg = fmt.Sprintf("%s, Affected Rows = %d", msg, r.AffectedRows)
}
if r.InsertId > 0 {
msg = fmt.Sprintf("%s, Insert Id = %d", msg, r.InsertId)
}
msg = fmt.Sprintf("%s, Server Status = %x", msg, r.ServerStatus)
if r.WarningCount > 0 {
msg = fmt.Sprintf("%s, Warnings = %x", msg, r.WarningCount)
}
if len(r.Message) > 0 {
msg = fmt.Sprintf("%s, Message = %s", msg, r.Message)
}
return msg
}
// This is terrible should return a interface or something instead of converting to strings.
func readFieldData(br *bufio.Reader, f *MySQLField) (string, bool, error) {
switch f.Type {
case MYSQL_TYPE_TINY:
var l int8
err := binary.Read(br, binary.LittleEndian, &l)
return fmt.Sprintf("%d", l), false, err
case MYSQL_TYPE_SHORT:
var l int16
err := binary.Read(br, binary.LittleEndian, &l)
return fmt.Sprintf("%d", l), false, err
case MYSQL_TYPE_LONG:
var l int32
err := binary.Read(br, binary.LittleEndian, &l)
return fmt.Sprintf("%d", l), false, err
case MYSQL_TYPE_LONGLONG:
var l int64
err := binary.Read(br, binary.LittleEndian, &l)
return fmt.Sprintf("%d", l), false, err
case MYSQL_TYPE_FLOAT:
var f float32
err := binary.Read(br, binary.LittleEndian, &f)
return fmt.Sprintf("%f", f), false, err
case MYSQL_TYPE_DOUBLE:
var f float64
err := binary.Read(br, binary.LittleEndian, &f)
return fmt.Sprintf("%f", f), false, err
case MYSQL_TYPE_VAR_STRING:
return unpackString(br)
case MYSQL_TYPE_STRING:
return unpackString(br)
case MYSQL_TYPE_BLOB:
return unpackString(br)
case MYSQL_TYPE_DATETIME:
dt, err := unpackDateTime(br)
return fmt.Sprintf("%s", dt), false, err
case MYSQL_TYPE_DATE:
dt, err := unpackDate(br)
return fmt.Sprintf("%s", dt), false, err
case MYSQL_TYPE_TIME:
dt, err := unpackTime(br)
return fmt.Sprintf("%s", dt), false, err
}
log.Printf("Unknown type = %s\n", f.Type)
return "NULL", true, nil
}
func unpackDate(br *bufio.Reader) (dt time.Time, err error) {
var y uint16
var M, d, n uint8
err = binary.Read(br, binary.LittleEndian, &n)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &y)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &M)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &d)
if err != nil {
return
}
dt = time.Date(int(y), time.Month(M), int(d), 0, 0, 0, 0, time.UTC)
return
}
func unpackTime(br *bufio.Reader) (dt time.Time, err error) {
var h, m, s uint8
err = ignoreBytes(br, 6)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &h)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &m)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &s)
if err != nil {
return
}
dt = time.Date(0, 0, 0, int(h), int(m), int(s), 0, time.UTC)
return
}
func unpackDateTime(br *bufio.Reader) (dt time.Time, err error) {
var y uint16
var M, d, h, m, s, n uint8
err = binary.Read(br, binary.LittleEndian, &n)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &y)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &M)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &d)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &h)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &m)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &s)
if err != nil {
return
}
dt = time.Date(int(y), time.Month(M), int(d), int(h), int(m), int(s), 0, time.UTC)
return
}
type MySQLField struct {
Catalog string
Db string
Table string
OrgTable string
Name string
OrgName string
Charset uint16
Length uint32
Type uint8
Flags uint16
Decimals uint8
Default uint64
}
func (f *MySQLField) String() string { return f.Name }
type MySQLData struct {
Data string
Length uint64
IsNull bool
Type uint8
}
func (d *MySQLData) String() string {
if d.IsNull {
return "NULL"
}
return string(d.Data)
}
type MySQLRow struct {
Data []*MySQLData
}