packets.go 28 KB

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