ParseDSN extracts RuntimeParams

pull/111/head
Jack Christensen 2015-10-16 15:48:24 -05:00
parent 5b0550c1cb
commit 829d61ce40
2 changed files with 32 additions and 6 deletions

View File

@ -381,7 +381,7 @@ func ParseURI(uri string) (ConnConfig, error) {
return cp, nil
}
var dsn_regexp = regexp.MustCompile(`([a-z]+)=((?:"[^"]+")|(?:[^ ]+))`)
var dsn_regexp = regexp.MustCompile(`([a-zA-Z_]+)=((?:"[^"]+")|(?:[^ ]+))`)
// ParseDSN parses a database DSN (data source name) into a ConnConfig
//
@ -397,6 +397,8 @@ func ParseDSN(s string) (ConnConfig, error) {
var sslmode string
cp.RuntimeParams = make(map[string]string)
for _, b := range m {
switch b[1] {
case "user":
@ -415,6 +417,8 @@ func ParseDSN(s string) (ConnConfig, error) {
cp.Database = b[2]
case "sslmode":
sslmode = b[2]
default:
cp.RuntimeParams[b[1]] = b[2]
}
}

View File

@ -431,11 +431,12 @@ func TestParseDSN(t *testing.T) {
{
url: "user=jack password=secret host=localhost port=5432 dbname=mydb sslmode=disable",
connParams: pgx.ConnConfig{
User: "jack",
Password: "secret",
Host: "localhost",
Port: 5432,
Database: "mydb",
User: "jack",
Password: "secret",
Host: "localhost",
Port: 5432,
Database: "mydb",
RuntimeParams: map[string]string{},
},
},
{
@ -451,6 +452,7 @@ func TestParseDSN(t *testing.T) {
},
UseFallbackTLS: true,
FallbackTLSConfig: nil,
RuntimeParams: map[string]string{},
},
},
{
@ -466,6 +468,7 @@ func TestParseDSN(t *testing.T) {
},
UseFallbackTLS: true,
FallbackTLSConfig: nil,
RuntimeParams: map[string]string{},
},
},
{
@ -480,6 +483,7 @@ func TestParseDSN(t *testing.T) {
},
UseFallbackTLS: true,
FallbackTLSConfig: nil,
RuntimeParams: map[string]string{},
},
},
{
@ -493,6 +497,24 @@ func TestParseDSN(t *testing.T) {
},
UseFallbackTLS: true,
FallbackTLSConfig: nil,
RuntimeParams: map[string]string{},
},
},
{
url: "user=jack host=localhost dbname=mydb application_name=pgxtest search_path=myschema",
connParams: pgx.ConnConfig{
User: "jack",
Host: "localhost",
Database: "mydb",
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
UseFallbackTLS: true,
FallbackTLSConfig: nil,
RuntimeParams: map[string]string{
"application_name": "pgxtest",
"search_path": "myschema",
},
},
},
}