Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add includeLine option
  • Loading branch information
dveeden committed Feb 3, 2025
commit f56205095888f705890d3046c6bd620d6177a470
21 changes: 21 additions & 0 deletions client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"runtime"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -58,6 +59,8 @@ type Conn struct {
connectionID uint32

queryAttributes []QueryAttribute

includeLine bool
}

// This function will be called for every row in resultset from ExecuteSelectStreaming.
Expand Down Expand Up @@ -488,6 +491,18 @@ func (c *Conn) ReadOKPacket() (*Result, error) {
func (c *Conn) exec(query string) (*Result, error) {
var buf bytes.Buffer

if c.includeLine {
_, file, line, ok := runtime.Caller(2)
if ok {
lineAttr := QueryAttribute{
Name: "_line",
Value: fmt.Sprintf("%s:%d", file, line),
}
c.queryAttributes = append(c.queryAttributes, lineAttr)
}

}

if c.capability&CLIENT_QUERY_ATTRIBUTES > 0 {
numParams := len(c.queryAttributes)
buf.Write(PutLengthEncodedInt(uint64(numParams)))
Expand Down Expand Up @@ -656,3 +671,9 @@ func (c *Conn) SetQueryAttributes(attrs ...QueryAttribute) error {
c.queryAttributes = attrs
return nil
}

// IncludeLine can be passed as option when connecting to include the file name and line number
// of the caller as query attribute when sending queries.
func (c *Conn) IncludeLine() {
c.includeLine = true
}
13 changes: 13 additions & 0 deletions client/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math"
"runtime"

. "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/utils"
Expand Down Expand Up @@ -64,6 +65,18 @@ func (s *Stmt) write(args ...interface{}) error {
return fmt.Errorf("argument mismatch, need %d but got %d", s.params, len(args))
}

if s.conn.includeLine {
_, file, line, ok := runtime.Caller(2)
if ok {
lineAttr := QueryAttribute{
Name: "_line",
Value: fmt.Sprintf("%s:%d", file, line),
}
s.conn.queryAttributes = append(s.conn.queryAttributes, lineAttr)
}

}

qaLen := len(s.conn.queryAttributes)
paramTypes := make([][]byte, paramsNum+qaLen)
paramFlags := make([][]byte, paramsNum+qaLen)
Expand Down