@@ -238,12 +238,12 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro
238
238
pos += 1 + 2
239
239
240
240
// capability flags (upper 2 bytes) [2 bytes]
241
- mc .flags + = clientFlag (binary .LittleEndian .Uint16 (data [pos :pos + 2 ])) << 16
241
+ mc .flags | = clientFlag (binary .LittleEndian .Uint16 (data [pos :pos + 2 ])) << 16
242
242
pos += 2
243
243
244
244
// length of auth-plugin-data [1 byte]
245
245
// reserved (all [00]) [10 bytes]
246
- pos += + 1 + 10
246
+ pos += 1 + 10
247
247
248
248
// second part of the password cipher [mininum 13 bytes],
249
249
// where len=MAX(13, length of auth-plugin-data - 8)
@@ -614,7 +614,7 @@ func readStatus(b []byte) statusFlag {
614
614
}
615
615
616
616
// Ok Packet
617
- // http ://dev.mysql.com/doc/internals/en/generic-response-packets.html# packet-OK_Packet
617
+ // https ://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
618
618
func (mc * mysqlConn ) handleOkPacket (data []byte ) error {
619
619
// 0x00 or 0xFE [1 byte]
620
620
n := 1
@@ -640,22 +640,34 @@ func (mc *mysqlConn) handleOkPacket(data []byte) error {
640
640
641
641
// isEOFPacket will return true if the data is either a EOF-Packet or OK-Packet
642
642
// acting as an EOF.
643
- func isEOFPacket (data []byte ) bool {
644
- return data [0 ] == iEOF && len (data ) < 9
643
+ func (mc * mysqlConn ) isEOFPacket (data []byte ) bool {
644
+ // Legacy EOF packet
645
+ if data [0 ] == iEOF && (len (data ) == 5 || len (data ) == 1 ) && mc .flags & clientDeprecateEOF == 0 {
646
+ return true
647
+ }
648
+ return data [0 ] == iEOF && len (data ) < 9 && mc .flags & clientDeprecateEOF != 0
645
649
}
646
650
647
651
// Read Packets as Field Packets until EOF-Packet or an Error appears
648
652
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
649
653
func (mc * mysqlConn ) readColumns (count int ) ([]mysqlField , error ) {
650
654
columns := make ([]mysqlField , count )
651
655
652
- for i := 0 ; i < count ; i ++ {
656
+ // If we set clientDeprecateEOF capability flag,
657
+ // the EOF will be no longer sent after all columns.
658
+ packets := count
659
+ if mc .flags & clientDeprecateEOF == 0 {
660
+ // Legacy way, read one more EOF packet.
661
+ packets += 1
662
+ }
663
+
664
+ for i := 0 ; i < packets ; i ++ {
653
665
data , err := mc .readPacket ()
654
666
if err != nil {
655
667
return nil , err
656
668
}
657
669
658
- if mc .flags & clientDeprecateEOF == 0 && isEOFPacket (data ) {
670
+ if mc .isEOFPacket (data ) {
659
671
if i == count {
660
672
return columns , nil
661
673
}
@@ -759,12 +771,13 @@ func (rows *textRows) readRow(dest []driver.Value) error {
759
771
}
760
772
761
773
// EOF Packet
762
- if isEOFPacket (data ) {
774
+ if mc . isEOFPacket (data ) {
763
775
if mc .flags & clientDeprecateEOF == 0 {
764
776
// server_status [2 bytes]
765
777
rows .mc .status = readStatus (data [3 :])
766
778
} else {
767
779
if err := mc .handleOkPacket (data ); err != nil {
780
+ rows .mc = nil
768
781
return err
769
782
}
770
783
}
@@ -830,37 +843,22 @@ func (mc *mysqlConn) readUntilEOF() error {
830
843
switch {
831
844
case data [0 ] == iERR :
832
845
return mc .handleErrorPacket (data )
833
- case isEOFPacket (data ):
846
+ case mc . isEOFPacket (data ):
834
847
if mc .flags & clientDeprecateEOF == 0 {
835
848
mc .status = readStatus (data [3 :])
836
- } else {
837
- return mc .handleOkPacket (data )
849
+ return nil
838
850
}
839
- return nil
851
+ return mc . handleOkPacket ( data )
840
852
}
841
853
}
842
854
}
843
855
844
- func (mc * mysqlConn ) readPackets (num int ) error {
845
-
846
- // we need to read EOF as well
847
- if mc .flags & clientDeprecateEOF == 0 {
848
- num ++
849
- }
850
-
856
+ func (mc * mysqlConn ) readExactPackets (num int ) error {
851
857
for i := 0 ; i < num ; i ++ {
852
- data , err := mc .readPacket ()
858
+ _ , err := mc .readPacket ()
853
859
if err != nil {
854
860
return err
855
861
}
856
-
857
- switch {
858
- case data [0 ] == iERR :
859
- return mc .handleErrorPacket (data )
860
- case mc .flags & clientDeprecateEOF == 0 && isEOFPacket (data ):
861
- mc .status = readStatus (data [3 :])
862
- return nil
863
- }
864
862
}
865
863
return nil
866
864
}
@@ -1223,11 +1221,12 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
1223
1221
1224
1222
// packet indicator [1 byte]
1225
1223
if data [0 ] != iOK {
1226
- if isEOFPacket (data ) {
1224
+ if rows . mc . isEOFPacket (data ) {
1227
1225
if rows .mc .flags & clientDeprecateEOF == 0 {
1228
1226
rows .mc .status = readStatus (data [3 :])
1229
1227
} else {
1230
1228
if err := rows .mc .handleOkPacket (data ); err != nil {
1229
+ rows .mc = nil
1231
1230
return err
1232
1231
}
1233
1232
}
0 commit comments