mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Add ParseEnvLibpq with support for basic envvars
This commit is contained in:
parent
dd9d960ba3
commit
51d6d1a3a6
31
conn.go
31
conn.go
@ -331,6 +331,37 @@ func ParseDSN(s string) (ConnConfig, error) {
|
|||||||
return cp, nil
|
return cp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseEnvLibpq parses the environment like libpq does into a ConnConfig
|
||||||
|
//
|
||||||
|
// See http://www.postgresql.org/docs/9.4/static/libpq-envars.html for details
|
||||||
|
// on the meaning of environment variables.
|
||||||
|
//
|
||||||
|
// ParseEnvLibpq currently recognizes the following environment variables:
|
||||||
|
// PGHOST
|
||||||
|
// PGPORT
|
||||||
|
// PGDATABASE
|
||||||
|
// PGUSER
|
||||||
|
// PGPASSWORD
|
||||||
|
func ParseEnvLibpq() (ConnConfig, error) {
|
||||||
|
var cc ConnConfig
|
||||||
|
|
||||||
|
cc.Host = os.Getenv("PGHOST")
|
||||||
|
|
||||||
|
if pgport := os.Getenv("PGPORT"); pgport != "" {
|
||||||
|
if port, err := strconv.ParseUint(pgport, 10, 16); err == nil {
|
||||||
|
cc.Port = uint16(port)
|
||||||
|
} else {
|
||||||
|
return cc, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cc.Database = os.Getenv("PGDATABASE")
|
||||||
|
cc.User = os.Getenv("PGUSER")
|
||||||
|
cc.Password = os.Getenv("PGPASSWORD")
|
||||||
|
|
||||||
|
return cc, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare creates a prepared statement with name and sql. sql can contain placeholders
|
// Prepare creates a prepared statement with name and sql. sql can contain placeholders
|
||||||
// for bound parameters. These placeholders are referenced positional as $1, $2, etc.
|
// for bound parameters. These placeholders are referenced positional as $1, $2, etc.
|
||||||
func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) {
|
func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) {
|
||||||
|
70
conn_test.go
70
conn_test.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jackc/pgx"
|
"github.com/jackc/pgx"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -364,6 +365,75 @@ func TestParseDSN(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseEnvLibpq(t *testing.T) {
|
||||||
|
pgEnvvars := []string{"PGHOST", "PGPORT", "PGDATABASE", "PGUSER", "PGPASSWORD"}
|
||||||
|
|
||||||
|
savedEnv := make(map[string]string)
|
||||||
|
for _, n := range pgEnvvars {
|
||||||
|
savedEnv[n] = os.Getenv(n)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
for k, v := range savedEnv {
|
||||||
|
err := os.Setenv(k, v)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to restore environment:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
envvars map[string]string
|
||||||
|
config pgx.ConnConfig
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
envvars: map[string]string{},
|
||||||
|
config: pgx.ConnConfig{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
envvars: map[string]string{
|
||||||
|
"PGHOST": "123.123.123.123",
|
||||||
|
"PGPORT": "7777",
|
||||||
|
"PGDATABASE": "foo",
|
||||||
|
"PGUSER": "bar",
|
||||||
|
"PGPASSWORD": "baz",
|
||||||
|
},
|
||||||
|
config: pgx.ConnConfig{
|
||||||
|
Host: "123.123.123.123",
|
||||||
|
Port: 7777,
|
||||||
|
Database: "foo",
|
||||||
|
User: "bar",
|
||||||
|
Password: "baz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
for _, n := range pgEnvvars {
|
||||||
|
err := os.Unsetenv(n)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%d. Unable to clear environment:", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range tt.envvars {
|
||||||
|
err := os.Setenv(k, v)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%d. Unable to set environment:", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := pgx.ParseEnvLibpq()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d. Unexpected error from pgx.ParseLibpq() => %v", i, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(config, tt.config) {
|
||||||
|
t.Errorf("%d. expected %#v got %#v", i, tt.config, config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestExec(t *testing.T) {
|
func TestExec(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user