packets.go 25 KB

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