You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1272 lines
30 KiB

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