packets.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff)
  195. // To specify a db name
  196. if n := len(mc.cfg.dbname); n > 0 {
  197. clientFlags |= clientConnectWithDB
  198. pktLen += n + 1
  199. }
  200. // Calculate packet length and make buffer with that size
  201. data := make([]byte, pktLen+4)
  202. // ClientFlags [32 bit]
  203. data[4] = byte(clientFlags)
  204. data[5] = byte(clientFlags >> 8)
  205. data[6] = byte(clientFlags >> 16)
  206. data[7] = byte(clientFlags >> 24)
  207. // MaxPacketSize [32 bit] (none)
  208. //data[8] = 0x00
  209. //data[9] = 0x00
  210. //data[10] = 0x00
  211. //data[11] = 0x00
  212. // Charset [1 byte]
  213. data[12] = collation_utf8_general_ci
  214. // SSL Connection Request Packet
  215. // http://dev.mysql.com/doc/internals/en/connection-phase.html#packet-Protocol::SSLRequest
  216. if mc.cfg.tls != nil {
  217. // Packet header [24bit length + 1 byte sequence]
  218. data[0] = byte((4 + 4 + 1 + 23))
  219. data[1] = byte((4 + 4 + 1 + 23) >> 8)
  220. data[2] = byte((4 + 4 + 1 + 23) >> 16)
  221. data[3] = mc.sequence
  222. // Send TLS / SSL request packet
  223. if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil {
  224. return err
  225. }
  226. // Switch to TLS
  227. tlsConn := tls.Client(mc.netConn, mc.cfg.tls)
  228. if err := tlsConn.Handshake(); err != nil {
  229. return err
  230. }
  231. mc.netConn = tlsConn
  232. mc.buf.rd = tlsConn
  233. }
  234. // Add the packet header [24bit length + 1 byte sequence]
  235. data[0] = byte(pktLen)
  236. data[1] = byte(pktLen >> 8)
  237. data[2] = byte(pktLen >> 16)
  238. data[3] = mc.sequence
  239. // Filler [23 bytes] (all 0x00)
  240. pos := 13 + 23
  241. // User [null terminated string]
  242. if len(mc.cfg.user) > 0 {
  243. pos += copy(data[pos:], mc.cfg.user)
  244. }
  245. //data[pos] = 0x00
  246. pos++
  247. // ScrambleBuffer [length encoded integer]
  248. data[pos] = byte(len(scrambleBuff))
  249. pos += 1 + copy(data[pos+1:], scrambleBuff)
  250. // Databasename [null terminated string]
  251. if len(mc.cfg.dbname) > 0 {
  252. pos += copy(data[pos:], mc.cfg.dbname)
  253. //data[pos] = 0x00
  254. }
  255. // Send Auth packet
  256. return mc.writePacket(data)
  257. }
  258. // Client old authentication packet
  259. // http://dev.mysql.com/doc/internals/en/connection-phase.html#packet-Protocol::AuthSwitchResponse
  260. func (mc *mysqlConn) writeOldAuthPacket() error {
  261. // User password
  262. scrambleBuff := scrambleOldPassword(mc.cipher, []byte(mc.cfg.passwd))
  263. mc.cipher = nil
  264. // Calculate the packet lenght and add a tailing 0
  265. pktLen := len(scrambleBuff) + 1
  266. data := make([]byte, pktLen+4)
  267. // Add the packet header [24bit length + 1 byte sequence]
  268. data[0] = byte(pktLen)
  269. data[1] = byte(pktLen >> 8)
  270. data[2] = byte(pktLen >> 16)
  271. data[3] = mc.sequence
  272. // Add the scrambled password (it will be terminated by 0)
  273. copy(data[4:], scrambleBuff)
  274. return mc.writePacket(data)
  275. }
  276. /******************************************************************************
  277. * Command Packets *
  278. ******************************************************************************/
  279. func (mc *mysqlConn) writeCommandPacket(command byte) error {
  280. // Reset Packet Sequence
  281. mc.sequence = 0
  282. // Send CMD packet
  283. return mc.writePacket([]byte{
  284. // Add the packet header [24bit length + 1 byte sequence]
  285. 0x01, // 1 byte long
  286. 0x00,
  287. 0x00,
  288. 0x00, // mc.sequence
  289. // Add command byte
  290. command,
  291. })
  292. }
  293. func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
  294. // Reset Packet Sequence
  295. mc.sequence = 0
  296. pktLen := 1 + len(arg)
  297. data := make([]byte, pktLen+4)
  298. // Add the packet header [24bit length + 1 byte sequence]
  299. data[0] = byte(pktLen)
  300. data[1] = byte(pktLen >> 8)
  301. data[2] = byte(pktLen >> 16)
  302. //data[3] = mc.sequence
  303. // Add command byte
  304. data[4] = command
  305. // Add arg
  306. copy(data[5:], arg)
  307. // Send CMD packet
  308. return mc.writePacket(data)
  309. }
  310. func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
  311. // Reset Packet Sequence
  312. mc.sequence = 0
  313. // Send CMD packet
  314. return mc.writePacket([]byte{
  315. // Add the packet header [24bit length + 1 byte sequence]
  316. 0x05, // 5 bytes long
  317. 0x00,
  318. 0x00,
  319. 0x00, // mc.sequence
  320. // Add command byte
  321. command,
  322. // Add arg [32 bit]
  323. byte(arg),
  324. byte(arg >> 8),
  325. byte(arg >> 16),
  326. byte(arg >> 24),
  327. })
  328. }
  329. /******************************************************************************
  330. * Result Packets *
  331. ******************************************************************************/
  332. // Returns error if Packet is not an 'Result OK'-Packet
  333. func (mc *mysqlConn) readResultOK() error {
  334. data, err := mc.readPacket()
  335. if err == nil {
  336. // packet indicator
  337. switch data[0] {
  338. case iOK:
  339. // Remove the chipher in case of successfull authentication
  340. mc.cipher = nil
  341. return mc.handleOkPacket(data)
  342. case iEOF:
  343. // someone is using old_passwords
  344. err = mc.writeOldAuthPacket()
  345. if err != nil {
  346. return err
  347. }
  348. return mc.readResultOK()
  349. default: // Error otherwise
  350. return mc.handleErrorPacket(data)
  351. }
  352. }
  353. return err
  354. }
  355. // Result Set Header Packet
  356. // http://dev.mysql.com/doc/internals/en/text-protocol.html#packet-ProtocolText::Resultset
  357. func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {
  358. data, err := mc.readPacket()
  359. if err == nil {
  360. switch data[0] {
  361. case iOK:
  362. return 0, mc.handleOkPacket(data)
  363. case iERR:
  364. return 0, mc.handleErrorPacket(data)
  365. case iLocalInFile:
  366. return 0, mc.handleInFileRequest(string(data[1:]))
  367. }
  368. // column count
  369. num, _, n := readLengthEncodedInteger(data)
  370. if n-len(data) == 0 {
  371. return int(num), nil
  372. }
  373. return 0, errMalformPkt
  374. }
  375. return 0, err
  376. }
  377. // Error Packet
  378. // http://dev.mysql.com/doc/internals/en/overview.html#packet-ERR_Packet
  379. func (mc *mysqlConn) handleErrorPacket(data []byte) error {
  380. if data[0] != iERR {
  381. return errMalformPkt
  382. }
  383. // 0xff [1 byte]
  384. // Error Number [16 bit uint]
  385. errno := binary.LittleEndian.Uint16(data[1:3])
  386. pos := 3
  387. // SQL State [optional: # + 5bytes string]
  388. //sqlstate := string(data[pos : pos+6])
  389. if data[pos] == 0x23 {
  390. pos = 9
  391. }
  392. // Error Message [string]
  393. return &MySQLError{
  394. Number: errno,
  395. Message: string(data[pos:]),
  396. }
  397. }
  398. // Ok Packet
  399. // http://dev.mysql.com/doc/internals/en/overview.html#packet-OK_Packet
  400. func (mc *mysqlConn) handleOkPacket(data []byte) (err error) {
  401. var n, m int
  402. // 0x00 [1 byte]
  403. // Affected rows [Length Coded Binary]
  404. mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
  405. // Insert id [Length Coded Binary]
  406. mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
  407. // server_status [2 bytes]
  408. // warning count [2 bytes]
  409. if !mc.strict {
  410. return
  411. } else {
  412. pos := 1 + n + m + 2
  413. if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
  414. err = mc.getWarnings()
  415. }
  416. }
  417. // message [until end of packet]
  418. return
  419. }
  420. // Read Packets as Field Packets until EOF-Packet or an Error appears
  421. // http://dev.mysql.com/doc/internals/en/text-protocol.html#packet-Protocol::ColumnDefinition41
  422. func (mc *mysqlConn) readColumns(count int) (columns []mysqlField, err error) {
  423. var data []byte
  424. var i, pos, n int
  425. var name []byte
  426. columns = make([]mysqlField, count)
  427. for {
  428. data, err = mc.readPacket()
  429. if err != nil {
  430. return
  431. }
  432. // EOF Packet
  433. if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
  434. if i != count {
  435. err = fmt.Errorf("ColumnsCount mismatch n:%d len:%d", count, len(columns))
  436. }
  437. return
  438. }
  439. // Catalog
  440. pos, err = skipLengthEnodedString(data)
  441. if err != nil {
  442. return
  443. }
  444. // Database [len coded string]
  445. n, err = skipLengthEnodedString(data[pos:])
  446. if err != nil {
  447. return
  448. }
  449. pos += n
  450. // Table [len coded string]
  451. n, err = skipLengthEnodedString(data[pos:])
  452. if err != nil {
  453. return
  454. }
  455. pos += n
  456. // Original table [len coded string]
  457. n, err = skipLengthEnodedString(data[pos:])
  458. if err != nil {
  459. return
  460. }
  461. pos += n
  462. // Name [len coded string]
  463. name, _, n, err = readLengthEnodedString(data[pos:])
  464. if err != nil {
  465. return
  466. }
  467. columns[i].name = string(name)
  468. pos += n
  469. // Original name [len coded string]
  470. n, err = skipLengthEnodedString(data[pos:])
  471. if err != nil {
  472. return
  473. }
  474. // Filler [1 byte]
  475. // Charset [16 bit uint]
  476. // Length [32 bit uint]
  477. pos += n + 1 + 2 + 4
  478. // Field type [byte]
  479. columns[i].fieldType = data[pos]
  480. pos++
  481. // Flags [16 bit uint]
  482. columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  483. //pos += 2
  484. // Decimals [8 bit uint]
  485. //pos++
  486. // Default value [len coded binary]
  487. //if pos < len(data) {
  488. // defaultVal, _, err = bytesToLengthCodedBinary(data[pos:])
  489. //}
  490. i++
  491. }
  492. }
  493. // Read Packets as Field Packets until EOF-Packet or an Error appears
  494. // http://dev.mysql.com/doc/internals/en/text-protocol.html#packet-ProtocolText::ResultsetRow
  495. func (rows *mysqlRows) readRow(dest []driver.Value) (err error) {
  496. data, err := rows.mc.readPacket()
  497. if err != nil {
  498. return
  499. }
  500. // EOF Packet
  501. if data[0] == iEOF && len(data) == 5 {
  502. return io.EOF
  503. }
  504. // RowSet Packet
  505. var n int
  506. var isNull bool
  507. pos := 0
  508. for i := range dest {
  509. // Read bytes and convert to string
  510. dest[i], isNull, n, err = readLengthEnodedString(data[pos:])
  511. pos += n
  512. if err == nil {
  513. if !isNull {
  514. if !rows.mc.parseTime {
  515. continue
  516. } else {
  517. switch rows.columns[i].fieldType {
  518. case fieldTypeTimestamp, fieldTypeDateTime,
  519. fieldTypeDate, fieldTypeNewDate:
  520. dest[i], err = parseDateTime(string(dest[i].([]byte)), rows.mc.cfg.loc)
  521. if err == nil {
  522. continue
  523. }
  524. default:
  525. continue
  526. }
  527. }
  528. } else {
  529. dest[i] = nil
  530. continue
  531. }
  532. }
  533. return // err
  534. }
  535. return
  536. }
  537. // Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
  538. func (mc *mysqlConn) readUntilEOF() (err error) {
  539. var data []byte
  540. for {
  541. data, err = mc.readPacket()
  542. // No Err and no EOF Packet
  543. if err == nil && data[0] != iEOF {
  544. continue
  545. }
  546. return // Err or EOF
  547. }
  548. }
  549. /******************************************************************************
  550. * Prepared Statements *
  551. ******************************************************************************/
  552. // Prepare Result Packets
  553. // http://dev.mysql.com/doc/internals/en/prepared-statements.html#com-stmt-prepare-response
  554. func (stmt *mysqlStmt) readPrepareResultPacket() (columnCount uint16, err error) {
  555. data, err := stmt.mc.readPacket()
  556. if err == nil {
  557. // Position
  558. pos := 0
  559. // packet indicator [1 byte]
  560. if data[pos] != iOK {
  561. err = stmt.mc.handleErrorPacket(data)
  562. return
  563. }
  564. pos++
  565. // statement id [4 bytes]
  566. stmt.id = binary.LittleEndian.Uint32(data[pos : pos+4])
  567. pos += 4
  568. // Column count [16 bit uint]
  569. columnCount = binary.LittleEndian.Uint16(data[pos : pos+2])
  570. pos += 2
  571. // Param count [16 bit uint]
  572. stmt.paramCount = int(binary.LittleEndian.Uint16(data[pos : pos+2]))
  573. pos += 2
  574. // Reserved [8 bit]
  575. pos++
  576. // Warning count [16 bit uint]
  577. if !stmt.mc.strict {
  578. return
  579. } else {
  580. // Check for warnings count > 0, only available in MySQL > 4.1
  581. if len(data) >= 12 && binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
  582. err = stmt.mc.getWarnings()
  583. }
  584. }
  585. }
  586. return
  587. }
  588. // http://dev.mysql.com/doc/internals/en/prepared-statements.html#com-stmt-send-long-data
  589. func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) (err error) {
  590. maxLen := stmt.mc.maxPacketAllowed - 1
  591. pktLen := maxLen
  592. argLen := len(arg)
  593. data := make([]byte, 4+1+4+2+argLen)
  594. copy(data[4+1+4+2:], arg)
  595. for argLen > 0 {
  596. if 1+4+2+argLen < maxLen {
  597. pktLen = 1 + 4 + 2 + argLen
  598. }
  599. // Add the packet header [24bit length + 1 byte sequence]
  600. data[0] = byte(pktLen)
  601. data[1] = byte(pktLen >> 8)
  602. data[2] = byte(pktLen >> 16)
  603. data[3] = 0x00 // mc.sequence
  604. // Add command byte [1 byte]
  605. data[4] = comStmtSendLongData
  606. // Add stmtID [32 bit]
  607. data[5] = byte(stmt.id)
  608. data[6] = byte(stmt.id >> 8)
  609. data[7] = byte(stmt.id >> 16)
  610. data[8] = byte(stmt.id >> 24)
  611. // Add paramID [16 bit]
  612. data[9] = byte(paramID)
  613. data[10] = byte(paramID >> 8)
  614. // Send CMD packet
  615. err = stmt.mc.writePacket(data[:4+pktLen])
  616. if err == nil {
  617. argLen -= pktLen - (1 + 4 + 2)
  618. data = data[pktLen-(1+4+2):]
  619. continue
  620. }
  621. return err
  622. }
  623. // Reset Packet Sequence
  624. stmt.mc.sequence = 0
  625. return nil
  626. }
  627. // Execute Prepared Statement
  628. // http://dev.mysql.com/doc/internals/en/prepared-statements.html#com-stmt-execute
  629. func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
  630. if len(args) != stmt.paramCount {
  631. return fmt.Errorf(
  632. "Arguments count mismatch (Got: %d Has: %d)",
  633. len(args),
  634. stmt.paramCount)
  635. }
  636. // Reset packet-sequence
  637. stmt.mc.sequence = 0
  638. pktLen := 1 + 4 + 1 + 4 + ((stmt.paramCount + 7) >> 3) + 1 + (stmt.paramCount << 1)
  639. paramValues := make([][]byte, stmt.paramCount)
  640. paramTypes := make([]byte, (stmt.paramCount << 1))
  641. bitMask := uint64(0)
  642. var i int
  643. for i = range args {
  644. // build NULL-bitmap
  645. if args[i] == nil {
  646. bitMask += 1 << uint(i)
  647. paramTypes[i<<1] = fieldTypeNULL
  648. continue
  649. }
  650. // cache types and values
  651. switch v := args[i].(type) {
  652. case int64:
  653. paramTypes[i<<1] = fieldTypeLongLong
  654. paramValues[i] = uint64ToBytes(uint64(v))
  655. pktLen += 8
  656. continue
  657. case float64:
  658. paramTypes[i<<1] = fieldTypeDouble
  659. paramValues[i] = uint64ToBytes(math.Float64bits(v))
  660. pktLen += 8
  661. continue
  662. case bool:
  663. paramTypes[i<<1] = fieldTypeTiny
  664. pktLen++
  665. if v {
  666. paramValues[i] = []byte{0x01}
  667. } else {
  668. paramValues[i] = []byte{0x00}
  669. }
  670. continue
  671. case []byte:
  672. paramTypes[i<<1] = fieldTypeString
  673. if len(v) < stmt.mc.maxPacketAllowed-pktLen-(stmt.paramCount-(i+1))*64 {
  674. paramValues[i] = append(
  675. lengthEncodedIntegerToBytes(uint64(len(v))),
  676. v...,
  677. )
  678. pktLen += len(paramValues[i])
  679. continue
  680. } else {
  681. err := stmt.writeCommandLongData(i, v)
  682. if err == nil {
  683. continue
  684. }
  685. return err
  686. }
  687. case string:
  688. paramTypes[i<<1] = fieldTypeString
  689. if len(v) < stmt.mc.maxPacketAllowed-pktLen-(stmt.paramCount-(i+1))*64 {
  690. paramValues[i] = append(
  691. lengthEncodedIntegerToBytes(uint64(len(v))),
  692. []byte(v)...,
  693. )
  694. pktLen += len(paramValues[i])
  695. continue
  696. } else {
  697. err := stmt.writeCommandLongData(i, []byte(v))
  698. if err == nil {
  699. continue
  700. }
  701. return err
  702. }
  703. case time.Time:
  704. paramTypes[i<<1] = fieldTypeString
  705. var val []byte
  706. if v.IsZero() {
  707. val = []byte("0000-00-00")
  708. } else {
  709. val = []byte(v.In(stmt.mc.cfg.loc).Format(timeFormat))
  710. }
  711. paramValues[i] = append(
  712. lengthEncodedIntegerToBytes(uint64(len(val))),
  713. val...,
  714. )
  715. pktLen += len(paramValues[i])
  716. continue
  717. default:
  718. return fmt.Errorf("Can't convert type: %T", args[i])
  719. }
  720. }
  721. data := make([]byte, pktLen+4)
  722. // packet header [4 bytes]
  723. data[0] = byte(pktLen)
  724. data[1] = byte(pktLen >> 8)
  725. data[2] = byte(pktLen >> 16)
  726. data[3] = stmt.mc.sequence
  727. // command [1 byte]
  728. data[4] = comStmtExecute
  729. // statement_id [4 bytes]
  730. data[5] = byte(stmt.id)
  731. data[6] = byte(stmt.id >> 8)
  732. data[7] = byte(stmt.id >> 16)
  733. data[8] = byte(stmt.id >> 24)
  734. // flags (0: CURSOR_TYPE_NO_CURSOR) [1 byte]
  735. //data[9] = 0x00
  736. // iteration_count (uint32(1)) [4 bytes]
  737. data[10] = 0x01
  738. //data[11] = 0x00
  739. //data[12] = 0x00
  740. //data[13] = 0x00
  741. if stmt.paramCount > 0 {
  742. // NULL-bitmap [(param_count+7)/8 bytes]
  743. pos := 14 + ((stmt.paramCount + 7) >> 3)
  744. // Convert bitMask to bytes
  745. for i = 14; i < pos; i++ {
  746. data[i] = byte(bitMask >> uint((i-14)<<3))
  747. }
  748. // newParameterBoundFlag 1 [1 byte]
  749. data[pos] = 0x01
  750. pos++
  751. // type of parameters [param_count*2 bytes]
  752. pos += copy(data[pos:], paramTypes)
  753. // values for the parameters [n bytes]
  754. for i = range paramValues {
  755. pos += copy(data[pos:], paramValues[i])
  756. }
  757. }
  758. return stmt.mc.writePacket(data)
  759. }
  760. // http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::ResultsetRow
  761. func (rows *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
  762. data, err := rows.mc.readPacket()
  763. if err != nil {
  764. return
  765. }
  766. // packet indicator [1 byte]
  767. if data[0] != iOK {
  768. // EOF Packet
  769. if data[0] == iEOF && len(data) == 5 {
  770. return io.EOF
  771. } else {
  772. // Error otherwise
  773. return rows.mc.handleErrorPacket(data)
  774. }
  775. }
  776. // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
  777. pos := 1 + (len(dest)+7+2)>>3
  778. nullBitMap := data[1:pos]
  779. // values [rest]
  780. var n int
  781. var unsigned bool
  782. for i := range dest {
  783. // Field is NULL
  784. // (byte >> bit-pos) % 2 == 1
  785. if ((nullBitMap[(i+2)>>3] >> uint((i+2)&7)) & 1) == 1 {
  786. dest[i] = nil
  787. continue
  788. }
  789. unsigned = rows.columns[i].flags&flagUnsigned != 0
  790. // Convert to byte-coded string
  791. switch rows.columns[i].fieldType {
  792. case fieldTypeNULL:
  793. dest[i] = nil
  794. continue
  795. // Numeric Types
  796. case fieldTypeTiny:
  797. if unsigned {
  798. dest[i] = int64(data[pos])
  799. } else {
  800. dest[i] = int64(int8(data[pos]))
  801. }
  802. pos++
  803. continue
  804. case fieldTypeShort, fieldTypeYear:
  805. if unsigned {
  806. dest[i] = int64(binary.LittleEndian.Uint16(data[pos : pos+2]))
  807. } else {
  808. dest[i] = int64(int16(binary.LittleEndian.Uint16(data[pos : pos+2])))
  809. }
  810. pos += 2
  811. continue
  812. case fieldTypeInt24, fieldTypeLong:
  813. if unsigned {
  814. dest[i] = int64(binary.LittleEndian.Uint32(data[pos : pos+4]))
  815. } else {
  816. dest[i] = int64(int32(binary.LittleEndian.Uint32(data[pos : pos+4])))
  817. }
  818. pos += 4
  819. continue
  820. case fieldTypeLongLong:
  821. if unsigned {
  822. val := binary.LittleEndian.Uint64(data[pos : pos+8])
  823. if val > math.MaxInt64 {
  824. dest[i] = uint64ToString(val)
  825. } else {
  826. dest[i] = int64(val)
  827. }
  828. } else {
  829. dest[i] = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
  830. }
  831. pos += 8
  832. continue
  833. case fieldTypeFloat:
  834. dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
  835. pos += 4
  836. continue
  837. case fieldTypeDouble:
  838. dest[i] = math.Float64frombits(binary.LittleEndian.Uint64(data[pos : pos+8]))
  839. pos += 8
  840. continue
  841. // Length coded Binary Strings
  842. case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
  843. fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
  844. fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
  845. fieldTypeVarString, fieldTypeString, fieldTypeGeometry:
  846. var isNull bool
  847. dest[i], isNull, n, err = readLengthEnodedString(data[pos:])
  848. pos += n
  849. if err == nil {
  850. if !isNull {
  851. continue
  852. } else {
  853. dest[i] = nil
  854. continue
  855. }
  856. }
  857. return // err
  858. // Date YYYY-MM-DD
  859. case fieldTypeDate, fieldTypeNewDate:
  860. var num uint64
  861. var isNull bool
  862. num, isNull, n = readLengthEncodedInteger(data[pos:])
  863. pos += n
  864. if isNull {
  865. dest[i] = nil
  866. continue
  867. }
  868. if rows.mc.parseTime {
  869. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.loc)
  870. } else {
  871. dest[i], err = formatBinaryDate(num, data[pos:])
  872. }
  873. if err == nil {
  874. pos += int(num)
  875. continue
  876. } else {
  877. return err
  878. }
  879. // Time [-][H]HH:MM:SS[.fractal]
  880. case fieldTypeTime:
  881. var num uint64
  882. var isNull bool
  883. num, isNull, n = readLengthEncodedInteger(data[pos:])
  884. pos += n
  885. if num == 0 {
  886. if isNull {
  887. dest[i] = nil
  888. continue
  889. } else {
  890. dest[i] = []byte("00:00:00")
  891. continue
  892. }
  893. }
  894. var sign byte
  895. if data[pos] == 1 {
  896. sign = byte('-')
  897. }
  898. switch num {
  899. case 8:
  900. dest[i] = []byte(fmt.Sprintf(
  901. "%c%02d:%02d:%02d",
  902. sign,
  903. uint16(data[pos+1])*24+uint16(data[pos+5]),
  904. data[pos+6],
  905. data[pos+7],
  906. ))
  907. pos += 8
  908. continue
  909. case 12:
  910. dest[i] = []byte(fmt.Sprintf(
  911. "%c%02d:%02d:%02d.%06d",
  912. sign,
  913. uint16(data[pos+1])*24+uint16(data[pos+5]),
  914. data[pos+6],
  915. data[pos+7],
  916. binary.LittleEndian.Uint32(data[pos+8:pos+12]),
  917. ))
  918. pos += 12
  919. continue
  920. default:
  921. return fmt.Errorf("Invalid TIME-packet length %d", num)
  922. }
  923. // Timestamp YYYY-MM-DD HH:MM:SS[.fractal]
  924. case fieldTypeTimestamp, fieldTypeDateTime:
  925. var num uint64
  926. var isNull bool
  927. num, isNull, n = readLengthEncodedInteger(data[pos:])
  928. pos += n
  929. if isNull {
  930. dest[i] = nil
  931. continue
  932. }
  933. if rows.mc.parseTime {
  934. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.loc)
  935. } else {
  936. dest[i], err = formatBinaryDateTime(num, data[pos:])
  937. }
  938. if err == nil {
  939. pos += int(num)
  940. continue
  941. } else {
  942. return err
  943. }
  944. // Please report if this happens!
  945. default:
  946. return fmt.Errorf("Unknown FieldType %d", rows.columns[i].fieldType)
  947. }
  948. }
  949. return
  950. }