forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/connection.go

1133 lines
14 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"io"
"net"
"strconv"
"strings"
"time"
)
type mysqlConn struct {
buf buffer
netConn net.Conn
rawConn net.Conn // underlying connection when netConn is TLS connection.
affectedRows uint64
insertId uint64
cfg *Config
2024-02-18 10:42:21 +00:00
maxAllowedPacket int
maxWriteSize int
writeTimeout time.Duration
flags clientFlag
status statusFlag
sequence uint8
parseTime bool
reset bool // set when the Go SQL package calls ResetSession
2024-02-18 10:42:21 +00:00
// for context support (Go 1.8+)
2024-02-18 10:42:21 +00:00
watching bool
watcher chan<- context.Context
closech chan struct{}
2024-02-18 10:42:21 +00:00
finished chan<- struct{}
2024-02-18 10:42:21 +00:00
canceled atomicError // set non-nil if conn is canceled
closed atomicBool // set when conn is closed, before closech is closed
2024-02-18 10:42:21 +00:00
}
// Handles parameters set in DSN after the connection is established
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) handleParams() (err error) {
2024-02-18 10:42:21 +00:00
var cmdSet strings.Builder
2024-02-18 10:42:21 +00:00
for param, val := range mc.cfg.Params {
2024-02-18 10:42:21 +00:00
switch param {
2024-02-18 10:42:21 +00:00
// Charset: character_set_connection, character_set_client, character_set_results
2024-02-18 10:42:21 +00:00
case "charset":
2024-02-18 10:42:21 +00:00
charsets := strings.Split(val, ",")
2024-02-18 10:42:21 +00:00
for i := range charsets {
2024-02-18 10:42:21 +00:00
// ignore errors here - a charset may not exist
2024-02-18 10:42:21 +00:00
err = mc.exec("SET NAMES " + charsets[i])
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// Other system vars accumulated in a single SET command
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
if cmdSet.Len() == 0 {
2024-02-18 10:42:21 +00:00
// Heuristic: 29 chars for each other key=value to reduce reallocations
2024-02-18 10:42:21 +00:00
cmdSet.Grow(4 + len(param) + 1 + len(val) + 30*(len(mc.cfg.Params)-1))
2024-02-18 10:42:21 +00:00
cmdSet.WriteString("SET ")
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cmdSet.WriteByte(',')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmdSet.WriteString(param)
2024-02-18 10:42:21 +00:00
cmdSet.WriteByte('=')
2024-02-18 10:42:21 +00:00
cmdSet.WriteString(val)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if cmdSet.Len() > 0 {
2024-02-18 10:42:21 +00:00
err = mc.exec(cmdSet.String())
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) markBadConn(err error) error {
2024-02-18 10:42:21 +00:00
if mc == nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != errBadConnNoWrite {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) Begin() (driver.Tx, error) {
2024-02-18 10:42:21 +00:00
return mc.begin(false)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
errLog.Print(ErrInvalidConn)
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var q string
2024-02-18 10:42:21 +00:00
if readOnly {
2024-02-18 10:42:21 +00:00
q = "START TRANSACTION READ ONLY"
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
q = "START TRANSACTION"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err := mc.exec(q)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return &mysqlTx{mc}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, mc.markBadConn(err)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) Close() (err error) {
2024-02-18 10:42:21 +00:00
// Makes Close idempotent
2024-02-18 10:42:21 +00:00
if !mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
err = mc.writeCommandPacket(comQuit)
2024-02-18 10:42:21 +00:00
}
mc.cleanup()
return
2024-02-18 10:42:21 +00:00
}
// Closes the network connection and unsets internal variables. Do not call this
2024-02-18 10:42:21 +00:00
// function after successfully authentication, call Close instead. This function
2024-02-18 10:42:21 +00:00
// is called before auth or on auth failure because MySQL will have already
2024-02-18 10:42:21 +00:00
// closed the network connection.
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) cleanup() {
2024-02-18 10:42:21 +00:00
if !mc.closed.TrySet(true) {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// Makes cleanup idempotent
2024-02-18 10:42:21 +00:00
close(mc.closech)
2024-02-18 10:42:21 +00:00
if mc.netConn == nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err := mc.netConn.Close(); err != nil {
2024-02-18 10:42:21 +00:00
errLog.Print(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) error() error {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
if err := mc.canceled.Value(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ErrInvalidConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
errLog.Print(ErrInvalidConn)
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Send command
2024-02-18 10:42:21 +00:00
err := mc.writeCommandPacketStr(comStmtPrepare, query)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
// STMT_PREPARE is safe to retry. So we can return ErrBadConn here.
2024-02-18 10:42:21 +00:00
errLog.Print(err)
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
stmt := &mysqlStmt{
2024-02-18 10:42:21 +00:00
mc: mc,
}
// Read Result
2024-02-18 10:42:21 +00:00
columnCount, err := stmt.readPrepareResultPacket()
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if stmt.paramCount > 0 {
2024-02-18 10:42:21 +00:00
if err = mc.readUntilEOF(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if columnCount > 0 {
2024-02-18 10:42:21 +00:00
err = mc.readUntilEOF()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return stmt, err
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) {
2024-02-18 10:42:21 +00:00
// Number of ? should be same to len(args)
2024-02-18 10:42:21 +00:00
if strings.Count(query, "?") != len(args) {
2024-02-18 10:42:21 +00:00
return "", driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
buf, err := mc.buf.takeCompleteBuffer()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
// can not take the buffer. Something must be wrong with the connection
2024-02-18 10:42:21 +00:00
errLog.Print(err)
2024-02-18 10:42:21 +00:00
return "", ErrInvalidConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = buf[:0]
2024-02-18 10:42:21 +00:00
argPos := 0
for i := 0; i < len(query); i++ {
2024-02-18 10:42:21 +00:00
q := strings.IndexByte(query[i:], '?')
2024-02-18 10:42:21 +00:00
if q == -1 {
2024-02-18 10:42:21 +00:00
buf = append(buf, query[i:]...)
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = append(buf, query[i:i+q]...)
2024-02-18 10:42:21 +00:00
i += q
arg := args[argPos]
2024-02-18 10:42:21 +00:00
argPos++
if arg == nil {
2024-02-18 10:42:21 +00:00
buf = append(buf, "NULL"...)
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
switch v := arg.(type) {
2024-02-18 10:42:21 +00:00
case int64:
2024-02-18 10:42:21 +00:00
buf = strconv.AppendInt(buf, v, 10)
2024-02-18 10:42:21 +00:00
case uint64:
2024-02-18 10:42:21 +00:00
// Handle uint64 explicitly because our custom ConvertValue emits unsigned values
2024-02-18 10:42:21 +00:00
buf = strconv.AppendUint(buf, v, 10)
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
2024-02-18 10:42:21 +00:00
case bool:
2024-02-18 10:42:21 +00:00
if v {
2024-02-18 10:42:21 +00:00
buf = append(buf, '1')
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = append(buf, '0')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case time.Time:
2024-02-18 10:42:21 +00:00
if v.IsZero() {
2024-02-18 10:42:21 +00:00
buf = append(buf, "'0000-00-00'"...)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
buf, err = appendDateTime(buf, v.In(mc.cfg.Loc))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case json.RawMessage:
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
if mc.status&statusNoBackslashEscapes == 0 {
2024-02-18 10:42:21 +00:00
buf = escapeBytesBackslash(buf, v)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = escapeBytesQuotes(buf, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
case []byte:
2024-02-18 10:42:21 +00:00
if v == nil {
2024-02-18 10:42:21 +00:00
buf = append(buf, "NULL"...)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = append(buf, "_binary'"...)
2024-02-18 10:42:21 +00:00
if mc.status&statusNoBackslashEscapes == 0 {
2024-02-18 10:42:21 +00:00
buf = escapeBytesBackslash(buf, v)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = escapeBytesQuotes(buf, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
if mc.status&statusNoBackslashEscapes == 0 {
2024-02-18 10:42:21 +00:00
buf = escapeStringBackslash(buf, v)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf = escapeStringQuotes(buf, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = append(buf, '\'')
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return "", driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
if len(buf)+4 > mc.maxAllowedPacket {
2024-02-18 10:42:21 +00:00
return "", driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if argPos != len(args) {
2024-02-18 10:42:21 +00:00
return "", driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(buf), nil
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
errLog.Print(ErrInvalidConn)
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(args) != 0 {
2024-02-18 10:42:21 +00:00
if !mc.cfg.InterpolateParams {
2024-02-18 10:42:21 +00:00
return nil, driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// try to interpolate the parameters to save extra roundtrips for preparing and closing a statement
2024-02-18 10:42:21 +00:00
prepared, err := mc.interpolateParams(query, args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
query = prepared
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mc.affectedRows = 0
2024-02-18 10:42:21 +00:00
mc.insertId = 0
err := mc.exec(query)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return &mysqlResult{
2024-02-18 10:42:21 +00:00
affectedRows: int64(mc.affectedRows),
insertId: int64(mc.insertId),
2024-02-18 10:42:21 +00:00
}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, mc.markBadConn(err)
2024-02-18 10:42:21 +00:00
}
// Internal function to execute commands
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) exec(query string) error {
2024-02-18 10:42:21 +00:00
// Send command
2024-02-18 10:42:21 +00:00
if err := mc.writeCommandPacketStr(comQuery, query); err != nil {
2024-02-18 10:42:21 +00:00
return mc.markBadConn(err)
2024-02-18 10:42:21 +00:00
}
// Read Result
2024-02-18 10:42:21 +00:00
resLen, err := mc.readResultSetHeaderPacket()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if resLen > 0 {
2024-02-18 10:42:21 +00:00
// columns
2024-02-18 10:42:21 +00:00
if err := mc.readUntilEOF(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// rows
2024-02-18 10:42:21 +00:00
if err := mc.readUntilEOF(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return mc.discardResults()
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
2024-02-18 10:42:21 +00:00
return mc.query(query, args)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
errLog.Print(ErrInvalidConn)
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(args) != 0 {
2024-02-18 10:42:21 +00:00
if !mc.cfg.InterpolateParams {
2024-02-18 10:42:21 +00:00
return nil, driver.ErrSkip
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// try client-side prepare to reduce roundtrip
2024-02-18 10:42:21 +00:00
prepared, err := mc.interpolateParams(query, args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
query = prepared
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Send command
2024-02-18 10:42:21 +00:00
err := mc.writeCommandPacketStr(comQuery, query)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
// Read Result
2024-02-18 10:42:21 +00:00
var resLen int
2024-02-18 10:42:21 +00:00
resLen, err = mc.readResultSetHeaderPacket()
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
rows := new(textRows)
2024-02-18 10:42:21 +00:00
rows.mc = mc
if resLen == 0 {
2024-02-18 10:42:21 +00:00
rows.rs.done = true
switch err := rows.NextResultSet(); err {
2024-02-18 10:42:21 +00:00
case nil, io.EOF:
2024-02-18 10:42:21 +00:00
return rows, nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Columns
2024-02-18 10:42:21 +00:00
rows.rs.columns, err = mc.readColumns(resLen)
2024-02-18 10:42:21 +00:00
return rows, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, mc.markBadConn(err)
2024-02-18 10:42:21 +00:00
}
// Gets the value of the given MySQL System Variable
2024-02-18 10:42:21 +00:00
// The returned byte slice is only valid until the next read
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
2024-02-18 10:42:21 +00:00
// Send command
2024-02-18 10:42:21 +00:00
if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// Read Result
2024-02-18 10:42:21 +00:00
resLen, err := mc.readResultSetHeaderPacket()
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
rows := new(textRows)
2024-02-18 10:42:21 +00:00
rows.mc = mc
2024-02-18 10:42:21 +00:00
rows.rs.columns = []mysqlField{{fieldType: fieldTypeVarChar}}
if resLen > 0 {
2024-02-18 10:42:21 +00:00
// Columns
2024-02-18 10:42:21 +00:00
if err := mc.readUntilEOF(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
dest := make([]driver.Value, resLen)
2024-02-18 10:42:21 +00:00
if err = rows.readRow(dest); err == nil {
2024-02-18 10:42:21 +00:00
return dest[0].([]byte), mc.readUntilEOF()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// finish is called when the query has canceled.
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) cancel(err error) {
2024-02-18 10:42:21 +00:00
mc.canceled.Set(err)
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
}
// finish is called when the query has succeeded.
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) finish() {
2024-02-18 10:42:21 +00:00
if !mc.watching || mc.finished == nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case mc.finished <- struct{}{}:
2024-02-18 10:42:21 +00:00
mc.watching = false
2024-02-18 10:42:21 +00:00
case <-mc.closech:
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Ping implements driver.Pinger interface
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
errLog.Print(ErrInvalidConn)
2024-02-18 10:42:21 +00:00
return driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
if err = mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer mc.finish()
if err = mc.writeCommandPacket(comPing); err != nil {
2024-02-18 10:42:21 +00:00
return mc.markBadConn(err)
2024-02-18 10:42:21 +00:00
}
return mc.readResultOK()
2024-02-18 10:42:21 +00:00
}
// BeginTx implements driver.ConnBeginTx interface
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
return nil, driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
if err := mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer mc.finish()
if sql.IsolationLevel(opts.Isolation) != sql.LevelDefault {
2024-02-18 10:42:21 +00:00
level, err := mapIsolationLevel(opts.Isolation)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = mc.exec("SET TRANSACTION ISOLATION LEVEL " + level)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return mc.begin(opts.ReadOnly)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
2024-02-18 10:42:21 +00:00
dargs, err := namedValueToValue(args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if err := mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
rows, err := mc.query(query, dargs)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
mc.finish()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rows.finish = mc.finish
2024-02-18 10:42:21 +00:00
return rows, err
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
2024-02-18 10:42:21 +00:00
dargs, err := namedValueToValue(args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if err := mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer mc.finish()
return mc.Exec(query, dargs)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
2024-02-18 10:42:21 +00:00
if err := mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
stmt, err := mc.Prepare(query)
2024-02-18 10:42:21 +00:00
mc.finish()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
select {
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
stmt.Close()
2024-02-18 10:42:21 +00:00
return nil, ctx.Err()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return stmt, nil
2024-02-18 10:42:21 +00:00
}
func (stmt *mysqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
2024-02-18 10:42:21 +00:00
dargs, err := namedValueToValue(args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if err := stmt.mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
rows, err := stmt.query(dargs)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
stmt.mc.finish()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rows.finish = stmt.mc.finish
2024-02-18 10:42:21 +00:00
return rows, err
2024-02-18 10:42:21 +00:00
}
func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
2024-02-18 10:42:21 +00:00
dargs, err := namedValueToValue(args)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if err := stmt.mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer stmt.mc.finish()
return stmt.Exec(dargs)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) watchCancel(ctx context.Context) error {
2024-02-18 10:42:21 +00:00
if mc.watching {
2024-02-18 10:42:21 +00:00
// Reach here if canceled,
2024-02-18 10:42:21 +00:00
// so the connection is already invalid
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// When ctx is already cancelled, don't watch it.
2024-02-18 10:42:21 +00:00
if err := ctx.Err(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// When ctx is not cancellable, don't watch it.
2024-02-18 10:42:21 +00:00
if ctx.Done() == nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// When watcher is not alive, can't watch it.
2024-02-18 10:42:21 +00:00
if mc.watcher == nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
mc.watching = true
2024-02-18 10:42:21 +00:00
mc.watcher <- ctx
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) startWatcher() {
2024-02-18 10:42:21 +00:00
watcher := make(chan context.Context, 1)
2024-02-18 10:42:21 +00:00
mc.watcher = watcher
2024-02-18 10:42:21 +00:00
finished := make(chan struct{})
2024-02-18 10:42:21 +00:00
mc.finished = finished
2024-02-18 10:42:21 +00:00
go func() {
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
var ctx context.Context
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case ctx = <-watcher:
2024-02-18 10:42:21 +00:00
case <-mc.closech:
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
select {
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
mc.cancel(ctx.Err())
2024-02-18 10:42:21 +00:00
case <-finished:
2024-02-18 10:42:21 +00:00
case <-mc.closech:
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}()
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
2024-02-18 10:42:21 +00:00
nv.Value, err = converter{}.ConvertValue(nv.Value)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// ResetSession implements driver.SessionResetter.
2024-02-18 10:42:21 +00:00
// (From Go 1.10)
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) ResetSession(ctx context.Context) error {
2024-02-18 10:42:21 +00:00
if mc.closed.IsSet() {
2024-02-18 10:42:21 +00:00
return driver.ErrBadConn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mc.reset = true
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// IsValid implements driver.Validator interface
2024-02-18 10:42:21 +00:00
// (From Go 1.15)
2024-02-18 10:42:21 +00:00
func (mc *mysqlConn) IsValid() bool {
2024-02-18 10:42:21 +00:00
return !mc.closed.IsSet()
2024-02-18 10:42:21 +00:00
}