packets.go 32 KB

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