Skip to content

Add support for connection attributes. #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Aaron Hopkins <go-sql-driver at die.net>
Arne Hormann <arnehormann at gmail.com>
Carlos Nieto <jose.carlos at menteslibres.net>
Chris Moos <chris at tech9computers.com>
Daniël van Eeden <daniel.van.eeden at myname.nl>
DisposaBoy <disposaboy at dby.me>
Frederick Mayle <frederickmayle at gmail.com>
Gustavo Kristic <gkristic at gmail.com>
Expand Down
5 changes: 5 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"database/sql"
"database/sql/driver"
"net"
"os"
"strconv"
)

// This struct is exported to make the driver directly accessible.
Expand All @@ -30,6 +32,8 @@ type MySQLDriver struct{}
// Custom dial functions must be registered with RegisterDial
type DialFunc func(addr string) (net.Conn, error)

var pid string

var dials map[string]DialFunc

// RegisterDial registers a custom dial function. It can then be used by the
Expand Down Expand Up @@ -145,5 +149,6 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
}

func init() {
pid = strconv.Itoa(os.Getpid())
sql.Register("mysql", &MySQLDriver{})
}
33 changes: 33 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"fmt"
"io"
"math"
"os"
"path"
"runtime"
"time"
)

Expand Down Expand Up @@ -219,6 +222,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
clientTransactions |
clientLocalFiles |
clientPluginAuth |
clientConnectAttrs |
mc.flags&clientLongFlag

if mc.cfg.clientFoundRows {
Expand All @@ -233,7 +237,23 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
// User Password
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.passwd))

attrs := map[string]string {
"_os": runtime.GOOS,
"_client_name": "Go-MySQL-Driver",
"_pid": pid,
"_platform": runtime.GOARCH,
"program_name": path.Base(os.Args[0]),
}

attrlen := 0
for attrname, attrvalue := range attrs {
attrlen += len(attrname) + len(attrvalue)
// one byte to store attrname length and one byte to store attrvalue length
attrlen += 2
}

pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff) + 21 + 1
pktLen += attrlen + 1 // one byte to store the total length of attrs

// To specify a db name
if n := len(mc.cfg.dbname); n > 0 {
Expand Down Expand Up @@ -305,6 +325,19 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
// Assume native client during response
pos += copy(data[pos:], "mysql_native_password")
data[pos] = 0x00
pos++

// Connection attributes
data[pos] = byte(attrlen)
pos++

for attrname, attrvalue := range attrs {
data[pos] = byte(len(attrname))
pos += 1 + copy(data[pos+1:], attrname)

data[pos] = byte(len(attrvalue))
pos += 1 + copy(data[pos+1:], attrvalue)
}

// Send Auth packet
return mc.writePacket(data)
Expand Down