Skip to content

Commit

Permalink
Add support for connection attributes.
Browse files Browse the repository at this point in the history
This sets attribute _client_name with the value "Go MySQL Driver"
Also sets _os, _platform, _pid and program_name by default.
  • Loading branch information
dveeden committed May 28, 2015
1 parent 0cc29e9 commit 4fd4bff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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
33 changes: 33 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"fmt"
"io"
"math"
"os"
"path"
"runtime"
"strconv"
"time"
)

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

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

attrs := make(map[string]string)
attrs["_os"] = runtime.GOOS
attrs["_client_name"] = "Go MySQL Driver"
attrs["_pid"] = strconv.Itoa(os.Getpid())
attrs["_platform"] = runtime.GOARCH
attrs["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)
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 @@ -295,6 +315,19 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
pos += copy(data[pos:], mc.cfg.dbname)
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

0 comments on commit 4fd4bff

Please sign in to comment.