From 829d61ce407900030e3ee0ab3d332e45f48c25dc Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 16 Oct 2015 15:48:24 -0500 Subject: [PATCH] ParseDSN extracts RuntimeParams --- conn.go | 6 +++++- conn_test.go | 32 +++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/conn.go b/conn.go index bc2d7578..d72be8e0 100644 --- a/conn.go +++ b/conn.go @@ -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] } } diff --git a/conn_test.go b/conn_test.go index 22cace48..0d991164 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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", + }, }, }, }