Skip to content

Commit d43e080

Browse files
committed
multiple statements error msg
Show a more informative error message when multiple statements are executed at once
1 parent c1eca66 commit d43e080

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

packets.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Copyright 2012 Julien Schmidt. All rights reserved.
44
// http://www.julienschmidt.com
5-
//
5+
//
66
// This Source Code Form is subject to the terms of the Mozilla Public
77
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
88
// You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -41,7 +41,11 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
4141

4242
// Check Packet Sync
4343
if uint8(pktSeq) != mc.sequence {
44-
e = errors.New("Commands out of sync; you can't run this command now")
44+
if uint8(pktSeq) > mc.sequence {
45+
e = errors.New("Commands out of sync. Did you run multiple statements at once?")
46+
} else {
47+
e = errors.New("Commands out of sync; you can't run this command now")
48+
}
4549
return nil, e
4650
}
4751
mc.sequence++
@@ -113,7 +117,7 @@ func (mc *mysqlConn) writePacket(data *[]byte) error {
113117
* Initialisation Process *
114118
******************************************************************************/
115119

116-
/* Handshake Initialization Packet
120+
/* Handshake Initialization Packet
117121
Bytes Name
118122
----- ----
119123
1 protocol_version
@@ -127,7 +131,7 @@ func (mc *mysqlConn) writePacket(data *[]byte) error {
127131
2 server capabilities (two upper bytes)
128132
1 length of the scramble
129133
10 (filler) always 0
130-
n rest of the plugin provided data (at least 12 bytes)
134+
n rest of the plugin provided data (at least 12 bytes)
131135
1 \0 byte, terminating the second part of a scramble
132136
*/
133137
func (mc *mysqlConn) readInitPacket() (e error) {
@@ -187,7 +191,7 @@ func (mc *mysqlConn) readInitPacket() (e error) {
187191
return
188192
}
189193

190-
/* Client Authentication Packet
194+
/* Client Authentication Packet
191195
Bytes Name
192196
----- ----
193197
4 client_flags
@@ -355,7 +359,7 @@ func (mc *mysqlConn) readResultOK() (e error) {
355359
return
356360
}
357361

358-
/* Error Packet
362+
/* Error Packet
359363
Bytes Name
360364
----- ----
361365
1 field_count, always = 0xff
@@ -387,7 +391,7 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) (e error) {
387391
return
388392
}
389393

390-
/* Ok Packet
394+
/* Ok Packet
391395
Bytes Name
392396
----- ----
393397
1 (Length Coded Binary) field_count, always = 0
@@ -427,13 +431,13 @@ func (mc *mysqlConn) handleOkPacket(data []byte) (e error) {
427431
return
428432
}
429433

430-
/* Result Set Header Packet
434+
/* Result Set Header Packet
431435
Bytes Name
432436
----- ----
433437
1-9 (Length-Coded-Binary) field_count
434438
1-9 (Length-Coded-Binary) extra
435439
436-
The order of packets for a result set is:
440+
The order of packets for a result set is:
437441
(Result Set Header Packet) the number of columns
438442
(Field Packets) column descriptors
439443
(EOF Packet) marker: end of Field Packets
@@ -630,14 +634,14 @@ func (mc *mysqlConn) readUntilEOF() (count uint64, e error) {
630634
* Prepared Statements *
631635
******************************************************************************/
632636

633-
/* Prepare Result Packets
637+
/* Prepare Result Packets
634638
Type Of Result Packet Hexadecimal Value Of First Byte (field_count)
635639
--------------------- ---------------------------------------------
636640
637641
Prepare OK Packet 00
638642
Error Packet ff
639643
640-
Prepare OK Packet
644+
Prepare OK Packet
641645
Bytes Name
642646
----- ----
643647
1 0 - marker for OK packet
@@ -652,10 +656,10 @@ Prepare OK Packet
652656
a PREPARE_OK packet
653657
if "number of parameters" > 0
654658
(field packets) as in a Result Set Header Packet
655-
(EOF packet)
659+
(EOF packet)
656660
if "number of columns" > 0
657661
(field packets) as in a Result Set Header Packet
658-
(EOF packet)
662+
(EOF packet)
659663
660664
*/
661665
func (stmt mysqlStmt) readPrepareResultPacket() (columnCount uint16, e error) {
@@ -702,7 +706,7 @@ Bytes Name
702706
1 new_parameter_bound_flag
703707
if new_params_bound == 1:
704708
n*2 type of parameters
705-
n values for the parameters
709+
n values for the parameters
706710
*/
707711
func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
708712
argsLen := len(*args)
@@ -723,7 +727,7 @@ func (stmt mysqlStmt) buildExecutePacket(args *[]driver.Value) (e error) {
723727
var i, valLen int
724728
var pv reflect.Value
725729
for i = 0; i < stmt.paramCount; i++ {
726-
// build nullBitMap
730+
// build nullBitMap
727731
if (*args)[i] == nil {
728732
bitMask += 1 << uint(i)
729733
}

0 commit comments

Comments
 (0)