packets.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. // 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. func readStatus(b []byte) statusFlag {
  435. return statusFlag(b[0]) | statusFlag(b[1])<<8
  436. }
  437. // Ok Packet
  438. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
  439. func (mc *mysqlConn) handleOkPacket(data []byte) error {
  440. var n, m int
  441. // 0x00 [1 byte]
  442. // Affected rows [Length Coded Binary]
  443. mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
  444. // Insert id [Length Coded Binary]
  445. mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
  446. // server_status [2 bytes]
  447. mc.status = readStatus(data[1+n+m : 1+n+m+2])
  448. // warning count [2 bytes]
  449. if !mc.strict {
  450. return nil
  451. }
  452. pos := 1 + n + m + 2
  453. if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
  454. return mc.getWarnings()
  455. }
  456. return nil
  457. }
  458. // Read Packets as Field Packets until EOF-Packet or an Error appears
  459. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
  460. func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
  461. columns := make([]mysqlField, count)
  462. for i := 0; ; i++ {
  463. data, err := mc.readPacket()
  464. if err != nil {
  465. return nil, err
  466. }
  467. // EOF Packet
  468. if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
  469. if i == count {
  470. return columns, nil
  471. }
  472. return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
  473. }
  474. // Catalog
  475. pos, err := skipLengthEncodedString(data)
  476. if err != nil {
  477. return nil, err
  478. }
  479. // Database [len coded string]
  480. n, err := skipLengthEncodedString(data[pos:])
  481. if err != nil {
  482. return nil, err
  483. }
  484. pos += n
  485. // Table [len coded string]
  486. if mc.cfg.ColumnsWithAlias {
  487. tableName, _, n, err := readLengthEncodedString(data[pos:])
  488. if err != nil {
  489. return nil, err
  490. }
  491. pos += n
  492. columns[i].tableName = string(tableName)
  493. } else {
  494. n, err = skipLengthEncodedString(data[pos:])
  495. if err != nil {
  496. return nil, err
  497. }
  498. pos += n
  499. }
  500. // Original table [len coded string]
  501. n, err = skipLengthEncodedString(data[pos:])
  502. if err != nil {
  503. return nil, err
  504. }
  505. pos += n
  506. // Name [len coded string]
  507. name, _, n, err := readLengthEncodedString(data[pos:])
  508. if err != nil {
  509. return nil, err
  510. }
  511. columns[i].name = string(name)
  512. pos += n
  513. // Original name [len coded string]
  514. n, err = skipLengthEncodedString(data[pos:])
  515. if err != nil {
  516. return nil, err
  517. }
  518. // Filler [uint8]
  519. // Charset [charset, collation uint8]
  520. // Length [uint32]
  521. pos += n + 1 + 2 + 4
  522. // Field type [uint8]
  523. columns[i].fieldType = data[pos]
  524. pos++
  525. // Flags [uint16]
  526. columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  527. pos += 2
  528. // Decimals [uint8]
  529. columns[i].decimals = data[pos]
  530. //pos++
  531. // Default value [len coded binary]
  532. //if pos < len(data) {
  533. // defaultVal, _, err = bytesToLengthCodedBinary(data[pos:])
  534. //}
  535. }
  536. }
  537. // Read Packets as Field Packets until EOF-Packet or an Error appears
  538. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::ResultsetRow
  539. func (rows *textRows) readRow(dest []driver.Value) error {
  540. mc := rows.mc
  541. data, err := mc.readPacket()
  542. if err != nil {
  543. return err
  544. }
  545. // EOF Packet
  546. if data[0] == iEOF && len(data) == 5 {
  547. // server_status [2 bytes]
  548. rows.mc.status = readStatus(data[3:])
  549. if err := rows.mc.discardMoreResultsIfExists(); err != nil {
  550. return err
  551. }
  552. rows.mc = nil
  553. return io.EOF
  554. }
  555. if data[0] == iERR {
  556. rows.mc = nil
  557. return mc.handleErrorPacket(data)
  558. }
  559. // RowSet Packet
  560. var n int
  561. var isNull bool
  562. pos := 0
  563. for i := range dest {
  564. // Read bytes and convert to string
  565. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  566. pos += n
  567. if err == nil {
  568. if !isNull {
  569. if !mc.parseTime {
  570. continue
  571. } else {
  572. switch rows.columns[i].fieldType {
  573. case fieldTypeTimestamp, fieldTypeDateTime,
  574. fieldTypeDate, fieldTypeNewDate:
  575. dest[i], err = parseDateTime(
  576. string(dest[i].([]byte)),
  577. mc.cfg.Loc,
  578. )
  579. if err == nil {
  580. continue
  581. }
  582. default:
  583. continue
  584. }
  585. }
  586. } else {
  587. dest[i] = nil
  588. continue
  589. }
  590. }
  591. return err // err != nil
  592. }
  593. return nil
  594. }
  595. // Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
  596. func (mc *mysqlConn) readUntilEOF() error {
  597. for {
  598. data, err := mc.readPacket()
  599. // No Err and no EOF Packet
  600. if err == nil && data[0] != iEOF {
  601. continue
  602. }
  603. if err == nil && data[0] == iEOF && len(data) == 5 {
  604. mc.status = readStatus(data[3:])
  605. }
  606. return err // Err or EOF
  607. }
  608. }
  609. /******************************************************************************
  610. * Prepared Statements *
  611. ******************************************************************************/
  612. // Prepare Result Packets
  613. // http://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html
  614. func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
  615. data, err := stmt.mc.readPacket()
  616. if err == nil {
  617. // packet indicator [1 byte]
  618. if data[0] != iOK {
  619. return 0, stmt.mc.handleErrorPacket(data)
  620. }
  621. // statement id [4 bytes]
  622. stmt.id = binary.LittleEndian.Uint32(data[1:5])
  623. // Column count [16 bit uint]
  624. columnCount := binary.LittleEndian.Uint16(data[5:7])
  625. // Param count [16 bit uint]
  626. stmt.paramCount = int(binary.LittleEndian.Uint16(data[7:9]))
  627. // Reserved [8 bit]
  628. // Warning count [16 bit uint]
  629. if !stmt.mc.strict {
  630. return columnCount, nil
  631. }
  632. // Check for warnings count > 0, only available in MySQL > 4.1
  633. if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
  634. return columnCount, stmt.mc.getWarnings()
  635. }
  636. return columnCount, nil
  637. }
  638. return 0, err
  639. }
  640. // http://dev.mysql.com/doc/internals/en/com-stmt-send-long-data.html
  641. func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
  642. maxLen := stmt.mc.maxPacketAllowed - 1
  643. pktLen := maxLen
  644. // After the header (bytes 0-3) follows before the data:
  645. // 1 byte command
  646. // 4 bytes stmtID
  647. // 2 bytes paramID
  648. const dataOffset = 1 + 4 + 2
  649. // Can not use the write buffer since
  650. // a) the buffer is too small
  651. // b) it is in use
  652. data := make([]byte, 4+1+4+2+len(arg))
  653. copy(data[4+dataOffset:], arg)
  654. for argLen := len(arg); argLen > 0; argLen -= pktLen - dataOffset {
  655. if dataOffset+argLen < maxLen {
  656. pktLen = dataOffset + argLen
  657. }
  658. stmt.mc.sequence = 0
  659. // Add command byte [1 byte]
  660. data[4] = comStmtSendLongData
  661. // Add stmtID [32 bit]
  662. data[5] = byte(stmt.id)
  663. data[6] = byte(stmt.id >> 8)
  664. data[7] = byte(stmt.id >> 16)
  665. data[8] = byte(stmt.id >> 24)
  666. // Add paramID [16 bit]
  667. data[9] = byte(paramID)
  668. data[10] = byte(paramID >> 8)
  669. // Send CMD packet
  670. err := stmt.mc.writePacket(data[:4+pktLen])
  671. if err == nil {
  672. data = data[pktLen-dataOffset:]
  673. continue
  674. }
  675. return err
  676. }
  677. // Reset Packet Sequence
  678. stmt.mc.sequence = 0
  679. return nil
  680. }
  681. // Execute Prepared Statement
  682. // http://dev.mysql.com/doc/internals/en/com-stmt-execute.html
  683. func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
  684. if len(args) != stmt.paramCount {
  685. return fmt.Errorf(
  686. "argument count mismatch (got: %d; has: %d)",
  687. len(args),
  688. stmt.paramCount,
  689. )
  690. }
  691. const minPktLen = 4 + 1 + 4 + 1 + 4
  692. mc := stmt.mc
  693. // Reset packet-sequence
  694. mc.sequence = 0
  695. var data []byte
  696. if len(args) == 0 {
  697. data = mc.buf.takeBuffer(minPktLen)
  698. } else {
  699. data = mc.buf.takeCompleteBuffer()
  700. }
  701. if data == nil {
  702. // can not take the buffer. Something must be wrong with the connection
  703. errLog.Print(ErrBusyBuffer)
  704. return driver.ErrBadConn
  705. }
  706. // command [1 byte]
  707. data[4] = comStmtExecute
  708. // statement_id [4 bytes]
  709. data[5] = byte(stmt.id)
  710. data[6] = byte(stmt.id >> 8)
  711. data[7] = byte(stmt.id >> 16)
  712. data[8] = byte(stmt.id >> 24)
  713. // flags (0: CURSOR_TYPE_NO_CURSOR) [1 byte]
  714. data[9] = 0x00
  715. // iteration_count (uint32(1)) [4 bytes]
  716. data[10] = 0x01
  717. data[11] = 0x00
  718. data[12] = 0x00
  719. data[13] = 0x00
  720. if len(args) > 0 {
  721. pos := minPktLen
  722. var nullMask []byte
  723. if maskLen, typesLen := (len(args)+7)/8, 1+2*len(args); pos+maskLen+typesLen >= len(data) {
  724. // buffer has to be extended but we don't know by how much so
  725. // we depend on append after all data with known sizes fit.
  726. // We stop at that because we deal with a lot of columns here
  727. // which makes the required allocation size hard to guess.
  728. tmp := make([]byte, pos+maskLen+typesLen)
  729. copy(tmp[:pos], data[:pos])
  730. data = tmp
  731. nullMask = data[pos : pos+maskLen]
  732. pos += maskLen
  733. } else {
  734. nullMask = data[pos : pos+maskLen]
  735. for i := 0; i < maskLen; i++ {
  736. nullMask[i] = 0
  737. }
  738. pos += maskLen
  739. }
  740. // newParameterBoundFlag 1 [1 byte]
  741. data[pos] = 0x01
  742. pos++
  743. // type of each parameter [len(args)*2 bytes]
  744. paramTypes := data[pos:]
  745. pos += len(args) * 2
  746. // value of each parameter [n bytes]
  747. paramValues := data[pos:pos]
  748. valuesCap := cap(paramValues)
  749. for i, arg := range args {
  750. // build NULL-bitmap
  751. if arg == nil {
  752. nullMask[i/8] |= 1 << (uint(i) & 7)
  753. paramTypes[i+i] = fieldTypeNULL
  754. paramTypes[i+i+1] = 0x00
  755. continue
  756. }
  757. // cache types and values
  758. switch v := arg.(type) {
  759. case int64:
  760. paramTypes[i+i] = fieldTypeLongLong
  761. paramTypes[i+i+1] = 0x00
  762. if cap(paramValues)-len(paramValues)-8 >= 0 {
  763. paramValues = paramValues[:len(paramValues)+8]
  764. binary.LittleEndian.PutUint64(
  765. paramValues[len(paramValues)-8:],
  766. uint64(v),
  767. )
  768. } else {
  769. paramValues = append(paramValues,
  770. uint64ToBytes(uint64(v))...,
  771. )
  772. }
  773. case float64:
  774. paramTypes[i+i] = fieldTypeDouble
  775. paramTypes[i+i+1] = 0x00
  776. if cap(paramValues)-len(paramValues)-8 >= 0 {
  777. paramValues = paramValues[:len(paramValues)+8]
  778. binary.LittleEndian.PutUint64(
  779. paramValues[len(paramValues)-8:],
  780. math.Float64bits(v),
  781. )
  782. } else {
  783. paramValues = append(paramValues,
  784. uint64ToBytes(math.Float64bits(v))...,
  785. )
  786. }
  787. case bool:
  788. paramTypes[i+i] = fieldTypeTiny
  789. paramTypes[i+i+1] = 0x00
  790. if v {
  791. paramValues = append(paramValues, 0x01)
  792. } else {
  793. paramValues = append(paramValues, 0x00)
  794. }
  795. case []byte:
  796. // Common case (non-nil value) first
  797. if v != nil {
  798. paramTypes[i+i] = fieldTypeString
  799. paramTypes[i+i+1] = 0x00
  800. if len(v) < mc.maxPacketAllowed-pos-len(paramValues)-(len(args)-(i+1))*64 {
  801. paramValues = appendLengthEncodedInteger(paramValues,
  802. uint64(len(v)),
  803. )
  804. paramValues = append(paramValues, v...)
  805. } else {
  806. if err := stmt.writeCommandLongData(i, v); err != nil {
  807. return err
  808. }
  809. }
  810. continue
  811. }
  812. // Handle []byte(nil) as a NULL value
  813. nullMask[i/8] |= 1 << (uint(i) & 7)
  814. paramTypes[i+i] = fieldTypeNULL
  815. paramTypes[i+i+1] = 0x00
  816. case string:
  817. paramTypes[i+i] = fieldTypeString
  818. paramTypes[i+i+1] = 0x00
  819. if len(v) < mc.maxPacketAllowed-pos-len(paramValues)-(len(args)-(i+1))*64 {
  820. paramValues = appendLengthEncodedInteger(paramValues,
  821. uint64(len(v)),
  822. )
  823. paramValues = append(paramValues, v...)
  824. } else {
  825. if err := stmt.writeCommandLongData(i, []byte(v)); err != nil {
  826. return err
  827. }
  828. }
  829. case time.Time:
  830. paramTypes[i+i] = fieldTypeString
  831. paramTypes[i+i+1] = 0x00
  832. var val []byte
  833. if v.IsZero() {
  834. val = []byte("0000-00-00")
  835. } else {
  836. val = []byte(v.In(mc.cfg.Loc).Format(timeFormat))
  837. }
  838. paramValues = appendLengthEncodedInteger(paramValues,
  839. uint64(len(val)),
  840. )
  841. paramValues = append(paramValues, val...)
  842. default:
  843. return fmt.Errorf("can not convert type: %T", arg)
  844. }
  845. }
  846. // Check if param values exceeded the available buffer
  847. // In that case we must build the data packet with the new values buffer
  848. if valuesCap != cap(paramValues) {
  849. data = append(data[:pos], paramValues...)
  850. mc.buf.buf = data
  851. }
  852. pos += len(paramValues)
  853. data = data[:pos]
  854. }
  855. return mc.writePacket(data)
  856. }
  857. func (mc *mysqlConn) discardMoreResultsIfExists() error {
  858. for mc.status&statusMoreResultsExists != 0 {
  859. resLen, err := mc.readResultSetHeaderPacket()
  860. if err != nil {
  861. return err
  862. }
  863. if resLen > 0 {
  864. // columns
  865. if err := mc.readUntilEOF(); err != nil {
  866. return err
  867. }
  868. // rows
  869. if err := mc.readUntilEOF(); err != nil {
  870. return err
  871. }
  872. } else {
  873. mc.status &^= statusMoreResultsExists
  874. }
  875. }
  876. return nil
  877. }
  878. // http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
  879. func (rows *binaryRows) readRow(dest []driver.Value) error {
  880. data, err := rows.mc.readPacket()
  881. if err != nil {
  882. return err
  883. }
  884. // packet indicator [1 byte]
  885. if data[0] != iOK {
  886. // EOF Packet
  887. if data[0] == iEOF && len(data) == 5 {
  888. rows.mc.status = readStatus(data[3:])
  889. if err := rows.mc.discardMoreResultsIfExists(); err != nil {
  890. return err
  891. }
  892. rows.mc = nil
  893. return io.EOF
  894. }
  895. rows.mc = nil
  896. // Error otherwise
  897. return rows.mc.handleErrorPacket(data)
  898. }
  899. // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
  900. pos := 1 + (len(dest)+7+2)>>3
  901. nullMask := data[1:pos]
  902. for i := range dest {
  903. // Field is NULL
  904. // (byte >> bit-pos) % 2 == 1
  905. if ((nullMask[(i+2)>>3] >> uint((i+2)&7)) & 1) == 1 {
  906. dest[i] = nil
  907. continue
  908. }
  909. // Convert to byte-coded string
  910. switch rows.columns[i].fieldType {
  911. case fieldTypeNULL:
  912. dest[i] = nil
  913. continue
  914. // Numeric Types
  915. case fieldTypeTiny:
  916. if rows.columns[i].flags&flagUnsigned != 0 {
  917. dest[i] = int64(data[pos])
  918. } else {
  919. dest[i] = int64(int8(data[pos]))
  920. }
  921. pos++
  922. continue
  923. case fieldTypeShort, fieldTypeYear:
  924. if rows.columns[i].flags&flagUnsigned != 0 {
  925. dest[i] = int64(binary.LittleEndian.Uint16(data[pos : pos+2]))
  926. } else {
  927. dest[i] = int64(int16(binary.LittleEndian.Uint16(data[pos : pos+2])))
  928. }
  929. pos += 2
  930. continue
  931. case fieldTypeInt24, fieldTypeLong:
  932. if rows.columns[i].flags&flagUnsigned != 0 {
  933. dest[i] = int64(binary.LittleEndian.Uint32(data[pos : pos+4]))
  934. } else {
  935. dest[i] = int64(int32(binary.LittleEndian.Uint32(data[pos : pos+4])))
  936. }
  937. pos += 4
  938. continue
  939. case fieldTypeLongLong:
  940. if rows.columns[i].flags&flagUnsigned != 0 {
  941. val := binary.LittleEndian.Uint64(data[pos : pos+8])
  942. if val > math.MaxInt64 {
  943. dest[i] = uint64ToString(val)
  944. } else {
  945. dest[i] = int64(val)
  946. }
  947. } else {
  948. dest[i] = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
  949. }
  950. pos += 8
  951. continue
  952. case fieldTypeFloat:
  953. dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
  954. pos += 4
  955. continue
  956. case fieldTypeDouble:
  957. dest[i] = math.Float64frombits(binary.LittleEndian.Uint64(data[pos : pos+8]))
  958. pos += 8
  959. continue
  960. // Length coded Binary Strings
  961. case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
  962. fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
  963. fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
  964. fieldTypeVarString, fieldTypeString, fieldTypeGeometry:
  965. var isNull bool
  966. var n int
  967. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  968. pos += n
  969. if err == nil {
  970. if !isNull {
  971. continue
  972. } else {
  973. dest[i] = nil
  974. continue
  975. }
  976. }
  977. return err
  978. case
  979. fieldTypeDate, fieldTypeNewDate, // Date YYYY-MM-DD
  980. fieldTypeTime, // Time [-][H]HH:MM:SS[.fractal]
  981. fieldTypeTimestamp, fieldTypeDateTime: // Timestamp YYYY-MM-DD HH:MM:SS[.fractal]
  982. num, isNull, n := readLengthEncodedInteger(data[pos:])
  983. pos += n
  984. switch {
  985. case isNull:
  986. dest[i] = nil
  987. continue
  988. case rows.columns[i].fieldType == fieldTypeTime:
  989. // database/sql does not support an equivalent to TIME, return a string
  990. var dstlen uint8
  991. switch decimals := rows.columns[i].decimals; decimals {
  992. case 0x00, 0x1f:
  993. dstlen = 8
  994. case 1, 2, 3, 4, 5, 6:
  995. dstlen = 8 + 1 + decimals
  996. default:
  997. return fmt.Errorf(
  998. "protocol error, illegal decimals value %d",
  999. rows.columns[i].decimals,
  1000. )
  1001. }
  1002. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, true)
  1003. case rows.mc.parseTime:
  1004. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.Loc)
  1005. default:
  1006. var dstlen uint8
  1007. if rows.columns[i].fieldType == fieldTypeDate {
  1008. dstlen = 10
  1009. } else {
  1010. switch decimals := rows.columns[i].decimals; decimals {
  1011. case 0x00, 0x1f:
  1012. dstlen = 19
  1013. case 1, 2, 3, 4, 5, 6:
  1014. dstlen = 19 + 1 + decimals
  1015. default:
  1016. return fmt.Errorf(
  1017. "protocol error, illegal decimals value %d",
  1018. rows.columns[i].decimals,
  1019. )
  1020. }
  1021. }
  1022. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, false)
  1023. }
  1024. if err == nil {
  1025. pos += int(num)
  1026. continue
  1027. } else {
  1028. return err
  1029. }
  1030. // Please report if this happens!
  1031. default:
  1032. return fmt.Errorf("unknown field type %d", rows.columns[i].fieldType)
  1033. }
  1034. }
  1035. return nil
  1036. }