-
Notifications
You must be signed in to change notification settings - Fork 37
/
handler.go
372 lines (362 loc) · 9.05 KB
/
handler.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// GoMySQL - A MySQL client library for Go
//
// Copyright 2010-2011 Phil Bayfield. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysql
import (
"os"
"strconv"
)
// OK packet handler
func handleOK(p *packetOK, c *Client, a, i *uint64, w *uint16) (err os.Error) {
// Log OK result
c.log(1, "[%d] Received OK packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Store packet data
*a = p.affectedRows
*i = p.insertId
*w = p.warningCount
c.serverStatus = ServerStatus(p.serverStatus)
// Full logging [level 3]
if c.LogLevel > 2 {
c.logStatus()
}
return
}
// Error packet handler
func handleError(p *packetError, c *Client) (err os.Error) {
// Log error result
c.log(1, "[%d] Received error packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Check and unset more results flag
// @todo maybe serverStatus should just be zeroed?
if c.MoreResults() {
c.serverStatus ^= SERVER_MORE_RESULTS_EXISTS
}
// Return error
return &ServerError{Errno(p.errno), Error(p.error)}
}
// EOF packet handler
func handleEOF(p *packetEOF, c *Client) (err os.Error) {
// Log EOF result
c.log(1, "[%d] Received EOF packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Store packet data
if p.useStatus {
c.serverStatus = ServerStatus(p.serverStatus)
// Full logging [level 3]
if c.LogLevel > 2 {
c.logStatus()
}
}
return
}
// Result set packet handler
func handleResultSet(p *packetResultSet, c *Client, r *Result) (err os.Error) {
// Log error result
c.log(1, "[%d] Received result set packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Assign field count
r.fieldCount = p.fieldCount
return
}
// Field packet handler
func handleField(p *packetField, c *Client, r *Result) (err os.Error) {
// Log field result
c.log(1, "[%d] Received field packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Check if packet needs to be stored
if r == nil || r.mode == RESULT_FREE {
return
}
// Apppend new field
r.fields = append(r.fields, &Field{
Database: p.database,
Table: p.table,
Name: p.name,
Length: p.length,
Type: FieldType(p.fieldType),
Flags: FieldFlag(p.flags),
Decimals: p.decimals,
})
return
}
// Row packet hander
func handleRow(p *packetRowData, c *Client, r *Result) (err os.Error) {
// Log field result
c.log(1, "[%d] Received row packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Check if there is a result set
if r == nil || r.mode == RESULT_FREE {
return
}
// Basic type conversion
var row []interface{}
var field interface{}
// Iterate fields to get types
for i, f := range r.fields {
// Check null
if len(p.row[i].([]byte)) ==0 {
field = nil
} else {
switch f.Type {
// Signed/unsigned ints
case FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_YEAR, FIELD_TYPE_INT24, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG:
if f.Flags&FLAG_UNSIGNED > 0 {
field, err = strconv.Atoui64(string(p.row[i].([]byte)))
} else {
field, err = strconv.Atoi64(string(p.row[i].([]byte)))
}
if err != nil {
return
}
// Floats and doubles
case FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
field, err = strconv.Atof64(string(p.row[i].([]byte)))
if err != nil {
return
}
// Strings
case FIELD_TYPE_DECIMAL, FIELD_TYPE_NEWDECIMAL, FIELD_TYPE_VARCHAR, FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING:
field = string(p.row[i].([]byte))
// Anything else
default:
field = p.row[i]
}
}
// Add to row
row = append(row, field)
}
// Stored result
if r.mode == RESULT_STORED {
// Cast and append the row
r.rows = append(r.rows, Row(row))
}
// Used result
if r.mode == RESULT_USED {
// Only save 1 row, overwrite previous
r.rows = []Row{Row(row)}
}
return
}
// Prepare OK packet handler
func handlePrepareOK(p *packetPrepareOK, c *Client, s *Statement) (err os.Error) {
// Log result
c.log(1, "[%d] Received prepare OK packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Store packet data
s.statementId = p.statementId
s.paramCount = p.paramCount
s.columnCount = uint64(p.columnCount)
s.Warnings = p.warningCount
return
}
// Parameter packet handler
func handleParam(p *packetParameter, c *Client) (err os.Error) {
// Log result
c.log(1, "[%d] Received parameter packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// @todo at some point implement this properly if any versions of MySQL are doing so
return
}
// Binary row packet handler
func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err os.Error) {
// Log binary row result
c.log(1, "[%d] Received binary row packet", p.sequence)
// Check sequence
err = c.checkSequence(p.sequence)
if err != nil {
return
}
// Check if there is a result set
if r == nil || r.mode == RESULT_FREE {
return
}
// Read data into fields
var row []interface{}
var field interface{}
// Get null bit map
nc := (r.fieldCount + 9) / 8
nbm := p.data[1 : nc+1]
pos := nc + 1
for i, f := range r.fields {
// Check if field is null
posByte := (i + 2) / 8
posBit := i - (posByte * 8) + 2
if nbm[posByte]&(1<<uint8(posBit)) != 0 {
field = nil
continue
}
// Otherwise use field type
switch f.Type {
// Tiny int (8 bit int unsigned or signed)
case FIELD_TYPE_TINY:
if f.Flags&FLAG_UNSIGNED > 0 {
field = uint64(p.data[pos])
} else {
field = int64(p.data[pos])
}
pos++
// Small int (16 bit int unsigned or signed)
case FIELD_TYPE_SHORT, FIELD_TYPE_YEAR:
if f.Flags&FLAG_UNSIGNED > 0 {
field = uint64(btoui16(p.data[pos : pos+2]))
} else {
field = int64(btoi16(p.data[pos : pos+2]))
}
pos += 2
// Int (32 bit int unsigned or signed) and medium int which is actually in int32 format
case FIELD_TYPE_LONG, FIELD_TYPE_INT24:
if f.Flags&FLAG_UNSIGNED > 0 {
field = uint64(btoui32(p.data[pos : pos+4]))
} else {
field = int64(btoi32(p.data[pos : pos+4]))
}
pos += 4
// Big int (64 bit int unsigned or signed)
case FIELD_TYPE_LONGLONG:
if f.Flags&FLAG_UNSIGNED > 0 {
field = btoui64(p.data[pos : pos+8])
} else {
field = btoi64(p.data[pos : pos+8])
}
pos += 8
// Floats (Single precision floating point, 32 bit signed)
case FIELD_TYPE_FLOAT:
field = btof32(p.data[pos : pos+4])
pos += 4
// Double (Double precision floating point, 64 bit signed)
case FIELD_TYPE_DOUBLE:
field = btof64(p.data[pos : pos+8])
pos += 8
// Bit, decimal, strings, blobs etc, all length coded binary strings
case FIELD_TYPE_BIT, FIELD_TYPE_DECIMAL, FIELD_TYPE_NEWDECIMAL, FIELD_TYPE_VARCHAR,
FIELD_TYPE_TINY_BLOB, FIELD_TYPE_MEDIUM_BLOB, FIELD_TYPE_LONG_BLOB, FIELD_TYPE_BLOB,
FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_GEOMETRY:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
}
field = p.data[pos+uint64(n) : pos+uint64(n)+num]
pos += uint64(n) + num
// Date (From libmysql/libmysql.c read_binary_datetime)
case FIELD_TYPE_DATE:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
}
// New date
d := Date{}
// Check zero
if num == 0 {
field = d
pos++
break
}
// Year 2 bytes
d.Year = btoui16(p.data[pos+uint64(n) : pos+uint64(n)+2])
// Month 1 byte
d.Month = p.data[pos+uint64(n)+2]
// Day 1 byte
d.Day = p.data[pos+uint64(n)+3]
field = d
pos += uint64(n) + num
// Time (From libmysql/libmysql.c read_binary_time)
case FIELD_TYPE_TIME:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
}
// New time
t := Time{}
// Default zero values
if num == 0 {
field = t
pos++
break
}
// Hour 1 byte
t.Hour = p.data[pos+6]
// Minute 1 byte
t.Minute = p.data[pos+7]
// Second 1 byte
t.Second = p.data[pos+8]
field = t
pos += uint64(n) + num
// Datetime/Timestamp (From libmysql/libmysql.c read_binary_datetime)
case FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
}
// New datetime
d := DateTime{}
// Check zero
if num == 0 {
field = d
pos++
break
}
// Year 2 bytes
d.Year = btoui16(p.data[pos+uint64(n) : pos+uint64(n)+2])
// Month 1 byte
d.Month = p.data[pos+uint64(n)+2]
// Day 1 byte
d.Day = p.data[pos+uint64(n)+3]
// Hour 1 byte
d.Hour = p.data[pos+uint64(n)+4]
// Minute 1 byte
d.Minute = p.data[pos+uint64(n)+5]
// Second 1 byte
d.Second = p.data[pos+uint64(n)+6]
field = d
pos += uint64(n) + num
}
// Add to row
row = append(row, field)
}
// Stored result
if r.mode == RESULT_STORED {
// Cast and append the row
r.rows = append(r.rows, Row(row))
}
// Used result
if r.mode == RESULT_USED {
// Only save 1 row, overwrite previous
r.rows = []Row{Row(row)}
}
return
}