packets.go 28 KB

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