Skip to content

Commit

Permalink
add wrapper method to call mc.cfg.Logger (go-sql-driver#1564)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1a64773)
  • Loading branch information
methane authored Mar 16, 2024
1 parent 33fa6e5 commit 1e75613
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
return authEd25519(authData, mc.cfg.Passwd)

default:
mc.cfg.Logger.Print("unknown auth plugin:", plugin)
mc.log("unknown auth plugin:", plugin)
return nil, ErrUnknownPlugin
}
}
Expand Down
23 changes: 14 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type mysqlConn struct {
closed atomicBool // set when conn is closed, before closech is closed
}

// Helper function to call per-connection logger.
func (mc *mysqlConn) log(v ...any) {
mc.cfg.Logger.Print(v...)
}

// Handles parameters set in DSN after the connection is established
func (mc *mysqlConn) handleParams() (err error) {
var cmdSet strings.Builder
Expand Down Expand Up @@ -109,7 +114,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {

func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
if mc.closed.Load() {
mc.cfg.Logger.Print(ErrInvalidConn)
mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
var q string
Expand Down Expand Up @@ -151,7 +156,7 @@ func (mc *mysqlConn) cleanup() {
return
}
if err := mc.netConn.Close(); err != nil {
mc.cfg.Logger.Print(err)
mc.log(err)
}
mc.clearResult()
}
Expand All @@ -168,14 +173,14 @@ func (mc *mysqlConn) error() error {

func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
if mc.closed.Load() {
mc.cfg.Logger.Print(ErrInvalidConn)
mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
err := mc.writeCommandPacketStr(comStmtPrepare, query)
if err != nil {
// STMT_PREPARE is safe to retry. So we can return ErrBadConn here.
mc.cfg.Logger.Print(err)
mc.log(err)
return nil, driver.ErrBadConn
}

Expand Down Expand Up @@ -209,7 +214,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
buf, err := mc.buf.takeCompleteBuffer()
if err != nil {
// can not take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return "", ErrInvalidConn
}
buf = buf[:0]
Expand Down Expand Up @@ -301,7 +306,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin

func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if mc.closed.Load() {
mc.cfg.Logger.Print(ErrInvalidConn)
mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if len(args) != 0 {
Expand Down Expand Up @@ -361,7 +366,7 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
handleOk := mc.clearResult()

if mc.closed.Load() {
mc.cfg.Logger.Print(ErrInvalidConn)
mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if len(args) != 0 {
Expand Down Expand Up @@ -456,7 +461,7 @@ func (mc *mysqlConn) finish() {
// Ping implements driver.Pinger interface
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
if mc.closed.Load() {
mc.cfg.Logger.Print(ErrInvalidConn)
mc.log(ErrInvalidConn)
return driver.ErrBadConn
}

Expand Down Expand Up @@ -665,7 +670,7 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
err = connCheck(conn)
}
if err != nil {
mc.cfg.Logger.Print("closing bad idle connection: ", err)
mc.log("closing bad idle connection: ", err)
return driver.ErrBadConn
}
}
Expand Down
24 changes: 12 additions & 12 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
if cerr := mc.canceled.Value(); cerr != nil {
return nil, cerr
}
mc.cfg.Logger.Print(err)
mc.log(err)
mc.Close()
return nil, ErrInvalidConn
}
Expand All @@ -57,7 +57,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
if pktLen == 0 {
// there was no previous packet
if prevData == nil {
mc.cfg.Logger.Print(ErrMalformPkt)
mc.log(ErrMalformPkt)
mc.Close()
return nil, ErrInvalidConn
}
Expand All @@ -71,7 +71,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
if cerr := mc.canceled.Value(); cerr != nil {
return nil, cerr
}
mc.cfg.Logger.Print(err)
mc.log(err)
mc.Close()
return nil, ErrInvalidConn
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
// Handle error
if err == nil { // n != len(data)
mc.cleanup()
mc.cfg.Logger.Print(ErrMalformPkt)
mc.log(ErrMalformPkt)
} else {
if cerr := mc.canceled.Value(); cerr != nil {
return cerr
Expand All @@ -144,7 +144,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
return errBadConnNoWrite
}
mc.cleanup()
mc.cfg.Logger.Print(err)
mc.log(err)
}
return ErrInvalidConn
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
data, err := mc.buf.takeBuffer(pktLen + 4)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand Down Expand Up @@ -392,7 +392,7 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte) error {
data, err := mc.buf.takeSmallBuffer(pktLen)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand All @@ -412,7 +412,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
data, err := mc.buf.takeSmallBuffer(4 + 1)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand All @@ -431,7 +431,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
data, err := mc.buf.takeBuffer(pktLen + 4)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand All @@ -452,7 +452,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
data, err := mc.buf.takeSmallBuffer(4 + 1 + 4)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand Down Expand Up @@ -994,7 +994,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
}
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}

Expand Down Expand Up @@ -1193,7 +1193,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
if valuesCap != cap(paramValues) {
data = append(data[:pos], paramValues...)
if err = mc.buf.store(data); err != nil {
mc.cfg.Logger.Print(err)
mc.log(err)
return errBadConnNoWrite
}
}
Expand Down
4 changes: 2 additions & 2 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (stmt *mysqlStmt) CheckNamedValue(nv *driver.NamedValue) (err error) {

func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
if stmt.mc.closed.Load() {
stmt.mc.cfg.Logger.Print(ErrInvalidConn)
stmt.mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
Expand Down Expand Up @@ -95,7 +95,7 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {

func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
if stmt.mc.closed.Load() {
stmt.mc.cfg.Logger.Print(ErrInvalidConn)
stmt.mc.log(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
Expand Down

0 comments on commit 1e75613

Please sign in to comment.