packets.go 26 KB

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