Skip to content

Commit 228ba34

Browse files
committed
packets: YAR (yet another refactoring)
1 parent d8e6c38 commit 228ba34

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

packets.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
360360
data[0] = 0x01 // 1 byte long
361361
data[1] = 0x00
362362
data[2] = 0x00
363-
data[3] = 0x00 // sequence is always 0
363+
data[3] = 0x00 // new command, sequence id is always 0
364364

365365
// Add command byte
366366
data[4] = command
@@ -385,7 +385,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
385385
data[0] = byte(pktLen)
386386
data[1] = byte(pktLen >> 8)
387387
data[2] = byte(pktLen >> 16)
388-
data[3] = 0x00 // sequence is always 0
388+
data[3] = 0x00 // new command, sequence id is always 0
389389

390390
// Add command byte
391391
data[4] = command
@@ -412,7 +412,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
412412
data[0] = 0x05 // 5 bytes long
413413
data[1] = 0x00
414414
data[2] = 0x00
415-
data[3] = 0x00 // sequence is always 0
415+
data[3] = 0x00 // new command, sequence id is always 0
416416

417417
// Add command byte
418418
data[4] = command
@@ -495,8 +495,8 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
495495
pos := 3
496496

497497
// SQL State [optional: # + 5bytes string]
498-
//sqlstate := string(data[pos : pos+6])
499498
if data[3] == 0x23 {
499+
//sqlstate := string(data[4 : 4+5])
500500
pos = 9
501501
}
502502

@@ -700,13 +700,13 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
700700
}
701701

702702
// statement id [4 bytes]
703-
stmt.id = binary.LittleEndian.Uint32(data[1 : 1+4])
703+
stmt.id = binary.LittleEndian.Uint32(data[1:5])
704704

705705
// Column count [16 bit uint]
706-
columnCount := binary.LittleEndian.Uint16(data[1+4 : 1+4+2])
706+
columnCount := binary.LittleEndian.Uint16(data[5:7])
707707

708708
// 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]))
710710

711711
// Reserved [8 bit]
712712

@@ -715,7 +715,7 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
715715
return columnCount, nil
716716
} else {
717717
// 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 {
719719
return columnCount, stmt.mc.getWarnings()
720720
}
721721
return columnCount, nil
@@ -729,16 +729,22 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
729729
maxLen := stmt.mc.maxPacketAllowed - 1
730730
pktLen := maxLen
731731

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+
732738
// Can not use the write buffer since
733739
// a) the buffer is too small
734740
// b) it is in use
735741
data := make([]byte, 4+1+4+2+len(arg))
736742

737-
copy(data[4+1+4+2:], arg)
743+
copy(data[4+dataOffset:], arg)
738744

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
742748
}
743749

744750
// Add the packet header [24bit length + 1 byte sequence]
@@ -763,7 +769,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
763769
// Send CMD packet
764770
err := stmt.mc.writePacket(data[:4+pktLen])
765771
if err == nil {
766-
data = data[pktLen-(1+4+2):]
772+
data = data[pktLen-dataOffset:]
767773
continue
768774
}
769775
return err
@@ -806,7 +812,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
806812
data[0] = byte(pktLen)
807813
data[1] = byte(pktLen >> 8)
808814
data[2] = byte(pktLen >> 16)
809-
data[3] = 0x00 // sequence is always 0
815+
data[3] = 0x00 // new command, sequence id is always 0
810816
} else {
811817
data = mc.buf.takeCompleteBuffer()
812818
if data == nil {
@@ -871,7 +877,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
871877

872878
if cap(paramValues)-len(paramValues)-8 >= 0 {
873879
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+
)
875884
} else {
876885
paramValues = append(paramValues,
877886
uint64ToBytes(uint64(v))...,
@@ -884,7 +893,10 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
884893

885894
if cap(paramValues)-len(paramValues)-8 >= 0 {
886895
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+
)
888900
} else {
889901
paramValues = append(paramValues,
890902
uint64ToBytes(math.Float64bits(v))...,
@@ -991,10 +1003,10 @@ func (rows *mysqlRows) readBinaryRow(dest []driver.Value) error {
9911003
// EOF Packet
9921004
if data[0] == iEOF && len(data) == 5 {
9931005
return io.EOF
994-
} else {
995-
// Error otherwise
996-
return rows.mc.handleErrorPacket(data)
9971006
}
1007+
1008+
// Error otherwise
1009+
return rows.mc.handleErrorPacket(data)
9981010
}
9991011

10001012
// NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]

0 commit comments

Comments
 (0)