packets.go 28 KB

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