Skip to content

Commit b2c046d

Browse files
committed
Rename connectionAttributes to connectAttrs and mention performance_schema in readme
1 parent 6a2c0a1 commit b2c046d

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ before_script:
1616
- sudo service mysql restart
1717
- .travis/wait_mysql.sh
1818
- mysql -e 'create database gotest;'
19+
- mysql -e 'show databases;'
1920

2021
matrix:
2122
include:

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ SELECT u.id FROM users as u
204204

205205
will return `u.id` instead of just `id` if `columnsWithAlias=true`.
206206

207-
##### `connectionAttributes`
207+
##### `connectAttrs`
208208

209209
```
210210
Type: map
211211
Valid Values: comma-separated list of attribute:value pairs
212212
Default: empty
213213
```
214214

215-
Allows setting of connection attributes, for example `connectionAttributes=program_name:YourProgramName` will show `YourProgramName` in `Program` field of connections list of Mysql Workbench.
215+
Allows setting of connection attributes, for example `connectAttrs=program_name:YourProgramName` will show `YourProgramName` in `Program` field of connections list of Mysql Workbench, if your server supports it (requires `performance_schema` to be supported and enabled).
216216

217217
##### `interpolateParams`
218218

@@ -497,4 +497,3 @@ Please read the [MPL 2.0 FAQ](https://www.mozilla.org/en-US/MPL/2.0/FAQ/) if you
497497
You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE).
498498

499499
![Go Gopher and MySQL Dolphin](https://raw.github.com/wiki/go-sql-driver/mysql/go-mysql-driver_m.jpg "Golang Gopher transporting the MySQL Dolphin in a wheelbarrow")
500-

driver_test.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -2074,21 +2074,29 @@ func TestEmptyPassword(t *testing.T) {
20742074
}
20752075
}
20762076

2077-
func TestConnectionAttributes(t *testing.T) {
2077+
func TestConnectAttrs(t *testing.T) {
20782078
if !available {
20792079
t.Skipf("MySQL server not running on %s", netAddr)
20802080
}
20812081

2082-
db, err := sql.Open("mysql", dsn+"&connectionAttributes=program_name:GoTest,foo:bar")
2082+
db, err := sql.Open("mysql", dsn+"&connectAttrs=program_name:GoTest,foo:bar")
20832083
if err != nil {
20842084
t.Fatalf("error connecting: %s", err.Error())
20852085
}
20862086
defer db.Close()
20872087
dbt := &DBTest{t, db}
20882088

2089-
rows, err := dbt.db.Query("SELECT program_name FROM sys.processlist WHERE db=?", dbname)
2089+
// performance_schema seems to be updated with a delay in some conditions, so first see if we are in list:
2090+
rows := dbt.mustQuery("SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST where ID=CONNECTION_ID()")
2091+
if rows.Next() {
2092+
} else {
2093+
dbt.Error("no data in processlist")
2094+
}
2095+
2096+
rows, err = dbt.db.Query("select attr_value from performance_schema.session_account_connect_attrs where processlist_id=CONNECTION_ID() and attr_name='program_name'")
20902097
if err != nil {
2091-
dbt.Skip("server probably does not support program_name in sys.processlist")
2098+
fmt.Println(err)
2099+
dbt.Skip("server probably does not support performance_schema.session_account_connect_attrs")
20922100
}
20932101

20942102
if rows.Next() {
@@ -2098,7 +2106,7 @@ func TestConnectionAttributes(t *testing.T) {
20982106
dbt.Errorf("GoTest != %s", str)
20992107
}
21002108
} else {
2101-
dbt.Error("no data")
2109+
dbt.Error("no data for program_name")
21022110
}
21032111

21042112
rows = dbt.mustQuery("select attr_value from performance_schema.session_account_connect_attrs where processlist_id=CONNECTION_ID() and attr_name='foo'")
@@ -2109,6 +2117,6 @@ func TestConnectionAttributes(t *testing.T) {
21092117
dbt.Errorf("bar != %s", str)
21102118
}
21112119
} else {
2112-
dbt.Error("no data")
2120+
dbt.Error("no data for custom attribute")
21132121
}
21142122
}

