Rename ConnectionParameters to ConnConfig

scan-io
Jack Christensen 2014-05-17 13:30:22 -05:00
parent 4eb597d20b
commit 1ff653ae15
12 changed files with 115 additions and 115 deletions

2
.gitignore vendored
View File

@ -21,4 +21,4 @@ _testmain.go
*.exe *.exe
connection_settings_test.go conn_config_test.go

View File

@ -99,7 +99,7 @@ if notification, err := conn.WaitForNotification(time.Second); err != nil {
### TLS ### TLS
The pgx ConnectionParameters struct has a TLSConfig field. If this field is The pgx ConnConfig struct has a TLSConfig field. If this field is
nil, then TLS will be disabled. If it is present, then it will be used to nil, then TLS will be disabled. If it is present, then it will be used to
configure the TLS connection. configure the TLS connection.

View File

@ -636,7 +636,7 @@ func BenchmarkTimestampTzBinary(b *testing.B) {
func BenchmarkConnectionPool(b *testing.B) { func BenchmarkConnectionPool(b *testing.B) {
options := pgx.ConnectionPoolOptions{MaxConnections: 5} options := pgx.ConnectionPoolOptions{MaxConnections: 5}
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, options) pool, err := pgx.NewConnectionPool(*defaultConnConfig, options)
if err != nil { if err != nil {
b.Fatalf("Unable to create connection pool: %v", err) b.Fatalf("Unable to create connection pool: %v", err)
} }

82
conn.go
View File

@ -23,8 +23,8 @@ import (
"time" "time"
) )
// ConnectionParameters contains all the options used to establish a connection. // ConnConfig contains all the options used to establish a connection.
type ConnectionParameters struct { type ConnConfig struct {
Socket string // path to unix domain socket directory (e.g. /private/tmp) Socket string // path to unix domain socket directory (e.g. /private/tmp)
Host string // url (e.g. localhost) Host string // url (e.g. localhost)
Port uint16 // default: 5432 Port uint16 // default: 5432
@ -40,15 +40,15 @@ type ConnectionParameters struct {
// Use ConnectionPool to manage access to multiple database connections from multiple // Use ConnectionPool to manage access to multiple database connections from multiple
// goroutines. // goroutines.
type Conn struct { type Conn struct {
conn net.Conn // the underlying TCP or unix domain socket connection conn net.Conn // the underlying TCP or unix domain socket connection
reader *bufio.Reader // buffered reader to improve read performance reader *bufio.Reader // buffered reader to improve read performance
writer *bufio.Writer // buffered writer to avoid sending tiny packets writer *bufio.Writer // buffered writer to avoid sending tiny packets
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
bufSize int // desired size of buf bufSize int // desired size of buf
Pid int32 // backend pid Pid int32 // backend pid
SecretKey int32 // key to use to send a cancel query message to the server SecretKey int32 // key to use to send a cancel query message to the server
RuntimeParams map[string]string // parameters that have been reported by the server RuntimeParams map[string]string // parameters that have been reported by the server
parameters ConnectionParameters // parameters used when establishing this connection config ConnConfig // config used when establishing this connection
TxStatus byte TxStatus byte
preparedStatements map[string]*preparedStatement preparedStatements map[string]*preparedStatement
notifications []*Notification notifications []*Notification
@ -98,42 +98,42 @@ func (e ProtocolError) Error() string {
var NotificationTimeoutError = errors.New("Notification Timeout") var NotificationTimeoutError = errors.New("Notification Timeout")
// Connect establishes a connection with a PostgreSQL server using parameters. One // Connect establishes a connection with a PostgreSQL server using config. One
// of parameters.Socket or parameters.Host must be specified. parameters.User // of config.Socket or config.Host must be specified. config.User
// will default to the OS user name. Other parameters fields are optional. // will default to the OS user name. Other config fields are optional.
func Connect(parameters ConnectionParameters) (c *Conn, err error) { func Connect(config ConnConfig) (c *Conn, err error) {
c = new(Conn) c = new(Conn)
c.parameters = parameters c.config = config
if c.parameters.Logger != nil { if c.config.Logger != nil {
c.logger = c.parameters.Logger c.logger = c.config.Logger
} else { } else {
c.logger = nullLogger("null") c.logger = nullLogger("null")
} }
if c.parameters.User == "" { if c.config.User == "" {
user, err := user.Current() user, err := user.Current()
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.logger.Debug("Using default User " + user.Username) c.logger.Debug("Using default User " + user.Username)
c.parameters.User = user.Username c.config.User = user.Username
} }
if c.parameters.Port == 0 { if c.config.Port == 0 {
c.logger.Debug("Using default Port") c.logger.Debug("Using default Port")
c.parameters.Port = 5432 c.config.Port = 5432
} }
if c.parameters.MsgBufSize == 0 { if c.config.MsgBufSize == 0 {
c.logger.Debug("Using default MsgBufSize") c.logger.Debug("Using default MsgBufSize")
c.parameters.MsgBufSize = 1024 c.config.MsgBufSize = 1024
} }
if c.parameters.Socket != "" { if c.config.Socket != "" {
// For backward compatibility accept socket file paths -- but directories are now preferred // For backward compatibility accept socket file paths -- but directories are now preferred
socket := c.parameters.Socket socket := c.config.Socket
if !strings.Contains(socket, "/.s.PGSQL.") { if !strings.Contains(socket, "/.s.PGSQL.") {
socket = filepath.Join(socket, ".s.PGSQL.") + strconv.FormatInt(int64(c.parameters.Port), 10) socket = filepath.Join(socket, ".s.PGSQL.") + strconv.FormatInt(int64(c.config.Port), 10)
} }
c.logger.Info(fmt.Sprintf("Dialing PostgreSQL server at socket: %s", socket)) c.logger.Info(fmt.Sprintf("Dialing PostgreSQL server at socket: %s", socket))
@ -142,9 +142,9 @@ func Connect(parameters ConnectionParameters) (c *Conn, err error) {
c.logger.Error(fmt.Sprintf("Connection failed: %v", err)) c.logger.Error(fmt.Sprintf("Connection failed: %v", err))
return nil, err return nil, err
} }
} else if c.parameters.Host != "" { } else if c.config.Host != "" {
c.logger.Info(fmt.Sprintf("Dialing PostgreSQL server at host: %s:%d", c.parameters.Host, c.parameters.Port)) c.logger.Info(fmt.Sprintf("Dialing PostgreSQL server at host: %s:%d", c.config.Host, c.config.Port))
c.conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", c.parameters.Host, c.parameters.Port)) c.conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", c.config.Host, c.config.Port))
if err != nil { if err != nil {
c.logger.Error(fmt.Sprintf("Connection failed: %v", err)) c.logger.Error(fmt.Sprintf("Connection failed: %v", err))
return nil, err return nil, err
@ -158,13 +158,13 @@ func Connect(parameters ConnectionParameters) (c *Conn, err error) {
} }
}() }()
c.bufSize = c.parameters.MsgBufSize c.bufSize = c.config.MsgBufSize
c.buf = bytes.NewBuffer(make([]byte, 0, c.bufSize)) c.buf = bytes.NewBuffer(make([]byte, 0, c.bufSize))
c.RuntimeParams = make(map[string]string) c.RuntimeParams = make(map[string]string)
c.preparedStatements = make(map[string]*preparedStatement) c.preparedStatements = make(map[string]*preparedStatement)
c.alive = true c.alive = true
if parameters.TLSConfig != nil { if config.TLSConfig != nil {
c.logger.Debug("Starting TLS handshake") c.logger.Debug("Starting TLS handshake")
if err = c.startTLS(); err != nil { if err = c.startTLS(); err != nil {
c.logger.Error(fmt.Sprintf("TLS failed: %v", err)) c.logger.Error(fmt.Sprintf("TLS failed: %v", err))
@ -176,9 +176,9 @@ func Connect(parameters ConnectionParameters) (c *Conn, err error) {
c.writer = bufio.NewWriter(c.conn) c.writer = bufio.NewWriter(c.conn)
msg := newStartupMessage() msg := newStartupMessage()
msg.options["user"] = c.parameters.User msg.options["user"] = c.config.User
if c.parameters.Database != "" { if c.config.Database != "" {
msg.options["database"] = c.parameters.Database msg.options["database"] = c.config.Database
} }
if err = c.txStartupMessage(msg); err != nil { if err = c.txStartupMessage(msg); err != nil {
return return
@ -218,9 +218,9 @@ func (c *Conn) Close() (err error) {
return err return err
} }
// ParseURI parses a database URI into ConnectionParameters // ParseURI parses a database URI into ConnConfig
func ParseURI(uri string) (ConnectionParameters, error) { func ParseURI(uri string) (ConnConfig, error) {
var cp ConnectionParameters var cp ConnConfig
url, err := url.Parse(uri) url, err := url.Parse(uri)
if err != nil { if err != nil {
@ -910,10 +910,10 @@ func (c *Conn) rxAuthenticationX(r *MessageReader) (err error) {
switch code { switch code {
case 0: // AuthenticationOk case 0: // AuthenticationOk
case 3: // AuthenticationCleartextPassword case 3: // AuthenticationCleartextPassword
err = c.txPasswordMessage(c.parameters.Password) err = c.txPasswordMessage(c.config.Password)
case 5: // AuthenticationMD5Password case 5: // AuthenticationMD5Password
salt := r.ReadString(4) salt := r.ReadString(4)
digestedPassword := "md5" + hexMD5(hexMD5(c.parameters.Password+c.parameters.User)+salt) digestedPassword := "md5" + hexMD5(hexMD5(c.config.Password+c.config.User)+salt)
err = c.txPasswordMessage(digestedPassword) err = c.txPasswordMessage(digestedPassword)
default: default:
err = errors.New("Received unknown authentication message") err = errors.New("Received unknown authentication message")
@ -1024,7 +1024,7 @@ func (c *Conn) startTLS() (err error) {
return return
} }
c.conn = tls.Client(c.conn, c.parameters.TLSConfig) c.conn = tls.Client(c.conn, c.config.TLSConfig)
return nil return nil
} }

View File

@ -0,0 +1,22 @@
package pgx_test
import (
"github.com/JackC/pgx"
)
var defaultConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// To skip tests for specific connection / authentication types set that connection param to nil
var tcpConnConfig *pgx.ConnConfig = nil
var unixSocketConnConfig *pgx.ConnConfig = nil
var md5ConnConfig *pgx.ConnConfig = nil
var plainPasswordConnConfig *pgx.ConnConfig = nil
var noPasswordConnConfig *pgx.ConnConfig = nil
var invalidUserConnConfig *pgx.ConnConfig = nil
// var tcpConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var unixSocketConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Socket: "/private/tmp", User: "pgx_none", Database: "pgx_test"}
// var md5ConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var plainPasswordConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_pw", Password: "secret", Database: "pgx_test"}
// var noPasswordConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_none", Database: "pgx_test"}
// var invalidUserConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "invalid", Database: "pgx_test"}

View File

@ -11,7 +11,7 @@ import (
) )
func TestConnect(t *testing.T) { func TestConnect(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -30,12 +30,12 @@ func TestConnect(t *testing.T) {
var rows []map[string]interface{} var rows []map[string]interface{}
rows, err = conn.SelectRows("select current_database()") rows, err = conn.SelectRows("select current_database()")
if err != nil || rows[0]["current_database"] != defaultConnectionParameters.Database { if err != nil || rows[0]["current_database"] != defaultConnConfig.Database {
t.Errorf("Did not connect to specified database (%v)", defaultConnectionParameters.Database) t.Errorf("Did not connect to specified database (%v)", defaultConnConfig.Database)
} }
if user := mustSelectValue(t, conn, "select current_user"); user != defaultConnectionParameters.User { if user := mustSelectValue(t, conn, "select current_user"); user != defaultConnConfig.User {
t.Errorf("Did not connect as specified user (%v)", defaultConnectionParameters.User) t.Errorf("Did not connect as specified user (%v)", defaultConnConfig.User)
} }
err = conn.Close() err = conn.Close()
@ -46,11 +46,11 @@ func TestConnect(t *testing.T) {
func TestConnectWithUnixSocketDirectory(t *testing.T) { func TestConnectWithUnixSocketDirectory(t *testing.T) {
// /.s.PGSQL.5432 // /.s.PGSQL.5432
if unixSocketConnectionParameters == nil { if unixSocketConnConfig == nil {
return return
} }
conn, err := pgx.Connect(*unixSocketConnectionParameters) conn, err := pgx.Connect(*unixSocketConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -62,11 +62,11 @@ func TestConnectWithUnixSocketDirectory(t *testing.T) {
} }
func TestConnectWithUnixSocketFile(t *testing.T) { func TestConnectWithUnixSocketFile(t *testing.T) {
if unixSocketConnectionParameters == nil { if unixSocketConnConfig == nil {
return return
} }
connParams := *unixSocketConnectionParameters connParams := *unixSocketConnConfig
connParams.Socket = connParams.Socket + "/.s.PGSQL.5432" connParams.Socket = connParams.Socket + "/.s.PGSQL.5432"
conn, err := pgx.Connect(connParams) conn, err := pgx.Connect(connParams)
if err != nil { if err != nil {
@ -80,11 +80,11 @@ func TestConnectWithUnixSocketFile(t *testing.T) {
} }
func TestConnectWithTcp(t *testing.T) { func TestConnectWithTcp(t *testing.T) {
if tcpConnectionParameters == nil { if tcpConnConfig == nil {
return return
} }
conn, err := pgx.Connect(*tcpConnectionParameters) conn, err := pgx.Connect(*tcpConnConfig)
if err != nil { if err != nil {
t.Fatal("Unable to establish connection: " + err.Error()) t.Fatal("Unable to establish connection: " + err.Error())
} }
@ -96,11 +96,11 @@ func TestConnectWithTcp(t *testing.T) {
} }
func TestConnectWithTLS(t *testing.T) { func TestConnectWithTLS(t *testing.T) {
if tlsConnectionParameters == nil { if tlsConnConfig == nil {
return return
} }
conn, err := pgx.Connect(*tlsConnectionParameters) conn, err := pgx.Connect(*tlsConnConfig)
if err != nil { if err != nil {
t.Fatal("Unable to establish connection: " + err.Error()) t.Fatal("Unable to establish connection: " + err.Error())
} }
@ -112,11 +112,11 @@ func TestConnectWithTLS(t *testing.T) {
} }
func TestConnectWithInvalidUser(t *testing.T) { func TestConnectWithInvalidUser(t *testing.T) {
if invalidUserConnectionParameters == nil { if invalidUserConnConfig == nil {
return return
} }
_, err := pgx.Connect(*invalidUserConnectionParameters) _, err := pgx.Connect(*invalidUserConnConfig)
pgErr, ok := err.(pgx.PgError) pgErr, ok := err.(pgx.PgError)
if !ok { if !ok {
t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err) t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err)
@ -127,11 +127,11 @@ func TestConnectWithInvalidUser(t *testing.T) {
} }
func TestConnectWithPlainTextPassword(t *testing.T) { func TestConnectWithPlainTextPassword(t *testing.T) {
if plainPasswordConnectionParameters == nil { if plainPasswordConnConfig == nil {
return return
} }
conn, err := pgx.Connect(*plainPasswordConnectionParameters) conn, err := pgx.Connect(*plainPasswordConnConfig)
if err != nil { if err != nil {
t.Fatal("Unable to establish connection: " + err.Error()) t.Fatal("Unable to establish connection: " + err.Error())
} }
@ -143,11 +143,11 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
} }
func TestConnectWithMD5Password(t *testing.T) { func TestConnectWithMD5Password(t *testing.T) {
if md5ConnectionParameters == nil { if md5ConnConfig == nil {
return return
} }
conn, err := pgx.Connect(*md5ConnectionParameters) conn, err := pgx.Connect(*md5ConnConfig)
if err != nil { if err != nil {
t.Fatal("Unable to establish connection: " + err.Error()) t.Fatal("Unable to establish connection: " + err.Error())
} }
@ -161,11 +161,11 @@ func TestConnectWithMD5Password(t *testing.T) {
func TestParseURI(t *testing.T) { func TestParseURI(t *testing.T) {
tests := []struct { tests := []struct {
url string url string
connParams pgx.ConnectionParameters connParams pgx.ConnConfig
}{ }{
{ {
url: "postgres://jack:secret@localhost:5432/mydb", url: "postgres://jack:secret@localhost:5432/mydb",
connParams: pgx.ConnectionParameters{ connParams: pgx.ConnConfig{
User: "jack", User: "jack",
Password: "secret", Password: "secret",
Host: "localhost", Host: "localhost",
@ -175,7 +175,7 @@ func TestParseURI(t *testing.T) {
}, },
{ {
url: "postgresql://jack:secret@localhost:5432/mydb", url: "postgresql://jack:secret@localhost:5432/mydb",
connParams: pgx.ConnectionParameters{ connParams: pgx.ConnConfig{
User: "jack", User: "jack",
Password: "secret", Password: "secret",
Host: "localhost", Host: "localhost",
@ -185,7 +185,7 @@ func TestParseURI(t *testing.T) {
}, },
{ {
url: "postgres://jack@localhost:5432/mydb", url: "postgres://jack@localhost:5432/mydb",
connParams: pgx.ConnectionParameters{ connParams: pgx.ConnConfig{
User: "jack", User: "jack",
Host: "localhost", Host: "localhost",
Port: 5432, Port: 5432,
@ -194,7 +194,7 @@ func TestParseURI(t *testing.T) {
}, },
{ {
url: "postgres://jack@localhost/mydb", url: "postgres://jack@localhost/mydb",
connParams: pgx.ConnectionParameters{ connParams: pgx.ConnConfig{
User: "jack", User: "jack",
Host: "localhost", Host: "localhost",
Database: "mydb", Database: "mydb",
@ -243,7 +243,7 @@ func TestExecute(t *testing.T) {
} }
func TestExecuteFailure(t *testing.T) { func TestExecuteFailure(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -281,7 +281,7 @@ func TestSelectFunc(t *testing.T) {
} }
func TestSelectFuncFailure(t *testing.T) { func TestSelectFuncFailure(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -298,7 +298,7 @@ func TestSelectFuncFailure(t *testing.T) {
} }
func Example_connectionSelectFunc() { func Example_connectionSelectFunc() {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
fmt.Printf("Unable to establish connection: %v", err) fmt.Printf("Unable to establish connection: %v", err)
return return
@ -344,7 +344,7 @@ func TestSelectRows(t *testing.T) {
} }
func Example_connectionSelectRows() { func Example_connectionSelectRows() {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
fmt.Printf("Unable to establish connection: %v", err) fmt.Printf("Unable to establish connection: %v", err)
return return
@ -513,7 +513,7 @@ func TestSelectValues(t *testing.T) {
} }
func TestPrepare(t *testing.T) { func TestPrepare(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -594,7 +594,7 @@ func TestPrepare(t *testing.T) {
} }
func TestPrepareFailure(t *testing.T) { func TestPrepareFailure(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -610,7 +610,7 @@ func TestPrepareFailure(t *testing.T) {
} }
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -721,7 +721,7 @@ func TestTransaction(t *testing.T) {
} }
func TestTransactionIso(t *testing.T) { func TestTransactionIso(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -742,7 +742,7 @@ func TestTransactionIso(t *testing.T) {
} }
func TestListenNotify(t *testing.T) { func TestListenNotify(t *testing.T) {
listener, err := pgx.Connect(*defaultConnectionParameters) listener, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -796,7 +796,7 @@ func TestListenNotify(t *testing.T) {
} }
func TestFatalRxError(t *testing.T) { func TestFatalRxError(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -812,7 +812,7 @@ func TestFatalRxError(t *testing.T) {
} }
}() }()
otherConn, err := pgx.Connect(*defaultConnectionParameters) otherConn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
@ -830,13 +830,13 @@ func TestFatalRxError(t *testing.T) {
} }
func TestFatalTxError(t *testing.T) { func TestFatalTxError(t *testing.T) {
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }
defer conn.Close() defer conn.Close()
otherConn, err := pgx.Connect(*defaultConnectionParameters) otherConn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }

View File

@ -15,7 +15,7 @@ type ConnectionPool struct {
allConnections []*Conn allConnections []*Conn
availableConnections []*Conn availableConnections []*Conn
cond *sync.Cond cond *sync.Cond
parameters ConnectionParameters // parameters used when establishing connection config ConnConfig // config used when establishing connection
maxConnections int maxConnections int
afterConnect func(*Conn) error afterConnect func(*Conn) error
logger Logger logger Logger
@ -27,11 +27,11 @@ type ConnectionPoolStat struct {
AvailableConnections int // unused live connections AvailableConnections int // unused live connections
} }
// NewConnectionPool creates a new ConnectionPool. parameters are passed through to // NewConnectionPool creates a new ConnectionPool. config are passed through to
// Connect directly. // Connect directly.
func NewConnectionPool(parameters ConnectionParameters, options ConnectionPoolOptions) (p *ConnectionPool, err error) { func NewConnectionPool(config ConnConfig, options ConnectionPoolOptions) (p *ConnectionPool, err error) {
p = new(ConnectionPool) p = new(ConnectionPool)
p.parameters = parameters p.config = config
p.maxConnections = options.MaxConnections p.maxConnections = options.MaxConnections
p.afterConnect = options.AfterConnect p.afterConnect = options.AfterConnect
if options.Logger != nil { if options.Logger != nil {
@ -143,7 +143,7 @@ func (p *ConnectionPool) CurrentConnectionCount() int {
} }
func (p *ConnectionPool) createConnection() (c *Conn, err error) { func (p *ConnectionPool) createConnection() (c *Conn, err error) {
c, err = Connect(p.parameters) c, err = Connect(p.config)
if err != nil { if err != nil {
return return
} }

View File

@ -10,7 +10,7 @@ import (
func createConnectionPool(t *testing.T, maxConnections int) *pgx.ConnectionPool { func createConnectionPool(t *testing.T, maxConnections int) *pgx.ConnectionPool {
options := pgx.ConnectionPoolOptions{MaxConnections: maxConnections} options := pgx.ConnectionPoolOptions{MaxConnections: maxConnections}
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, options) pool, err := pgx.NewConnectionPool(*defaultConnConfig, options)
if err != nil { if err != nil {
t.Fatalf("Unable to create connection pool: %v", err) t.Fatalf("Unable to create connection pool: %v", err)
} }
@ -25,7 +25,7 @@ func TestNewConnectionPool(t *testing.T) {
} }
options := pgx.ConnectionPoolOptions{MaxConnections: 2, AfterConnect: afterConnect} options := pgx.ConnectionPoolOptions{MaxConnections: 2, AfterConnect: afterConnect}
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, options) pool, err := pgx.NewConnectionPool(*defaultConnConfig, options)
if err != nil { if err != nil {
t.Fatal("Unable to establish connection pool") t.Fatal("Unable to establish connection pool")
} }
@ -44,7 +44,7 @@ func TestNewConnectionPool(t *testing.T) {
} }
options = pgx.ConnectionPoolOptions{MaxConnections: 2, AfterConnect: afterConnect} options = pgx.ConnectionPoolOptions{MaxConnections: 2, AfterConnect: afterConnect}
pool, err = pgx.NewConnectionPool(*defaultConnectionParameters, options) pool, err = pgx.NewConnectionPool(*defaultConnConfig, options)
if err != errAfterConnect { if err != errAfterConnect {
t.Errorf("Expected errAfterConnect but received unexpected: %v", err) t.Errorf("Expected errAfterConnect but received unexpected: %v", err)
} }

View File

@ -1,22 +0,0 @@
package pgx_test
import (
"github.com/JackC/pgx"
)
var defaultConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// To skip tests for specific connection / authentication types set that connection param to nil
var tcpConnectionParameters *pgx.ConnectionParameters = nil
var unixSocketConnectionParameters *pgx.ConnectionParameters = nil
var md5ConnectionParameters *pgx.ConnectionParameters = nil
var plainPasswordConnectionParameters *pgx.ConnectionParameters = nil
var noPasswordConnectionParameters *pgx.ConnectionParameters = nil
var invalidUserConnectionParameters *pgx.ConnectionParameters = nil
// var tcpConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var unixSocketConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Socket: "/private/tmp", User: "pgx_none", Database: "pgx_test"}
// var md5ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var plainPasswordConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_pw", Password: "secret", Database: "pgx_test"}
// var noPasswordConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_none", Database: "pgx_test"}
// var invalidUserConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "invalid", Database: "pgx_test"}

View File

@ -23,7 +23,7 @@ func Example_customValueTranscoder() {
DecodeText: decodePointFromText, DecodeText: decodePointFromText,
EncodeTo: encodePoint} EncodeTo: encodePoint}
conn, err := pgx.Connect(*defaultConnectionParameters) conn, err := pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
fmt.Printf("Unable to establish connection: %v", err) fmt.Printf("Unable to establish connection: %v", err)
return return

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/jackc/pgx" "github.com/JackC/pgx"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -97,7 +97,7 @@ func urlHandler(w http.ResponseWriter, req *http.Request) {
func main() { func main() {
var err error var err error
connectionOptions := pgx.ConnectionParameters{ connectionOptions := pgx.ConnConfig{
Host: "127.0.0.1", Host: "127.0.0.1",
User: "jack", User: "jack",
Password: "jack", Password: "jack",

View File

@ -11,7 +11,7 @@ var sharedConnection *pgx.Conn
func getSharedConnection(t testing.TB) (c *pgx.Conn) { func getSharedConnection(t testing.TB) (c *pgx.Conn) {
if sharedConnection == nil || !sharedConnection.IsAlive() { if sharedConnection == nil || !sharedConnection.IsAlive() {
var err error var err error
sharedConnection, err = pgx.Connect(*defaultConnectionParameters) sharedConnection, err = pgx.Connect(*defaultConnConfig)
if err != nil { if err != nil {
t.Fatalf("Unable to establish connection: %v", err) t.Fatalf("Unable to establish connection: %v", err)
} }