packets.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "bytes"
  11. "crypto/tls"
  12. "database/sql/driver"
  13. "encoding/binary"
  14. "errors"
  15. "fmt"
  16. "io"
  17. "math"
  18. "time"
  19. )
  20. // Packets documentation:
  21. // http://dev.mysql.com/doc/internals/en/client-server-protocol.html
  22. // Read packet to buffer 'data'
  23. func (mc *mysqlConn) readPacket() ([]byte, error) {
  24. var prevData []byte
  25. for {
  26. // read packet header
  27. data, err := mc.buf.readNext(4)
  28. if err != nil {
  29. if cerr := mc.canceled.Value(); cerr != nil {
  30. return nil, cerr
  31. }
  32. errLog.Print(err)
  33. mc.Close()
  34. return nil, ErrInvalidConn
  35. }
  36. // packet length [24 bit]
  37. pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
  38. // check packet sync [8 bit]
  39. if data[3] != mc.sequence {
  40. if data[3] > mc.sequence {
  41. return nil, ErrPktSyncMul
  42. }
  43. return nil, ErrPktSync
  44. }
  45. mc.sequence++
  46. // packets with length 0 terminate a previous packet which is a
  47. // multiple of (2^24)−1 bytes long
  48. if pktLen == 0 {
  49. // there was no previous packet
  50. if prevData == nil {
  51. errLog.Print(ErrMalformPkt)
  52. mc.Close()
  53. return nil, ErrInvalidConn
  54. }
  55. return prevData, nil
  56. }
  57. // read packet body [pktLen bytes]
  58. data, err = mc.buf.readNext(pktLen)
  59. if err != nil {
  60. if cerr := mc.canceled.Value(); cerr != nil {
  61. return nil, cerr
  62. }
  63. errLog.Print(err)
  64. mc.Close()
  65. return nil, ErrInvalidConn
  66. }
  67. // return data if this was the last packet
  68. if pktLen < maxPacketSize {
  69. // zero allocations for non-split packets
  70. if prevData == nil {
  71. return data, nil
  72. }
  73. return append(prevData, data...), nil
  74. }
  75. prevData = append(prevData, data...)
  76. }
  77. }
  78. // Write packet buffer 'data'
  79. func (mc *mysqlConn) writePacket(data []byte) error {
  80. pktLen := len(data) - 4
  81. if pktLen > mc.maxAllowedPacket {
  82. return ErrPktTooLarge
  83. }
  84. for {
  85. var size int
  86. if pktLen >= maxPacketSize {
  87. data[0] = 0xff
  88. data[1] = 0xff
  89. data[2] = 0xff
  90. size = maxPacketSize
  91. } else {
  92. data[0] = byte(pktLen)
  93. data[1] = byte(pktLen >> 8)
  94. data[2] = byte(pktLen >> 16)
  95. size = pktLen
  96. }
  97. data[3] = mc.sequence
  98. // Write packet
  99. if mc.writeTimeout > 0 {
  100. if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
  101. return err
  102. }
  103. }
  104. n, err := mc.netConn.Write(data[:4+size])
  105. if err == nil && n == 4+size {
  106. mc.sequence++
  107. if size != maxPacketSize {
  108. return nil
  109. }
  110. pktLen -= size
  111. data = data[size:]
  112. continue
  113. }
  114. // Handle error
  115. if err == nil { // n != len(data)
  116. mc.cleanup()
  117. errLog.Print(ErrMalformPkt)
  118. } else {
  119. if cerr := mc.canceled.Value(); cerr != nil {
  120. return cerr
  121. }
  122. if n == 0 && pktLen == len(data)-4 {
  123. // only for the first loop iteration when nothing was written yet
  124. return errBadConnNoWrite
  125. }
  126. mc.cleanup()
  127. errLog.Print(err)
  128. }
  129. return ErrInvalidConn
  130. }
  131. }
  132. /******************************************************************************
  133. * Initialization Process *
  134. ******************************************************************************/
  135. // Handshake Initialization Packet
  136. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
  137. func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err error) {
  138. data, err = mc.readPacket()
  139. if err != nil {
  140. // for init we can rewrite this to ErrBadConn for sql.Driver to retry, since
  141. // in connection initialization we don't risk retrying non-idempotent actions.
  142. if err == ErrInvalidConn {
  143. return nil, "", driver.ErrBadConn
  144. }
  145. return
  146. }
  147. if data[0] == iERR {
  148. return nil, "", mc.handleErrorPacket(data)
  149. }
  150. // protocol version [1 byte]
  151. if data[0] < minProtocolVersion {
  152. return nil, "", fmt.Errorf(
  153. "unsupported protocol version %d. Version %d or higher is required",
  154. data[0],
  155. minProtocolVersion,
  156. )
  157. }
  158. // server version [null terminated string]
  159. // connection id [4 bytes]
  160. pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
  161. // first part of the password cipher [8 bytes]
  162. authData := data[pos : pos+8]
  163. // (filler) always 0x00 [1 byte]
  164. pos += 8 + 1
  165. // capability flags (lower 2 bytes) [2 bytes]
  166. mc.flags = clientFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  167. if mc.flags&clientProtocol41 == 0 {
  168. return nil, "", ErrOldProtocol
  169. }
  170. if mc.flags&clientSSL == 0 && mc.cfg.tls != nil {
  171. return nil, "", ErrNoTLS
  172. }
  173. pos += 2
  174. if len(data) > pos {
  175. // character set [1 byte]
  176. // status flags [2 bytes]
  177. // capability flags (upper 2 bytes) [2 bytes]
  178. // length of auth-plugin-data [1 byte]
  179. // reserved (all [00]) [10 bytes]
  180. pos += 1 + 2 + 2 + 1 + 10
  181. // second part of the password cipher [mininum 13 bytes],
  182. // where len=MAX(13, length of auth-plugin-data - 8)
  183. //
  184. // The web documentation is ambiguous about the length. However,
  185. // according to mysql-5.7/sql/auth/sql_authentication.cc line 538,
  186. // the 13th byte is "\0 byte, terminating the second part of
  187. // a scramble". So the second part of the password cipher is
  188. // a NULL terminated string that's at least 13 bytes with the
  189. // last byte being NULL.
  190. //
  191. // The official Python library uses the fixed length 12
  192. // which seems to work but technically could have a hidden bug.
  193. authData = append(authData, data[pos:pos+12]...)
  194. pos += 13
  195. // EOF if version (>= 5.5.7 and < 5.5.10) or (>= 5.6.0 and < 5.6.2)
  196. // \NUL otherwise
  197. if end := bytes.IndexByte(data[pos:], 0x00); end != -1 {
  198. plugin = string(data[pos : pos+end])
  199. } else {
  200. plugin = string(data[pos:])
  201. }
  202. // make a memory safe copy of the cipher slice
  203. var b [20]byte
  204. copy(b[:], authData)
  205. return b[:], plugin, nil
  206. }
  207. // make a memory safe copy of the cipher slice
  208. var b [8]byte
  209. copy(b[:], authData)
  210. return b[:], plugin, nil
  211. }
  212. // Client Authentication Packet
  213. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
  214. func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool, plugin string) error {
  215. // Adjust client flags based on server support
  216. clientFlags := clientProtocol41 |
  217. clientSecureConn |
  218. clientLongPassword |
  219. clientTransactions |
  220. clientLocalFiles |
  221. clientPluginAuth |
  222. clientMultiResults |
  223. mc.flags&clientLongFlag
  224. if mc.cfg.ClientFoundRows {
  225. clientFlags |= clientFoundRows
  226. }
  227. // To enable TLS / SSL
  228. if mc.cfg.tls != nil {
  229. clientFlags |= clientSSL
  230. }
  231. if mc.cfg.MultiStatements {
  232. clientFlags |= clientMultiStatements
  233. }
  234. // encode length of the auth plugin data
  235. var authRespLEIBuf [9]byte
  236. authRespLEI := appendLengthEncodedInteger(authRespLEIBuf[:0], uint64(len(authResp)))
  237. if len(authRespLEI) > 1 {
  238. // if the length can not be written in 1 byte, it must be written as a
  239. // length encoded integer
  240. clientFlags |= clientPluginAuthLenEncClientData
  241. }
  242. pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1 + len(authRespLEI) + len(authResp) + 21 + 1
  243. if addNUL {
  244. pktLen++
  245. }
  246. // To specify a db name
  247. if n := len(mc.cfg.DBName); n > 0 {
  248. clientFlags |= clientConnectWithDB
  249. pktLen += n + 1
  250. }
  251. // Calculate packet length and get buffer with that size
  252. data := mc.buf.takeSmallBuffer(pktLen + 4)
  253. if data == nil {
  254. // cannot take the buffer. Something must be wrong with the connection
  255. errLog.Print(ErrBusyBuffer)
  256. return errBadConnNoWrite
  257. }
  258. // ClientFlags [32 bit]
  259. data[4] = byte(clientFlags)
  260. data[5] = byte(clientFlags >> 8)
  261. data[6] = byte(clientFlags >> 16)
  262. data[7] = byte(clientFlags >> 24)
  263. // MaxPacketSize [32 bit] (none)
  264. data[8] = 0x00
  265. data[9] = 0x00
  266. data[10] = 0x00
  267. data[11] = 0x00
  268. // Charset [1 byte]
  269. var found bool
  270. data[12], found = collations[mc.cfg.Collation]
  271. if !found {
  272. // Note possibility for false negatives:
  273. // could be triggered although the collation is valid if the
  274. // collations map does not contain entries the server supports.
  275. return errors.New("unknown collation")
  276. }
  277. // SSL Connection Request Packet
  278. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
  279. if mc.cfg.tls != nil {
  280. // Send TLS / SSL request packet
  281. if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil {
  282. return err
  283. }
  284. // Switch to TLS
  285. tlsConn := tls.Client(mc.netConn, mc.cfg.tls)
  286. if err := tlsConn.Handshake(); err != nil {
  287. return err
  288. }
  289. mc.netConn = tlsConn
  290. mc.buf.nc = tlsConn
  291. }
  292. // Filler [23 bytes] (all 0x00)
  293. pos := 13
  294. for ; pos < 13+23; pos++ {
  295. data[pos] = 0
  296. }
  297. // User [null terminated string]
  298. if len(mc.cfg.User) > 0 {
  299. pos += copy(data[pos:], mc.cfg.User)
  300. }
  301. data[pos] = 0x00
  302. pos++
  303. // Auth Data [length encoded integer]
  304. pos += copy(data[pos:], authRespLEI)
  305. pos += copy(data[pos:], authResp)
  306. if addNUL {
  307. data[pos] = 0x00
  308. pos++
  309. }
  310. // Databasename [null terminated string]
  311. if len(mc.cfg.DBName) > 0 {
  312. pos += copy(data[pos:], mc.cfg.DBName)
  313. data[pos] = 0x00
  314. pos++
  315. }
  316. pos += copy(data[pos:], plugin)
  317. data[pos] = 0x00
  318. // Send Auth packet
  319. return mc.writePacket(data)
  320. }
  321. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  322. func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte, addNUL bool) error {
  323. pktLen := 4 + len(authData)
  324. if addNUL {
  325. pktLen++
  326. }
  327. data := mc.buf.takeSmallBuffer(pktLen)
  328. if data == nil {
  329. // cannot take the buffer. Something must be wrong with the connection
  330. errLog.Print(ErrBusyBuffer)
  331. return errBadConnNoWrite
  332. }
  333. // Add the auth data [EOF]
  334. copy(data[4:], authData)
  335. if addNUL {
  336. data[pktLen-1] = 0x00
  337. }
  338. return mc.writePacket(data)
  339. }
  340. /******************************************************************************
  341. * Command Packets *
  342. ******************************************************************************/
  343. func (mc *mysqlConn) writeCommandPacket(command byte) error {
  344. // Reset Packet Sequence
  345. mc.sequence = 0
  346. data := mc.buf.takeSmallBuffer(4 + 1)
  347. if data == nil {
  348. // cannot take the buffer. Something must be wrong with the connection
  349. errLog.Print(ErrBusyBuffer)
  350. return errBadConnNoWrite
  351. }
  352. // Add command byte
  353. data[4] = command
  354. // Send CMD packet
  355. return mc.writePacket(data)
  356. }
  357. func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
  358. // Reset Packet Sequence
  359. mc.sequence = 0
  360. pktLen := 1 + len(arg)
  361. data := mc.buf.takeBuffer(pktLen + 4)
  362. if data == nil {
  363. // cannot take the buffer. Something must be wrong with the connection
  364. errLog.Print(ErrBusyBuffer)
  365. return errBadConnNoWrite
  366. }
  367. // Add command byte
  368. data[4] = command
  369. // Add arg
  370. copy(data[5:], arg)
  371. // Send CMD packet
  372. return mc.writePacket(data)
  373. }
  374. func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
  375. // Reset Packet Sequence
  376. mc.sequence = 0
  377. data := mc.buf.takeSmallBuffer(4 + 1 + 4)
  378. if data == nil {
  379. // cannot take the buffer. Something must be wrong with the connection
  380. errLog.Print(ErrBusyBuffer)
  381. return errBadConnNoWrite
  382. }
  383. // Add command byte
  384. data[4] = command
  385. // Add arg [32 bit]
  386. data[5] = byte(arg)
  387. data[6] = byte(arg >> 8)
  388. data[7] = byte(arg >> 16)
  389. data[8] = byte(arg >> 24)
  390. // Send CMD packet
  391. return mc.writePacket(data)
  392. }
  393. /******************************************************************************
  394. * Result Packets *
  395. ******************************************************************************/
  396. func (mc *mysqlConn) readAuthResult() ([]byte, string, error) {
  397. data, err := mc.readPacket()
  398. if err != nil {
  399. return nil, "", err
  400. }
  401. // packet indicator
  402. switch data[0] {
  403. case iOK:
  404. return nil, "", mc.handleOkPacket(data)
  405. case iAuthMoreData:
  406. return data[1:], "", err
  407. case iEOF:
  408. if len(data) < 1 {
  409. // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
  410. return nil, "mysql_old_password", nil
  411. }
  412. pluginEndIndex := bytes.IndexByte(data, 0x00)
  413. if pluginEndIndex < 0 {
  414. return nil, "", ErrMalformPkt
  415. }
  416. plugin := string(data[1:pluginEndIndex])
  417. authData := data[pluginEndIndex+1:]
  418. return authData, plugin, nil
  419. default: // Error otherwise
  420. return nil, "", mc.handleErrorPacket(data)
  421. }
  422. }
  423. // Returns error if Packet is not an 'Result OK'-Packet
  424. func (mc *mysqlConn) readResultOK() error {
  425. data, err := mc.readPacket()
  426. if err != nil {
  427. return err
  428. }
  429. if data[0] == iOK {
  430. return mc.handleOkPacket(data)
  431. }
  432. return mc.handleErrorPacket(data)
  433. }
  434. // Result Set Header Packet
  435. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
  436. func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {
  437. data, err := mc.readPacket()
  438. if err == nil {
  439. switch data[0] {
  440. case iOK:
  441. return 0, mc.handleOkPacket(data)
  442. case iERR:
  443. return 0, mc.handleErrorPacket(data)
  444. case iLocalInFile:
  445. return 0, mc.handleInFileRequest(string(data[1:]))
  446. }
  447. // column count
  448. num, _, n := readLengthEncodedInteger(data)
  449. if n-len(data) == 0 {
  450. return int(num), nil
  451. }
  452. return 0, ErrMalformPkt
  453. }
  454. return 0, err
  455. }
  456. // Error Packet
  457. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-ERR_Packet
  458. func (mc *mysqlConn) handleErrorPacket(data []byte) error {
  459. if data[0] != iERR {
  460. return ErrMalformPkt
  461. }
  462. // 0xff [1 byte]
  463. // Error Number [16 bit uint]
  464. errno := binary.LittleEndian.Uint16(data[1:3])
  465. // 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION
  466. // 1290: ER_OPTION_PREVENTS_STATEMENT (returned by Aurora during failover)
  467. if (errno == 1792 || errno == 1290) && mc.cfg.RejectReadOnly {
  468. // Oops; we are connected to a read-only connection, and won't be able
  469. // to issue any write statements. Since RejectReadOnly is configured,
  470. // we throw away this connection hoping this one would have write
  471. // permission. This is specifically for a possible race condition
  472. // during failover (e.g. on AWS Aurora). See README.md for more.
  473. //
  474. // We explicitly close the connection before returning
  475. // driver.ErrBadConn to ensure that `database/sql` purges this
  476. // connection and initiates a new one for next statement next time.
  477. mc.Close()
  478. return driver.ErrBadConn
  479. }
  480. pos := 3
  481. // SQL State [optional: # + 5bytes string]
  482. if data[3] == 0x23 {
  483. //sqlstate := string(data[4 : 4+5])
  484. pos = 9
  485. }
  486. // Error Message [string]
  487. return &MySQLError{
  488. Number: errno,
  489. Message: string(data[pos:]),
  490. }
  491. }
  492. func readStatus(b []byte) statusFlag {
  493. return statusFlag(b[0]) | statusFlag(b[1])<<8
  494. }
  495. // Ok Packet
  496. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
  497. func (mc *mysqlConn) handleOkPacket(data []byte) error {
  498. var n, m int
  499. // 0x00 [1 byte]
  500. // Affected rows [Length Coded Binary]
  501. mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
  502. // Insert id [Length Coded Binary]
  503. mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
  504. // server_status [2 bytes]
  505. mc.status = readStatus(data[1+n+m : 1+n+m+2])
  506. if mc.status&statusMoreResultsExists != 0 {
  507. return nil
  508. }
  509. // warning count [2 bytes]
  510. return nil
  511. }
  512. // Read Packets as Field Packets until EOF-Packet or an Error appears
  513. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
  514. func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
  515. columns := make([]mysqlField, count)
  516. for i := 0; ; i++ {
  517. data, err := mc.readPacket()
  518. if err != nil {
  519. return nil, err
  520. }
  521. // EOF Packet
  522. if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
  523. if i == count {
  524. return columns, nil
  525. }
  526. return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
  527. }
  528. // Catalog
  529. pos, err := skipLengthEncodedString(data)
  530. if err != nil {
  531. return nil, err
  532. }
  533. // Database [len coded string]
  534. n, err := skipLengthEncodedString(data[pos:])
  535. if err != nil {
  536. return nil, err
  537. }
  538. pos += n
  539. // Table [len coded string]
  540. if mc.cfg.ColumnsWithAlias {
  541. tableName, _, n, err := readLengthEncodedString(data[pos:])
  542. if err != nil {
  543. return nil, err
  544. }
  545. pos += n
  546. columns[i].tableName = string(tableName)
  547. } else {
  548. n, err = skipLengthEncodedString(data[pos:])
  549. if err != nil {
  550. return nil, err
  551. }
  552. pos += n
  553. }
  554. // Original table [len coded string]
  555. n, err = skipLengthEncodedString(data[pos:])
  556. if err != nil {
  557. return nil, err
  558. }
  559. pos += n
  560. // Name [len coded string]
  561. name, _, n, err := readLengthEncodedString(data[pos:])
  562. if err != nil {
  563. return nil, err
  564. }
  565. columns[i].name = string(name)
  566. pos += n
  567. // Original name [len coded string]
  568. n, err = skipLengthEncodedString(data[pos:])
  569. if err != nil {
  570. return nil, err
  571. }
  572. pos += n
  573. // Filler [uint8]
  574. pos++
  575. // Charset [charset, collation uint8]
  576. columns[i].charSet = data[pos]
  577. pos += 2
  578. // Length [uint32]
  579. columns[i].length = binary.LittleEndian.Uint32(data[pos : pos+4])
  580. pos += 4
  581. // Field type [uint8]
  582. columns[i].fieldType = fieldType(data[pos])
  583. pos++
  584. // Flags [uint16]
  585. columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  586. pos += 2
  587. // Decimals [uint8]
  588. columns[i].decimals = data[pos]
  589. //pos++
  590. // Default value [len coded binary]
  591. //if pos < len(data) {
  592. // defaultVal, _, err = bytesToLengthCodedBinary(data[pos:])
  593. //}
  594. }
  595. }
  596. // Read Packets as Field Packets until EOF-Packet or an Error appears
  597. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::ResultsetRow
  598. func (rows *textRows) readRow(dest []driver.Value) error {
  599. mc := rows.mc
  600. if rows.rs.done {
  601. return io.EOF
  602. }
  603. data, err := mc.readPacket()
  604. if err != nil {
  605. return err
  606. }
  607. // EOF Packet
  608. if data[0] == iEOF && len(data) == 5 {
  609. // server_status [2 bytes]
  610. rows.mc.status = readStatus(data[3:])
  611. rows.rs.done = true
  612. if !rows.HasNextResultSet() {
  613. rows.mc = nil
  614. }
  615. return io.EOF
  616. }
  617. if data[0] == iERR {
  618. rows.mc = nil
  619. return mc.handleErrorPacket(data)
  620. }
  621. // RowSet Packet
  622. var n int
  623. var isNull bool
  624. pos := 0
  625. for i := range dest {
  626. // Read bytes and convert to string
  627. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  628. pos += n
  629. if err == nil {
  630. if !isNull {
  631. if !mc.parseTime {
  632. continue
  633. } else {
  634. switch rows.rs.columns[i].fieldType {
  635. case fieldTypeTimestamp, fieldTypeDateTime,
  636. fieldTypeDate, fieldTypeNewDate:
  637. dest[i], err = parseDateTime(
  638. string(dest[i].([]byte)),
  639. mc.cfg.Loc,
  640. )
  641. if err == nil {
  642. continue
  643. }
  644. default:
  645. continue
  646. }
  647. }
  648. } else {
  649. dest[i] = nil
  650. continue
  651. }
  652. }
  653. return err // err != nil
  654. }
  655. return nil
  656. }
  657. // Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
  658. func (mc *mysqlConn) readUntilEOF() error {
  659. for {
  660. data, err := mc.readPacket()
  661. if err != nil {
  662. return err
  663. }
  664. switch data[0] {
  665. case iERR:
  666. return mc.handleErrorPacket(data)
  667. case iEOF:
  668. if len(data) == 5 {
  669. mc.status = readStatus(data[3:])
  670. }
  671. return nil
  672. }
  673. }
  674. }
  675. /******************************************************************************
  676. * Prepared Statements *
  677. ******************************************************************************/
  678. // Prepare Result Packets
  679. // http://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html
  680. func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
  681. data, err := stmt.mc.readPacket()
  682. if err == nil {
  683. // packet indicator [1 byte]
  684. if data[0] != iOK {
  685. return 0, stmt.mc.handleErrorPacket(data)
  686. }
  687. // statement id [4 bytes]
  688. stmt.id = binary.LittleEndian.Uint32(data[1:5])
  689. // Column count [16 bit uint]
  690. columnCount := binary.LittleEndian.Uint16(data[5:7])
  691. // Param count [16 bit uint]
  692. stmt.paramCount = int(binary.LittleEndian.Uint16(data[7:9]))
  693. // Reserved [8 bit]
  694. // Warning count [16 bit uint]
  695. return columnCount, nil
  696. }
  697. return 0, err
  698. }
  699. // http://dev.mysql.com/doc/internals/en/com-stmt-send-long-data.html
  700. func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
  701. maxLen := stmt.mc.maxAllowedPacket - 1
  702. pktLen := maxLen
  703. // After the header (bytes 0-3) follows before the data:
  704. // 1 byte command
  705. // 4 bytes stmtID
  706. // 2 bytes paramID
  707. const dataOffset = 1 + 4 + 2
  708. // Cannot use the write buffer since
  709. // a) the buffer is too small
  710. // b) it is in use
  711. data := make([]byte, 4+1+4+2+len(arg))
  712. copy(data[4+dataOffset:], arg)
  713. for argLen := len(arg); argLen > 0; argLen -= pktLen - dataOffset {
  714. if dataOffset+argLen < maxLen {
  715. pktLen = dataOffset + argLen
  716. }
  717. stmt.mc.sequence = 0
  718. // Add command byte [1 byte]
  719. data[4] = comStmtSendLongData
  720. // Add stmtID [32 bit]
  721. data[5] = byte(stmt.id)
  722. data[6] = byte(stmt.id >> 8)
  723. data[7] = byte(stmt.id >> 16)
  724. data[8] = byte(stmt.id >> 24)
  725. // Add paramID [16 bit]
  726. data[9] = byte(paramID)
  727. data[10] = byte(paramID >> 8)
  728. // Send CMD packet
  729. err := stmt.mc.writePacket(data[:4+pktLen])
  730. if err == nil {
  731. data = data[pktLen-dataOffset:]
  732. continue
  733. }
  734. return err
  735. }
  736. // Reset Packet Sequence
  737. stmt.mc.sequence = 0
  738. return nil
  739. }
  740. // Execute Prepared Statement
  741. // http://dev.mysql.com/doc/internals/en/com-stmt-execute.html
  742. func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
  743. if len(args) != stmt.paramCount {
  744. return fmt.Errorf(
  745. "argument count mismatch (got: %d; has: %d)",
  746. len(args),
  747. stmt.paramCount,
  748. )
  749. }
  750. const minPktLen = 4 + 1 + 4 + 1 + 4
  751. mc := stmt.mc
  752. // Determine threshould dynamically to avoid packet size shortage.
  753. longDataSize := mc.maxAllowedPacket / (stmt.paramCount + 1)
  754. if longDataSize < 64 {
  755. longDataSize = 64
  756. }
  757. // Reset packet-sequence
  758. mc.sequence = 0
  759. var data []byte
  760. if len(args) == 0 {
  761. data = mc.buf.takeBuffer(minPktLen)
  762. } else {
  763. data = mc.buf.takeCompleteBuffer()
  764. }
  765. if data == nil {
  766. // cannot take the buffer. Something must be wrong with the connection
  767. errLog.Print(ErrBusyBuffer)
  768. return errBadConnNoWrite
  769. }
  770. // command [1 byte]
  771. data[4] = comStmtExecute
  772. // statement_id [4 bytes]
  773. data[5] = byte(stmt.id)
  774. data[6] = byte(stmt.id >> 8)
  775. data[7] = byte(stmt.id >> 16)
  776. data[8] = byte(stmt.id >> 24)
  777. // flags (0: CURSOR_TYPE_NO_CURSOR) [1 byte]
  778. data[9] = 0x00
  779. // iteration_count (uint32(1)) [4 bytes]
  780. data[10] = 0x01
  781. data[11] = 0x00
  782. data[12] = 0x00
  783. data[13] = 0x00
  784. if len(args) > 0 {
  785. pos := minPktLen
  786. var nullMask []byte
  787. if maskLen, typesLen := (len(args)+7)/8, 1+2*len(args); pos+maskLen+typesLen >= len(data) {
  788. // buffer has to be extended but we don't know by how much so
  789. // we depend on append after all data with known sizes fit.
  790. // We stop at that because we deal with a lot of columns here
  791. // which makes the required allocation size hard to guess.
  792. tmp := make([]byte, pos+maskLen+typesLen)
  793. copy(tmp[:pos], data[:pos])
  794. data = tmp
  795. nullMask = data[pos : pos+maskLen]
  796. pos += maskLen
  797. } else {
  798. nullMask = data[pos : pos+maskLen]
  799. for i := 0; i < maskLen; i++ {
  800. nullMask[i] = 0
  801. }
  802. pos += maskLen
  803. }
  804. // newParameterBoundFlag 1 [1 byte]
  805. data[pos] = 0x01
  806. pos++
  807. // type of each parameter [len(args)*2 bytes]
  808. paramTypes := data[pos:]
  809. pos += len(args) * 2
  810. // value of each parameter [n bytes]
  811. paramValues := data[pos:pos]
  812. valuesCap := cap(paramValues)
  813. for i, arg := range args {
  814. // build NULL-bitmap
  815. if arg == nil {
  816. nullMask[i/8] |= 1 << (uint(i) & 7)
  817. paramTypes[i+i] = byte(fieldTypeNULL)
  818. paramTypes[i+i+1] = 0x00
  819. continue
  820. }
  821. // cache types and values
  822. switch v := arg.(type) {
  823. case int64:
  824. paramTypes[i+i] = byte(fieldTypeLongLong)
  825. paramTypes[i+i+1] = 0x00
  826. if cap(paramValues)-len(paramValues)-8 >= 0 {
  827. paramValues = paramValues[:len(paramValues)+8]
  828. binary.LittleEndian.PutUint64(
  829. paramValues[len(paramValues)-8:],
  830. uint64(v),
  831. )
  832. } else {
  833. paramValues = append(paramValues,
  834. uint64ToBytes(uint64(v))...,
  835. )
  836. }
  837. case float64:
  838. paramTypes[i+i] = byte(fieldTypeDouble)
  839. paramTypes[i+i+1] = 0x00
  840. if cap(paramValues)-len(paramValues)-8 >= 0 {
  841. paramValues = paramValues[:len(paramValues)+8]
  842. binary.LittleEndian.PutUint64(
  843. paramValues[len(paramValues)-8:],
  844. math.Float64bits(v),
  845. )
  846. } else {
  847. paramValues = append(paramValues,
  848. uint64ToBytes(math.Float64bits(v))...,
  849. )
  850. }
  851. case bool:
  852. paramTypes[i+i] = byte(fieldTypeTiny)
  853. paramTypes[i+i+1] = 0x00
  854. if v {
  855. paramValues = append(paramValues, 0x01)
  856. } else {
  857. paramValues = append(paramValues, 0x00)
  858. }
  859. case []byte:
  860. // Common case (non-nil value) first
  861. if v != nil {
  862. paramTypes[i+i] = byte(fieldTypeString)
  863. paramTypes[i+i+1] = 0x00
  864. if len(v) < longDataSize {
  865. paramValues = appendLengthEncodedInteger(paramValues,
  866. uint64(len(v)),
  867. )
  868. paramValues = append(paramValues, v...)
  869. } else {
  870. if err := stmt.writeCommandLongData(i, v); err != nil {
  871. return err
  872. }
  873. }
  874. continue
  875. }
  876. // Handle []byte(nil) as a NULL value
  877. nullMask[i/8] |= 1 << (uint(i) & 7)
  878. paramTypes[i+i] = byte(fieldTypeNULL)
  879. paramTypes[i+i+1] = 0x00
  880. case string:
  881. paramTypes[i+i] = byte(fieldTypeString)
  882. paramTypes[i+i+1] = 0x00
  883. if len(v) < longDataSize {
  884. paramValues = appendLengthEncodedInteger(paramValues,
  885. uint64(len(v)),
  886. )
  887. paramValues = append(paramValues, v...)
  888. } else {
  889. if err := stmt.writeCommandLongData(i, []byte(v)); err != nil {
  890. return err
  891. }
  892. }
  893. case time.Time:
  894. paramTypes[i+i] = byte(fieldTypeString)
  895. paramTypes[i+i+1] = 0x00
  896. var a [64]byte
  897. var b = a[:0]
  898. if v.IsZero() {
  899. b = append(b, "0000-00-00"...)
  900. } else {
  901. b = v.In(mc.cfg.Loc).AppendFormat(b, timeFormat)
  902. }
  903. paramValues = appendLengthEncodedInteger(paramValues,
  904. uint64(len(b)),
  905. )
  906. paramValues = append(paramValues, b...)
  907. default:
  908. return fmt.Errorf("cannot convert type: %T", arg)
  909. }
  910. }
  911. // Check if param values exceeded the available buffer
  912. // In that case we must build the data packet with the new values buffer
  913. if valuesCap != cap(paramValues) {
  914. data = append(data[:pos], paramValues...)
  915. mc.buf.buf = data
  916. }
  917. pos += len(paramValues)
  918. data = data[:pos]
  919. }
  920. return mc.writePacket(data)
  921. }
  922. func (mc *mysqlConn) discardResults() error {
  923. for mc.status&statusMoreResultsExists != 0 {
  924. resLen, err := mc.readResultSetHeaderPacket()
  925. if err != nil {
  926. return err
  927. }
  928. if resLen > 0 {
  929. // columns
  930. if err := mc.readUntilEOF(); err != nil {
  931. return err
  932. }
  933. // rows
  934. if err := mc.readUntilEOF(); err != nil {
  935. return err
  936. }
  937. }
  938. }
  939. return nil
  940. }
  941. // http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
  942. func (rows *binaryRows) readRow(dest []driver.Value) error {
  943. data, err := rows.mc.readPacket()
  944. if err != nil {
  945. return err
  946. }
  947. // packet indicator [1 byte]
  948. if data[0] != iOK {
  949. // EOF Packet
  950. if data[0] == iEOF && len(data) == 5 {
  951. rows.mc.status = readStatus(data[3:])
  952. rows.rs.done = true
  953. if !rows.HasNextResultSet() {
  954. rows.mc = nil
  955. }
  956. return io.EOF
  957. }
  958. mc := rows.mc
  959. rows.mc = nil
  960. // Error otherwise
  961. return mc.handleErrorPacket(data)
  962. }
  963. // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
  964. pos := 1 + (len(dest)+7+2)>>3
  965. nullMask := data[1:pos]
  966. for i := range dest {
  967. // Field is NULL
  968. // (byte >> bit-pos) % 2 == 1
  969. if ((nullMask[(i+2)>>3] >> uint((i+2)&7)) & 1) == 1 {
  970. dest[i] = nil
  971. continue
  972. }
  973. // Convert to byte-coded string
  974. switch rows.rs.columns[i].fieldType {
  975. case fieldTypeNULL:
  976. dest[i] = nil
  977. continue
  978. // Numeric Types
  979. case fieldTypeTiny:
  980. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  981. dest[i] = int64(data[pos])
  982. } else {
  983. dest[i] = int64(int8(data[pos]))
  984. }
  985. pos++
  986. continue
  987. case fieldTypeShort, fieldTypeYear:
  988. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  989. dest[i] = int64(binary.LittleEndian.Uint16(data[pos : pos+2]))
  990. } else {
  991. dest[i] = int64(int16(binary.LittleEndian.Uint16(data[pos : pos+2])))
  992. }
  993. pos += 2
  994. continue
  995. case fieldTypeInt24, fieldTypeLong:
  996. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  997. dest[i] = int64(binary.LittleEndian.Uint32(data[pos : pos+4]))
  998. } else {
  999. dest[i] = int64(int32(binary.LittleEndian.Uint32(data[pos : pos+4])))
  1000. }
  1001. pos += 4
  1002. continue
  1003. case fieldTypeLongLong:
  1004. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  1005. val := binary.LittleEndian.Uint64(data[pos : pos+8])
  1006. if val > math.MaxInt64 {
  1007. dest[i] = uint64ToString(val)
  1008. } else {
  1009. dest[i] = int64(val)
  1010. }
  1011. } else {
  1012. dest[i] = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1013. }
  1014. pos += 8
  1015. continue
  1016. case fieldTypeFloat:
  1017. dest[i] = math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4]))
  1018. pos += 4
  1019. continue
  1020. case fieldTypeDouble:
  1021. dest[i] = math.Float64frombits(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1022. pos += 8
  1023. continue
  1024. // Length coded Binary Strings
  1025. case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
  1026. fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
  1027. fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
  1028. fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON:
  1029. var isNull bool
  1030. var n int
  1031. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  1032. pos += n
  1033. if err == nil {
  1034. if !isNull {
  1035. continue
  1036. } else {
  1037. dest[i] = nil
  1038. continue
  1039. }
  1040. }
  1041. return err
  1042. case
  1043. fieldTypeDate, fieldTypeNewDate, // Date YYYY-MM-DD
  1044. fieldTypeTime, // Time [-][H]HH:MM:SS[.fractal]
  1045. fieldTypeTimestamp, fieldTypeDateTime: // Timestamp YYYY-MM-DD HH:MM:SS[.fractal]
  1046. num, isNull, n := readLengthEncodedInteger(data[pos:])
  1047. pos += n
  1048. switch {
  1049. case isNull:
  1050. dest[i] = nil
  1051. continue
  1052. case rows.rs.columns[i].fieldType == fieldTypeTime:
  1053. // database/sql does not support an equivalent to TIME, return a string
  1054. var dstlen uint8
  1055. switch decimals := rows.rs.columns[i].decimals; decimals {
  1056. case 0x00, 0x1f:
  1057. dstlen = 8
  1058. case 1, 2, 3, 4, 5, 6:
  1059. dstlen = 8 + 1 + decimals
  1060. default:
  1061. return fmt.Errorf(
  1062. "protocol error, illegal decimals value %d",
  1063. rows.rs.columns[i].decimals,
  1064. )
  1065. }
  1066. dest[i], err = formatBinaryTime(data[pos:pos+int(num)], dstlen)
  1067. case rows.mc.parseTime:
  1068. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.Loc)
  1069. default:
  1070. var dstlen uint8
  1071. if rows.rs.columns[i].fieldType == fieldTypeDate {
  1072. dstlen = 10
  1073. } else {
  1074. switch decimals := rows.rs.columns[i].decimals; decimals {
  1075. case 0x00, 0x1f:
  1076. dstlen = 19
  1077. case 1, 2, 3, 4, 5, 6:
  1078. dstlen = 19 + 1 + decimals
  1079. default:
  1080. return fmt.Errorf(
  1081. "protocol error, illegal decimals value %d",
  1082. rows.rs.columns[i].decimals,
  1083. )
  1084. }
  1085. }
  1086. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen)
  1087. }
  1088. if err == nil {
  1089. pos += int(num)
  1090. continue
  1091. } else {
  1092. return err
  1093. }
  1094. // Please report if this happens!
  1095. default:
  1096. return fmt.Errorf("unknown field type %d", rows.rs.columns[i].fieldType)
  1097. }
  1098. }
  1099. return nil
  1100. }