connection.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. "context"
  11. "database/sql"
  12. "database/sql/driver"
  13. "encoding/json"
  14. "io"
  15. "net"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. type mysqlConn struct {
  21. buf buffer
  22. netConn net.Conn
  23. rawConn net.Conn // underlying connection when netConn is TLS connection.
  24. affectedRows uint64
  25. insertId uint64
  26. cfg *Config
  27. maxAllowedPacket int
  28. maxWriteSize int
  29. writeTimeout time.Duration
  30. flags clientFlag
  31. status statusFlag
  32. sequence uint8
  33. parseTime bool
  34. reset bool // set when the Go SQL package calls ResetSession
  35. // for context support (Go 1.8+)
  36. watching bool
  37. watcher chan<- context.Context
  38. closech chan struct{}
  39. finished chan<- struct{}
  40. canceled atomicError // set non-nil if conn is canceled
  41. closed atomicBool // set when conn is closed, before closech is closed
  42. }
  43. // Handles parameters set in DSN after the connection is established
  44. func (mc *mysqlConn) handleParams() (err error) {
  45. var cmdSet strings.Builder
  46. for param, val := range mc.cfg.Params {
  47. switch param {
  48. // Charset: character_set_connection, character_set_client, character_set_results
  49. case "charset":
  50. charsets := strings.Split(val, ",")
  51. for i := range charsets {
  52. // ignore errors here - a charset may not exist
  53. err = mc.exec("SET NAMES " + charsets[i])
  54. if err == nil {
  55. break
  56. }
  57. }
  58. if err != nil {
  59. return
  60. }
  61. // Other system vars accumulated in a single SET command
  62. default:
  63. if cmdSet.Len() == 0 {
  64. // Heuristic: 29 chars for each other key=value to reduce reallocations
  65. cmdSet.Grow(4 + len(param) + 1 + len(val) + 30*(len(mc.cfg.Params)-1))
  66. cmdSet.WriteString("SET ")
  67. } else {
  68. cmdSet.WriteByte(',')
  69. }
  70. cmdSet.WriteString(param)
  71. cmdSet.WriteByte('=')
  72. cmdSet.WriteString(val)
  73. }
  74. }
  75. if cmdSet.Len() > 0 {
  76. err = mc.exec(cmdSet.String())
  77. if err != nil {
  78. return
  79. }
  80. }
  81. return
  82. }
  83. func (mc *mysqlConn) markBadConn(err error) error {
  84. if mc == nil {
  85. return err
  86. }
  87. if err != errBadConnNoWrite {
  88. return err
  89. }
  90. return driver.ErrBadConn
  91. }
  92. func (mc *mysqlConn) Begin() (driver.Tx, error) {
  93. return mc.begin(false)
  94. }
  95. func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
  96. if mc.closed.Load() {
  97. errLog.Print(ErrInvalidConn)
  98. return nil, driver.ErrBadConn
  99. }
  100. var q string
  101. if readOnly {
  102. q = "START TRANSACTION READ ONLY"
  103. } else {
  104. q = "START TRANSACTION"
  105. }
  106. err := mc.exec(q)
  107. if err == nil {
  108. return &mysqlTx{mc}, err
  109. }
  110. return nil, mc.markBadConn(err)
  111. }
  112. func (mc *mysqlConn) Close() (err error) {
  113. // Makes Close idempotent
  114. if !mc.closed.Load() {
  115. err = mc.writeCommandPacket(comQuit)
  116. }
  117. mc.cleanup()
  118. return
  119. }
  120. // Closes the network connection and unsets internal variables. Do not call this
  121. // function after successfully authentication, call Close instead. This function
  122. // is called before auth or on auth failure because MySQL will have already
  123. // closed the network connection.
  124. func (mc *mysqlConn) cleanup() {
  125. if mc.closed.Swap(true) {
  126. return
  127. }
  128. // Makes cleanup idempotent
  129. close(mc.closech)
  130. if mc.netConn == nil {
  131. return
  132. }
  133. if err := mc.netConn.Close(); err != nil {
  134. errLog.Print(err)
  135. }
  136. }
  137. func (mc *mysqlConn) error() error {
  138. if mc.closed.Load() {
  139. if err := mc.canceled.Value(); err != nil {
  140. return err
  141. }
  142. return ErrInvalidConn
  143. }
  144. return nil
  145. }
  146. func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
  147. if mc.closed.Load() {
  148. errLog.Print(ErrInvalidConn)
  149. return nil, driver.ErrBadConn
  150. }
  151. // Send command
  152. err := mc.writeCommandPacketStr(comStmtPrepare, query)
  153. if err != nil {
  154. // STMT_PREPARE is safe to retry. So we can return ErrBadConn here.
  155. errLog.Print(err)
  156. return nil, driver.ErrBadConn
  157. }
  158. stmt := &mysqlStmt{
  159. mc: mc,
  160. }
  161. // Read Result
  162. columnCount, err := stmt.readPrepareResultPacket()
  163. if err == nil {
  164. if stmt.paramCount > 0 {
  165. if err = mc.readUntilEOF(); err != nil {
  166. return nil, err
  167. }
  168. }
  169. if columnCount > 0 {
  170. err = mc.readUntilEOF()
  171. }
  172. }
  173. return stmt, err
  174. }
  175. func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) {
  176. // Number of ? should be same to len(args)
  177. if strings.Count(query, "?") != len(args) {
  178. return "", driver.ErrSkip
  179. }
  180. buf, err := mc.buf.takeCompleteBuffer()
  181. if err != nil {
  182. // can not take the buffer. Something must be wrong with the connection
  183. errLog.Print(err)
  184. return "", ErrInvalidConn
  185. }
  186. buf = buf[:0]
  187. argPos := 0
  188. for i := 0; i < len(query); i++ {
  189. q := strings.IndexByte(query[i:], '?')
  190. if q == -1 {
  191. buf = append(buf, query[i:]...)
  192. break
  193. }
  194. buf = append(buf, query[i:i+q]...)
  195. i += q
  196. arg := args[argPos]
  197. argPos++
  198. if arg == nil {
  199. buf = append(buf, "NULL"...)
  200. continue
  201. }
  202. switch v := arg.(type) {
  203. case int64:
  204. buf = strconv.AppendInt(buf, v, 10)
  205. case uint64:
  206. // Handle uint64 explicitly because our custom ConvertValue emits unsigned values
  207. buf = strconv.AppendUint(buf, v, 10)
  208. case float64:
  209. buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
  210. case bool:
  211. if v {
  212. buf = append(buf, '1')
  213. } else {
  214. buf = append(buf, '0')
  215. }
  216. case time.Time:
  217. if v.IsZero() {
  218. buf = append(buf, "'0000-00-00'"...)
  219. } else {
  220. buf = append(buf, '\'')
  221. buf, err = appendDateTime(buf, v.In(mc.cfg.Loc))
  222. if err != nil {
  223. return "", err
  224. }
  225. buf = append(buf, '\'')
  226. }
  227. case json.RawMessage:
  228. buf = append(buf, '\'')
  229. if mc.status&statusNoBackslashEscapes == 0 {
  230. buf = escapeBytesBackslash(buf, v)
  231. } else {
  232. buf = escapeBytesQuotes(buf, v)
  233. }
  234. buf = append(buf, '\'')
  235. case []byte:
  236. if v == nil {
  237. buf = append(buf, "NULL"...)
  238. } else {
  239. buf = append(buf, "_binary'"...)
  240. if mc.status&statusNoBackslashEscapes == 0 {
  241. buf = escapeBytesBackslash(buf, v)
  242. } else {
  243. buf = escapeBytesQuotes(buf, v)
  244. }
  245. buf = append(buf, '\'')
  246. }
  247. case string:
  248. buf = append(buf, '\'')
  249. if mc.status&statusNoBackslashEscapes == 0 {
  250. buf = escapeStringBackslash(buf, v)
  251. } else {
  252. buf = escapeStringQuotes(buf, v)
  253. }
  254. buf = append(buf, '\'')
  255. default:
  256. return "", driver.ErrSkip
  257. }
  258. if len(buf)+4 > mc.maxAllowedPacket {
  259. return "", driver.ErrSkip
  260. }
  261. }
  262. if argPos != len(args) {
  263. return "", driver.ErrSkip
  264. }
  265. return string(buf), nil
  266. }
  267. func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  268. if mc.closed.Load() {
  269. errLog.Print(ErrInvalidConn)
  270. return nil, driver.ErrBadConn
  271. }
  272. if len(args) != 0 {
  273. if !mc.cfg.InterpolateParams {
  274. return nil, driver.ErrSkip
  275. }
  276. // try to interpolate the parameters to save extra roundtrips for preparing and closing a statement
  277. prepared, err := mc.interpolateParams(query, args)
  278. if err != nil {
  279. return nil, err
  280. }
  281. query = prepared
  282. }
  283. mc.affectedRows = 0
  284. mc.insertId = 0
  285. err := mc.exec(query)
  286. if err == nil {
  287. return &mysqlResult{
  288. affectedRows: int64(mc.affectedRows),
  289. insertId: int64(mc.insertId),
  290. }, err
  291. }
  292. return nil, mc.markBadConn(err)
  293. }
  294. // Internal function to execute commands
  295. func (mc *mysqlConn) exec(query string) error {
  296. // Send command
  297. if err := mc.writeCommandPacketStr(comQuery, query); err != nil {
  298. return mc.markBadConn(err)
  299. }
  300. // Read Result
  301. resLen, err := mc.readResultSetHeaderPacket()
  302. if err != nil {
  303. return err
  304. }
  305. if resLen > 0 {
  306. // columns
  307. if err := mc.readUntilEOF(); err != nil {
  308. return err
  309. }
  310. // rows
  311. if err := mc.readUntilEOF(); err != nil {
  312. return err
  313. }
  314. }
  315. return mc.discardResults()
  316. }
  317. func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  318. return mc.query(query, args)
  319. }
  320. func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
  321. if mc.closed.Load() {
  322. errLog.Print(ErrInvalidConn)
  323. return nil, driver.ErrBadConn
  324. }
  325. if len(args) != 0 {
  326. if !mc.cfg.InterpolateParams {
  327. return nil, driver.ErrSkip
  328. }
  329. // try client-side prepare to reduce roundtrip
  330. prepared, err := mc.interpolateParams(query, args)
  331. if err != nil {
  332. return nil, err
  333. }
  334. query = prepared
  335. }
  336. // Send command
  337. err := mc.writeCommandPacketStr(comQuery, query)
  338. if err == nil {
  339. // Read Result
  340. var resLen int
  341. resLen, err = mc.readResultSetHeaderPacket()
  342. if err == nil {
  343. rows := new(textRows)
  344. rows.mc = mc
  345. if resLen == 0 {
  346. rows.rs.done = true
  347. switch err := rows.NextResultSet(); err {
  348. case nil, io.EOF:
  349. return rows, nil
  350. default:
  351. return nil, err
  352. }
  353. }
  354. // Columns
  355. rows.rs.columns, err = mc.readColumns(resLen)
  356. return rows, err
  357. }
  358. }
  359. return nil, mc.markBadConn(err)
  360. }
  361. // Gets the value of the given MySQL System Variable
  362. // The returned byte slice is only valid until the next read
  363. func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
  364. // Send command
  365. if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil {
  366. return nil, err
  367. }
  368. // Read Result
  369. resLen, err := mc.readResultSetHeaderPacket()
  370. if err == nil {
  371. rows := new(textRows)
  372. rows.mc = mc
  373. rows.rs.columns = []mysqlField{{fieldType: fieldTypeVarChar}}
  374. if resLen > 0 {
  375. // Columns
  376. if err := mc.readUntilEOF(); err != nil {
  377. return nil, err
  378. }
  379. }
  380. dest := make([]driver.Value, resLen)
  381. if err = rows.readRow(dest); err == nil {
  382. return dest[0].([]byte), mc.readUntilEOF()
  383. }
  384. }
  385. return nil, err
  386. }
  387. // finish is called when the query has canceled.
  388. func (mc *mysqlConn) cancel(err error) {
  389. mc.canceled.Set(err)
  390. mc.cleanup()
  391. }
  392. // finish is called when the query has succeeded.
  393. func (mc *mysqlConn) finish() {
  394. if !mc.watching || mc.finished == nil {
  395. return
  396. }
  397. select {
  398. case mc.finished <- struct{}{}:
  399. mc.watching = false
  400. case <-mc.closech:
  401. }
  402. }
  403. // Ping implements driver.Pinger interface
  404. func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
  405. if mc.closed.Load() {
  406. errLog.Print(ErrInvalidConn)
  407. return driver.ErrBadConn
  408. }
  409. if err = mc.watchCancel(ctx); err != nil {
  410. return
  411. }
  412. defer mc.finish()
  413. if err = mc.writeCommandPacket(comPing); err != nil {
  414. return mc.markBadConn(err)
  415. }
  416. return mc.readResultOK()
  417. }
  418. // BeginTx implements driver.ConnBeginTx interface
  419. func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
  420. if mc.closed.Load() {
  421. return nil, driver.ErrBadConn
  422. }
  423. if err := mc.watchCancel(ctx); err != nil {
  424. return nil, err
  425. }
  426. defer mc.finish()
  427. if sql.IsolationLevel(opts.Isolation) != sql.LevelDefault {
  428. level, err := mapIsolationLevel(opts.Isolation)
  429. if err != nil {
  430. return nil, err
  431. }
  432. err = mc.exec("SET TRANSACTION ISOLATION LEVEL " + level)
  433. if err != nil {
  434. return nil, err
  435. }
  436. }
  437. return mc.begin(opts.ReadOnly)
  438. }
  439. func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
  440. dargs, err := namedValueToValue(args)
  441. if err != nil {
  442. return nil, err
  443. }
  444. if err := mc.watchCancel(ctx); err != nil {
  445. return nil, err
  446. }
  447. rows, err := mc.query(query, dargs)
  448. if err != nil {
  449. mc.finish()
  450. return nil, err
  451. }
  452. rows.finish = mc.finish
  453. return rows, err
  454. }
  455. func (mc *mysqlConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
  456. dargs, err := namedValueToValue(args)
  457. if err != nil {
  458. return nil, err
  459. }
  460. if err := mc.watchCancel(ctx); err != nil {
  461. return nil, err
  462. }
  463. defer mc.finish()
  464. return mc.Exec(query, dargs)
  465. }
  466. func (mc *mysqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
  467. if err := mc.watchCancel(ctx); err != nil {
  468. return nil, err
  469. }
  470. stmt, err := mc.Prepare(query)
  471. mc.finish()
  472. if err != nil {
  473. return nil, err
  474. }
  475. select {
  476. default:
  477. case <-ctx.Done():
  478. stmt.Close()
  479. return nil, ctx.Err()
  480. }
  481. return stmt, nil
  482. }
  483. func (stmt *mysqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
  484. dargs, err := namedValueToValue(args)
  485. if err != nil {
  486. return nil, err
  487. }
  488. if err := stmt.mc.watchCancel(ctx); err != nil {
  489. return nil, err
  490. }
  491. rows, err := stmt.query(dargs)
  492. if err != nil {
  493. stmt.mc.finish()
  494. return nil, err
  495. }
  496. rows.finish = stmt.mc.finish
  497. return rows, err
  498. }
  499. func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
  500. dargs, err := namedValueToValue(args)
  501. if err != nil {
  502. return nil, err
  503. }
  504. if err := stmt.mc.watchCancel(ctx); err != nil {
  505. return nil, err
  506. }
  507. defer stmt.mc.finish()
  508. return stmt.Exec(dargs)
  509. }
  510. func (mc *mysqlConn) watchCancel(ctx context.Context) error {
  511. if mc.watching {
  512. // Reach here if canceled,
  513. // so the connection is already invalid
  514. mc.cleanup()
  515. return nil
  516. }
  517. // When ctx is already cancelled, don't watch it.
  518. if err := ctx.Err(); err != nil {
  519. return err
  520. }
  521. // When ctx is not cancellable, don't watch it.
  522. if ctx.Done() == nil {
  523. return nil
  524. }
  525. // When watcher is not alive, can't watch it.
  526. if mc.watcher == nil {
  527. return nil
  528. }
  529. mc.watching = true
  530. mc.watcher <- ctx
  531. return nil
  532. }
  533. func (mc *mysqlConn) startWatcher() {
  534. watcher := make(chan context.Context, 1)
  535. mc.watcher = watcher
  536. finished := make(chan struct{})
  537. mc.finished = finished
  538. go func() {
  539. for {
  540. var ctx context.Context
  541. select {
  542. case ctx = <-watcher:
  543. case <-mc.closech:
  544. return
  545. }
  546. select {
  547. case <-ctx.Done():
  548. mc.cancel(ctx.Err())
  549. case <-finished:
  550. case <-mc.closech:
  551. return
  552. }
  553. }
  554. }()
  555. }
  556. func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
  557. nv.Value, err = converter{}.ConvertValue(nv.Value)
  558. return
  559. }
  560. // ResetSession implements driver.SessionResetter.
  561. // (From Go 1.10)
  562. func (mc *mysqlConn) ResetSession(ctx context.Context) error {
  563. if mc.closed.Load() {
  564. return driver.ErrBadConn
  565. }
  566. mc.reset = true
  567. return nil
  568. }
  569. // IsValid implements driver.Validator interface
  570. // (From Go 1.15)
  571. func (mc *mysqlConn) IsValid() bool {
  572. return !mc.closed.Load()
  573. }