Skip to content

Commit 0d50d22

Browse files
committed
Fixed Issue mysqljs#2.
Thanks to chengw (at) garena.com for the fix!
1 parent 59d433e commit 0d50d22

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packets.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ func (mc *mysqlConn) readPacket() (data []byte, e error) {
4747

4848
// Read rest of packet
4949
data = make([]byte, pktLen)
50-
n, e := mc.netConn.Read(data)
50+
var n, add int
51+
n, e = mc.netConn.Read(data)
52+
53+
// Read conventionally returns what is available instead of waiting for more
54+
for e == nil && n < int(pktLen) {
55+
add, e = mc.netConn.Read(data[n:])
56+
n += add
57+
}
58+
5159
if e != nil || n != int(pktLen) {
5260
e = driver.ErrBadConn
5361
return
5462
}
55-
return
63+
return data[:pktLen], e // Return without scratch space
5664
}
5765

5866
// Send Packet with given data
@@ -275,20 +283,20 @@ func (mc *mysqlConn) writeCommandPacket(command commandType, args ...interface{}
275283
// Commands without args
276284
case COM_QUIT, COM_PING:
277285
if len(args) > 0 {
278-
return fmt.Errorf("Too much arguments (Got: %d Has:0)", len(args))
286+
return fmt.Errorf("Too much arguments (Got: %d Has: 0)", len(args))
279287
}
280288

281289
// Commands with 1 arg unterminated string
282290
case COM_QUERY, COM_STMT_PREPARE:
283291
if len(args) != 1 {
284-
return fmt.Errorf("Invalid arguments count (Got:%d Need: 1)", len(args))
292+
return fmt.Errorf("Invalid arguments count (Got: %d Need: 1)", len(args))
285293
}
286294
data = append(data, []byte(args[0].(string))...)
287295

288296
// Commands with 1 arg 32 bit uint
289297
case COM_STMT_CLOSE:
290298
if len(args) != 1 {
291-
return fmt.Errorf("Invalid arguments count (Got:%d Need: 1)", len(args))
299+
return fmt.Errorf("Invalid arguments count (Got: %d Need: 1)", len(args))
292300
}
293301
data = append(data, uint32ToBytes(args[0].(uint32))...)
294302
default:

0 commit comments

Comments
 (0)