packets.go 32 KB

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