packets.go 29 KB

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