mirror of https://github.com/jackc/pgx.git
Add RuntimeParams to ConnConfig
parent
355a3854db
commit
1fb63a4b41
7
conn.go
7
conn.go
|
@ -36,6 +36,7 @@ type ConnConfig struct {
|
|||
Logger Logger
|
||||
LogLevel int
|
||||
Dial DialFunc
|
||||
RuntimeParams map[string]string // Run-time parameters to set on connection as session default values (e.g. search_path or application_name)
|
||||
}
|
||||
|
||||
// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage.
|
||||
|
@ -219,6 +220,12 @@ func (c *Conn) connect(config ConnConfig, network, address string, tlsConfig *tl
|
|||
c.mr.reader = c.reader
|
||||
|
||||
msg := newStartupMessage()
|
||||
|
||||
// Copy default run-time params
|
||||
for k, v := range config.RuntimeParams {
|
||||
msg.options[k] = v
|
||||
}
|
||||
|
||||
msg.options["user"] = c.config.User
|
||||
if c.config.Database != "" {
|
||||
msg.options["database"] = c.config.Database
|
||||
|
|
33
conn_test.go
33
conn_test.go
|
@ -258,6 +258,39 @@ func TestConnectCustomDialer(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConnectWithRuntimeParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
connConfig := *defaultConnConfig
|
||||
connConfig.RuntimeParams = map[string]string{
|
||||
"application_name": "pgxtest",
|
||||
"search_path": "myschema",
|
||||
}
|
||||
|
||||
conn, err := pgx.Connect(connConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to establish connection: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
var s string
|
||||
err = conn.QueryRow("show application_name").Scan(&s)
|
||||
if err != nil {
|
||||
t.Fatalf("QueryRow Scan unexpectedly failed: %v", err)
|
||||
}
|
||||
if s != "pgxtest" {
|
||||
t.Errorf("Expected application_name to be %s, but it was %s", "pgxtest", s)
|
||||
}
|
||||
|
||||
err = conn.QueryRow("show search_path").Scan(&s)
|
||||
if err != nil {
|
||||
t.Fatalf("QueryRow Scan unexpectedly failed: %v", err)
|
||||
}
|
||||
if s != "myschema" {
|
||||
t.Errorf("Expected search_path to be %s, but it was %s", "myschema", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseURI(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Reference in New Issue