@@ -75,48 +75,37 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
7575}
7676
7777// Write packet buffer 'data'
78- // The packet header must be already included
7978func (mc * mysqlConn ) writePacket (data []byte ) error {
80- if len (data )- 4 <= mc .maxWriteSize { // Can send data at once
81- // Write packet
82- n , err := mc .netConn .Write (data )
83- if err == nil && n == len (data ) {
84- mc .sequence ++
85- return nil
86- }
87-
88- // Handle error
89- if err == nil { // n != len(data)
90- errLog .Print (errMalformPkt .Error ())
91- } else {
92- errLog .Print (err .Error ())
93- }
94- return driver .ErrBadConn
95- }
96-
97- // Must split packet
98- return mc .splitPacket (data )
99- }
100-
101- func (mc * mysqlConn ) splitPacket (data []byte ) error {
10279 pktLen := len (data ) - 4
10380
10481 if pktLen > mc .maxPacketAllowed {
10582 return errPktTooLarge
10683 }
10784
108- for pktLen >= maxPacketSize {
109- data [0 ] = 0xff
110- data [1 ] = 0xff
111- data [2 ] = 0xff
85+ for {
86+ var size int
87+ if pktLen >= maxPacketSize {
88+ data [0 ] = 0xff
89+ data [1 ] = 0xff
90+ data [2 ] = 0xff
91+ size = maxPacketSize
92+ } else {
93+ data [0 ] = byte (pktLen )
94+ data [1 ] = byte (pktLen >> 8 )
95+ data [2 ] = byte (pktLen >> 16 )
96+ size = pktLen
97+ }
11298 data [3 ] = mc .sequence
11399
114100 // Write packet
115- n , err := mc .netConn .Write (data [:4 + maxPacketSize ])
116- if err == nil && n == 4 + maxPacketSize {
101+ n , err := mc .netConn .Write (data [:4 + size ])
102+ if err == nil && n == 4 + size {
117103 mc .sequence ++
118- data = data [maxPacketSize :]
119- pktLen -= maxPacketSize
104+ if size != maxPacketSize {
105+ return nil
106+ }
107+ pktLen -= size
108+ data = data [size :]
120109 continue
121110 }
122111
@@ -128,12 +117,6 @@ func (mc *mysqlConn) splitPacket(data []byte) error {
128117 }
129118 return driver .ErrBadConn
130119 }
131-
132- data [0 ] = byte (pktLen )
133- data [1 ] = byte (pktLen >> 8 )
134- data [2 ] = byte (pktLen >> 16 )
135- data [3 ] = mc .sequence
136- return mc .writePacket (data )
137120}
138121
139122/******************************************************************************
@@ -265,12 +248,6 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
265248 // SSL Connection Request Packet
266249 // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
267250 if mc .cfg .tls != nil {
268- // Packet header [24bit length + 1 byte sequence]
269- data [0 ] = byte ((4 + 4 + 1 + 23 ))
270- data [1 ] = byte ((4 + 4 + 1 + 23 ) >> 8 )
271- data [2 ] = byte ((4 + 4 + 1 + 23 ) >> 16 )
272- data [3 ] = mc .sequence
273-
274251 // Send TLS / SSL request packet
275252 if err := mc .writePacket (data [:(4 + 4 + 1 + 23 )+ 4 ]); err != nil {
276253 return err
@@ -285,12 +262,6 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
285262 mc .buf .rd = tlsConn
286263 }
287264
288- // Add the packet header [24bit length + 1 byte sequence]
289- data [0 ] = byte (pktLen )
290- data [1 ] = byte (pktLen >> 8 )
291- data [2 ] = byte (pktLen >> 16 )
292- data [3 ] = mc .sequence
293-
294265 // Filler [23 bytes] (all 0x00)
295266 pos := 13 + 23
296267
@@ -330,12 +301,6 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
330301 return driver .ErrBadConn
331302 }
332303
333- // Add the packet header [24bit length + 1 byte sequence]
334- data [0 ] = byte (pktLen )
335- data [1 ] = byte (pktLen >> 8 )
336- data [2 ] = byte (pktLen >> 16 )
337- data [3 ] = mc .sequence
338-
339304 // Add the scrambled password [null terminated string]
340305 copy (data [4 :], scrambleBuff )
341306
@@ -357,12 +322,6 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
357322 return driver .ErrBadConn
358323 }
359324
360- // Add the packet header [24bit length + 1 byte sequence]
361- data [0 ] = 0x01 // 1 byte long
362- data [1 ] = 0x00
363- data [2 ] = 0x00
364- data [3 ] = 0x00 // new command, sequence id is always 0
365-
366325 // Add command byte
367326 data [4 ] = command
368327
@@ -382,12 +341,6 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
382341 return driver .ErrBadConn
383342 }
384343
385- // Add the packet header [24bit length + 1 byte sequence]
386- data [0 ] = byte (pktLen )
387- data [1 ] = byte (pktLen >> 8 )
388- data [2 ] = byte (pktLen >> 16 )
389- data [3 ] = 0x00 // new command, sequence id is always 0
390-
391344 // Add command byte
392345 data [4 ] = command
393346
@@ -409,12 +362,6 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
409362 return driver .ErrBadConn
410363 }
411364
412- // Add the packet header [24bit length + 1 byte sequence]
413- data [0 ] = 0x05 // 5 bytes long
414- data [1 ] = 0x00
415- data [2 ] = 0x00
416- data [3 ] = 0x00 // new command, sequence id is always 0
417-
418365 // Add command byte
419366 data [4 ] = command
420367
@@ -748,12 +695,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
748695 pktLen = dataOffset + argLen
749696 }
750697
751- // Add the packet header [24bit length + 1 byte sequence]
752- data [0 ] = byte (pktLen )
753- data [1 ] = byte (pktLen >> 8 )
754- data [2 ] = byte (pktLen >> 16 )
755- data [3 ] = 0x00 // mc.sequence
756-
698+ stmt .mc .sequence = 0
757699 // Add command byte [1 byte]
758700 data [4 ] = comStmtSendLongData
759701
@@ -801,28 +743,14 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
801743 var data []byte
802744
803745 if len (args ) == 0 {
804- const pktLen = 1 + 4 + 1 + 4
805- data = mc .buf .takeBuffer (4 + pktLen )
806- if data == nil {
807- // can not take the buffer. Something must be wrong with the connection
808- errLog .Print ("Busy buffer" )
809- return driver .ErrBadConn
810- }
811-
812- // packet header [4 bytes]
813- data [0 ] = byte (pktLen )
814- data [1 ] = byte (pktLen >> 8 )
815- data [2 ] = byte (pktLen >> 16 )
816- data [3 ] = 0x00 // new command, sequence id is always 0
746+ data = mc .buf .takeBuffer (4 + 1 + 4 + 1 + 4 )
817747 } else {
818748 data = mc .buf .takeCompleteBuffer ()
819- if data == nil {
820- // can not take the buffer. Something must be wrong with the connection
821- errLog .Print ("Busy buffer" )
822- return driver .ErrBadConn
823- }
824-
825- // header (bytes 0-3) is added after we know the packet size
749+ }
750+ if data == nil {
751+ // can not take the buffer. Something must be wrong with the connection
752+ errLog .Print ("Busy buffer" )
753+ return driver .ErrBadConn
826754 }
827755
828756 // command [1 byte]
@@ -984,14 +912,6 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
984912 pos += len (paramValues )
985913 data = data [:pos ]
986914
987- pktLen := pos - 4
988-
989- // packet header [4 bytes]
990- data [0 ] = byte (pktLen )
991- data [1 ] = byte (pktLen >> 8 )
992- data [2 ] = byte (pktLen >> 16 )
993- data [3 ] = mc .sequence
994-
995915 // Convert nullMask to bytes
996916 for i , max := 0 , (stmt .paramCount + 7 )>> 3 ; i < max ; i ++ {
997917 data [i + 14 ] = byte (nullMask >> uint (i << 3 ))
0 commit comments