packets.go 28 KB

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