Skip to content

Commit

Permalink
QueryUnescape DSN ConnectionAttribute value (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyangyu committed Nov 14, 2023
1 parent 18b74e4 commit b2e2ccb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Expand Up @@ -109,6 +109,7 @@ Xiangyu Hu <xiangyu.hu at outlook.com>
Xiaobing Jiang <s7v7nislands at gmail.com>
Xiuming Chen <cc at cxm.cc>
Xuehong Chan <chanxuehong at gmail.com>
Zhang Xiang <angwerzx at 126.com>
Zhenye Xie <xiezhenye at gmail.com>
Zhixin Wen <john.wenzhixin at gmail.com>
Ziheng Lyu <zihenglv at gmail.com>
Expand All @@ -127,6 +128,7 @@ InfoSum Ltd.
Keybase Inc.
Multiplay Ltd.
Percona LLC
PingCAP Inc.
Pivotal Inc.
Stripe Inc.
Zendesk Inc.
Expand Down
18 changes: 15 additions & 3 deletions driver_test.go
Expand Up @@ -3379,9 +3379,10 @@ func TestConnectionAttributes(t *testing.T) {

attr1 := "attr1"
value1 := "value1"
attr2 := "foo"
value2 := "boo"
dsn += fmt.Sprintf("&connectionAttributes=%s:%s,%s:%s", attr1, value1, attr2, value2)
attr2 := "fo/o"
value2 := "bo/o"
dsn += "&connectionAttributes=" + url.QueryEscape(fmt.Sprintf("%s:%s,%s:%s", attr1, value1, attr2, value2))


var db *sql.DB
if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation {
Expand All @@ -3407,6 +3408,17 @@ func TestConnectionAttributes(t *testing.T) {
}
rows.Close()

rows = dbt.mustQuery(queryString, attr1)
if rows.Next() {
rows.Scan(&attrValue)
if attrValue != value1 {
dbt.Errorf("expected %q, got %q", value1, attrValue)
}
} else {
dbt.Errorf("no data")
}
rows.Close()

rows = dbt.mustQuery(queryString, attr2)
if rows.Next() {
rows.Scan(&attrValue)
Expand Down
6 changes: 5 additions & 1 deletion dsn.go
Expand Up @@ -569,7 +569,11 @@ func parseDSNParams(cfg *Config, params string) (err error) {

// Connection attributes
case "connectionAttributes":
cfg.ConnectionAttributes = value
connectionAttributes, err := url.QueryUnescape(value)
if err != nil {
return fmt.Errorf("invalid connectionAttributes value: %v", err)
}
cfg.ConnectionAttributes = connectionAttributes

default:
// lazy init
Expand Down

0 comments on commit b2e2ccb

Please sign in to comment.