packets.go 28 KB

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