@@ -360,7 +360,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
360
360
data [0 ] = 0x01 // 1 byte long
361
361
data [1 ] = 0x00
362
362
data [2 ] = 0x00
363
- data [3 ] = 0x00 // sequence is always 0
363
+ data [3 ] = 0x00 // new command, sequence id is always 0
364
364
365
365
// Add command byte
366
366
data [4 ] = command
@@ -385,7 +385,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
385
385
data [0 ] = byte (pktLen )
386
386
data [1 ] = byte (pktLen >> 8 )
387
387
data [2 ] = byte (pktLen >> 16 )
388
- data [3 ] = 0x00 // sequence is always 0
388
+ data [3 ] = 0x00 // new command, sequence id is always 0
389
389
390
390
// Add command byte
391
391
data [4 ] = command
@@ -412,7 +412,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
412
412
data [0 ] = 0x05 // 5 bytes long
413
413
data [1 ] = 0x00
414
414
data [2 ] = 0x00
415
- data [3 ] = 0x00 // sequence is always 0
415
+ data [3 ] = 0x00 // new command, sequence id is always 0
416
416
417
417
// Add command byte
418
418
data [4 ] = command
@@ -495,8 +495,8 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
495
495
pos := 3
496
496
497
497
// SQL State [optional: # + 5bytes string]
498
- //sqlstate := string(data[pos : pos+6])
499
498
if data [3 ] == 0x23 {
499
+ //sqlstate := string(data[4 : 4+5])
500
500
pos = 9
501
501
}
502
502
@@ -700,13 +700,13 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
700
700
}
701
701
702
702
// statement id [4 bytes]
703
- stmt .id = binary .LittleEndian .Uint32 (data [1 : 1 + 4 ])
703
+ stmt .id = binary .LittleEndian .Uint32 (data [1 : 5 ])
704
704
705
705
// Column count [16 bit uint]
706
- columnCount := binary .LittleEndian .Uint16 (data [1 + 4 : 1 + 4 + 2 ])
706
+ columnCount := binary .LittleEndian .Uint16 (data [5 : 7 ])
707
707
708
708
// Param count [16 bit uint]
709
- stmt .paramCount = int (binary .LittleEndian .Uint16 (data [1 + 4 + 2 : 1 + 4 + 2 + 2 ]))
709
+ stmt .paramCount = int (binary .LittleEndian .Uint16 (data [7 : 9 ]))
710
710
711
711
// Reserved [8 bit]
712
712
@@ -715,7 +715,7 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
715
715
return columnCount , nil
716
716
} else {
717
717
// Check for warnings count > 0, only available in MySQL > 4.1
718
- if len (data ) >= 12 && binary .LittleEndian .Uint16 (data [1 + 4 + 2 + 2 + 1 : 1 + 4 + 2 + 2 + 1 + 2 ]) > 0 {
718
+ if len (data ) >= 12 && binary .LittleEndian .Uint16 (data [10 : 12 ]) > 0 {
719
719
return columnCount , stmt .mc .getWarnings ()
720
720
}
721
721
return columnCount , nil
@@ -729,16 +729,22 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
729
729
maxLen := stmt .mc .maxPacketAllowed - 1
730
730
pktLen := maxLen
731
731
732
+ // After the header (bytes 0-3) follows before the data:
733
+ // 1 byte command
734
+ // 4 bytes stmtID
735
+ // 2 bytes paramID
736
+ const dataOffset = 1 + 4 + 2
737
+
732
738
// Can not use the write buffer since
733
739
// a) the buffer is too small
734
740
// b) it is in use
735
741
data := make ([]byte , 4 + 1 + 4 + 2 + len (arg ))
736
742
737
- copy (data [4 + 1 + 4 + 2 :], arg )
743
+ copy (data [4 + dataOffset :], arg )
738
744
739
- for argLen := len (arg ); argLen > 0 ; argLen -= pktLen - ( 1 + 4 + 2 ) {
740
- if 1 + 4 + 2 + argLen < maxLen {
741
- pktLen = 1 + 4 + 2 + argLen
745
+ for argLen := len (arg ); argLen > 0 ; argLen -= pktLen - dataOffset {
746
+ if dataOffset + argLen < maxLen {
747
+ pktLen = dataOffset + argLen
742
748
}
743
749
744
750
// Add the packet header [24bit length + 1 byte sequence]
@@ -763,7 +769,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
763
769
// Send CMD packet
764
770
err := stmt .mc .writePacket (data [:4 + pktLen ])
765
771
if err == nil {
766
- data = data [pktLen - ( 1 + 4 + 2 ) :]
772
+ data = data [pktLen - dataOffset :]
767
773
continue
768
774
}
769
775
return err
@@ -806,7 +812,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
806
812
data [0 ] = byte (pktLen )
807
813
data [1 ] = byte (pktLen >> 8 )
808
814
data [2 ] = byte (pktLen >> 16 )
809
- data [3 ] = 0x00 // sequence is always 0
815
+ data [3 ] = 0x00 // new command, sequence id is always 0
810
816
} else {
811
817
data = mc .buf .takeCompleteBuffer ()
812
818
if data == nil {
@@ -871,7 +877,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
871
877
872
878
if cap (paramValues )- len (paramValues )- 8 >= 0 {
873
879
paramValues = paramValues [:len (paramValues )+ 8 ]
874
- binary .LittleEndian .PutUint64 (paramValues [len (paramValues )- 8 :], uint64 (v ))
880
+ binary .LittleEndian .PutUint64 (
881
+ paramValues [len (paramValues )- 8 :],
882
+ uint64 (v ),
883
+ )
875
884
} else {
876
885
paramValues = append (paramValues ,
877
886
uint64ToBytes (uint64 (v ))... ,
@@ -884,7 +893,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
884
893
885
894
if cap (paramValues )- len (paramValues )- 8 >= 0 {
886
895
paramValues = paramValues [:len (paramValues )+ 8 ]
887
- binary .LittleEndian .PutUint64 (paramValues [len (paramValues )- 8 :], math .Float64bits (v ))
896
+ binary .LittleEndian .PutUint64 (
897
+ paramValues [len (paramValues )- 8 :],
898
+ math .Float64bits (v ),
899
+ )
888
900
} else {
889
901
paramValues = append (paramValues ,
890
902
uint64ToBytes (math .Float64bits (v ))... ,
@@ -991,10 +1003,10 @@ func (rows *mysqlRows) readBinaryRow(dest []driver.Value) error {
991
1003
// EOF Packet
992
1004
if data [0 ] == iEOF && len (data ) == 5 {
993
1005
return io .EOF
994
- } else {
995
- // Error otherwise
996
- return rows .mc .handleErrorPacket (data )
997
1006
}
1007
+
1008
+ // Error otherwise
1009
+ return rows .mc .handleErrorPacket (data )
998
1010
}
999
1011
1000
1012
// NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
0 commit comments