connection.go 15 KB

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