packets.go 26 KB

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