dsn.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Config struct {
3939
Addr string // Network address (requires Net)
4040
DBName string // Database name
4141
Params map[string]string // Connection parameters
42-
Attributes map[string]string // Connection attributes
42+
ConnectAttrs map[string]string // Connection attributes
4343
Collation string // Connection collation
4444
Loc *time.Location // Location for time.Time values
4545
MaxAllowedPacket int // Max packet size allowed
@@ -309,17 +309,17 @@ func (cfg *Config) FormatDSN() string {
309309

310310
}
311311

312-
if len(cfg.Attributes) > 0 {
313-
// connectionAttributes=program_name:Login Server,other_name:other
312+
if len(cfg.ConnectAttrs) > 0 {
313+
// connectAttrs=program_name:Login Server,other_name:other
314314
if hasParam {
315-
buf.WriteString("&connectionAttributes=")
315+
buf.WriteString("&connectAttrs=")
316316
} else {
317317
hasParam = true
318-
buf.WriteString("?connectionAttributes=")
318+
buf.WriteString("?connectAttrs=")
319319
}
320320

321321
var attr_names []string
322-
for attr_name := range cfg.Attributes {
322+
for attr_name := range cfg.ConnectAttrs {
323323
attr_names = append(attr_names, attr_name)
324324
}
325325
sort.Strings(attr_names)
@@ -329,7 +329,7 @@ func (cfg *Config) FormatDSN() string {
329329
}
330330
buf.WriteString(attr_name)
331331
buf.WriteByte(':')
332-
buf.WriteString(url.QueryEscape(cfg.Attributes[attr_name]))
332+
buf.WriteString(url.QueryEscape(cfg.ConnectAttrs[attr_name]))
333333
}
334334
}
335335

@@ -613,23 +613,23 @@ func parseDSNParams(cfg *Config, params string) (err error) {
613613
if err != nil {
614614
return
615615
}
616-
case "connectionAttributes":
617-
if cfg.Attributes == nil {
618-
cfg.Attributes = make(map[string]string)
616+
case "connectAttrs":
617+
if cfg.ConnectAttrs == nil {
618+
cfg.ConnectAttrs = make(map[string]string)
619619
}
620620

621-
var attributes string
622-
if attributes, err = url.QueryUnescape(value); err != nil {
621+
var ConnectAttrs string
622+
if ConnectAttrs, err = url.QueryUnescape(value); err != nil {
623623
return
624624
}
625625

626626
// program_name:Name,foo:bar
627-
for _, attr_str := range strings.Split(attributes, ",") {
627+
for _, attr_str := range strings.Split(ConnectAttrs, ",") {
628628
attr := strings.SplitN(attr_str, ":", 2)
629629
if len(attr) != 2 {
630630
continue
631631
}
632-
cfg.Attributes[attr[0]] = attr[1]
632+
cfg.ConnectAttrs[attr[0]] = attr[1]
633633
}
634634
default:
635635
// lazy init

dsn_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ var testDSNs = []struct {
7272
"tcp(de:ad:be:ef::ca:fe)/dbname",
7373
&Config{Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
7474
}, {
75-
"tcp(127.0.0.1)/dbname?connectionAttributes=program_name:SomeService",
76-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Attributes: map[string]string{"program_name": "SomeService"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
75+
"tcp(127.0.0.1)/dbname?connectAttrs=program_name:SomeService",
76+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", ConnectAttrs: map[string]string{"program_name": "SomeService"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
7777
},
7878
}
7979

@@ -322,16 +322,16 @@ func TestParamsAreSorted(t *testing.T) {
322322
}
323323

324324
func TestAttributesAreSorted(t *testing.T) {
325-
expected := "/dbname?connectionAttributes=p1:v1,p2:v2"
325+
expected := "/dbname?connectAttrs=p1:v1,p2:v2"
326326
cfg := NewConfig()
327327
cfg.DBName = "dbname"
328-
cfg.Attributes = map[string]string{
328+
cfg.ConnectAttrs = map[string]string{
329329
"p2": "v2",
330330
"p1": "v1",
331331
}
332332
actual := cfg.FormatDSN()
333333
if actual != expected {
334-
t.Errorf("generic Config.Attributes were not sorted: want %#v, got %#v", expected, actual)
334+
t.Errorf("generic Config.ConnectAttrs were not sorted: want %#v, got %#v", expected, actual)
335335
}
336336
}
337337

packets.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
295295
connectAttrsBuf = appendLengthEncodedString(connectAttrsBuf, []byte("_client_name"))
296296
connectAttrsBuf = appendLengthEncodedString(connectAttrsBuf, []byte("go-mysql-driver"))
297297

298-
for k, v := range mc.cfg.Attributes {
298+
for k, v := range mc.cfg.ConnectAttrs {
299299
if k == "_client_name" {
300300
// do not allow overwriting reserved values
301301
continue

0 commit comments

Comments
 (0)