mirror of
https://github.com/jackc/pgx.git
synced 2025-04-27 21:25:53 +00:00
Make ConnectionParameters fields public
This commit is contained in:
parent
d1a56c95d2
commit
19d4a4d577
@ -11,12 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ConnectionParameters struct {
|
type ConnectionParameters struct {
|
||||||
socket string // path to unix domain socket (e.g. /private/tmp/.s.PGSQL.5432)
|
Socket string // path to unix domain socket (e.g. /private/tmp/.s.PGSQL.5432)
|
||||||
host string
|
Host string
|
||||||
port uint16 // default: 5432
|
Port uint16 // default: 5432
|
||||||
database string
|
Database string
|
||||||
user string
|
User string
|
||||||
password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
@ -33,17 +33,17 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
|
|||||||
c = new(Connection)
|
c = new(Connection)
|
||||||
|
|
||||||
c.parameters = parameters
|
c.parameters = parameters
|
||||||
if c.parameters.port == 0 {
|
if c.parameters.Port == 0 {
|
||||||
c.parameters.port = 5432
|
c.parameters.Port = 5432
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.parameters.socket != "" {
|
if c.parameters.Socket != "" {
|
||||||
c.conn, err = net.Dial("unix", c.parameters.socket)
|
c.conn, err = net.Dial("unix", c.parameters.Socket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else if c.parameters.host != "" {
|
} else if c.parameters.Host != "" {
|
||||||
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.parameters.Host, c.parameters.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -53,9 +53,9 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
|
|||||||
c.runtimeParams = make(map[string]string)
|
c.runtimeParams = make(map[string]string)
|
||||||
|
|
||||||
msg := newStartupMessage()
|
msg := newStartupMessage()
|
||||||
msg.options["user"] = c.parameters.user
|
msg.options["user"] = c.parameters.User
|
||||||
if c.parameters.database != "" {
|
if c.parameters.Database != "" {
|
||||||
msg.options["database"] = c.parameters.database
|
msg.options["database"] = c.parameters.Database
|
||||||
}
|
}
|
||||||
c.txStartupMessage(msg)
|
c.txStartupMessage(msg)
|
||||||
|
|
||||||
@ -244,10 +244,10 @@ func (c *Connection) rxAuthenticationX(r *messageReader) (err error) {
|
|||||||
switch code {
|
switch code {
|
||||||
case 0: // AuthenticationOk
|
case 0: // AuthenticationOk
|
||||||
case 3: // AuthenticationCleartextPassword
|
case 3: // AuthenticationCleartextPassword
|
||||||
c.txPasswordMessage(c.parameters.password)
|
c.txPasswordMessage(c.parameters.Password)
|
||||||
case 5: // AuthenticationMD5Password
|
case 5: // AuthenticationMD5Password
|
||||||
salt := r.readByteString(4)
|
salt := r.readByteString(4)
|
||||||
digestedPassword := "md5" + hexMD5(hexMD5(c.parameters.password+c.parameters.user)+salt)
|
digestedPassword := "md5" + hexMD5(hexMD5(c.parameters.Password+c.parameters.User)+salt)
|
||||||
c.txPasswordMessage(digestedPassword)
|
c.txPasswordMessage(digestedPassword)
|
||||||
default:
|
default:
|
||||||
err = errors.New("Received unknown authentication message")
|
err = errors.New("Received unknown authentication message")
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func createConnectionPool(maxConnections int) *ConnectionPool {
|
func createConnectionPool(maxConnections int) *ConnectionPool {
|
||||||
connectionOptions := ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_none", database: "pgx_test"}
|
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
|
||||||
pool, err := NewConnectionPool(connectionOptions, maxConnections)
|
pool, err := NewConnectionPool(connectionOptions, maxConnections)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Unable to create connection pool")
|
panic("Unable to create connection pool")
|
||||||
@ -15,7 +15,7 @@ func createConnectionPool(maxConnections int) *ConnectionPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewConnectionPool(t *testing.T) {
|
func TestNewConnectionPool(t *testing.T) {
|
||||||
connectionOptions := ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_none", database: "pgx_test"}
|
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
|
||||||
pool, err := NewConnectionPool(connectionOptions, 5)
|
pool, err := NewConnectionPool(connectionOptions, 5)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection pool")
|
t.Fatal("Unable to establish connection pool")
|
||||||
|
@ -9,7 +9,7 @@ var SharedConnection *Connection
|
|||||||
func getSharedConnection() (c *Connection) {
|
func getSharedConnection() (c *Connection) {
|
||||||
if SharedConnection == nil {
|
if SharedConnection == nil {
|
||||||
var err error
|
var err error
|
||||||
SharedConnection, err = Connect(ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_none", database: "pgx_test"})
|
SharedConnection, err = Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Unable to establish connection")
|
panic("Unable to establish connection")
|
||||||
}
|
}
|
||||||
@ -19,7 +19,7 @@ func getSharedConnection() (c *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
conn, err := Connect(ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_none", database: "pgx_test"})
|
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection")
|
t.Fatal("Unable to establish connection")
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func TestConnect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithTcp(t *testing.T) {
|
func TestConnectWithTcp(t *testing.T) {
|
||||||
conn, err := Connect(ConnectionParameters{host: "127.0.0.1", user: "pgx_md5", password: "secret", database: "pgx_test"})
|
conn, err := Connect(ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ func TestConnectWithTcp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithInvalidUser(t *testing.T) {
|
func TestConnectWithInvalidUser(t *testing.T) {
|
||||||
_, err := Connect(ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "invalid_user", database: "pgx_test"})
|
_, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "invalid_user", Database: "pgx_test"})
|
||||||
pgErr := err.(PgError)
|
pgErr := err.(PgError)
|
||||||
if pgErr.Code != "28000" {
|
if pgErr.Code != "28000" {
|
||||||
t.Fatal("Did not receive expected error when connecting with invalid user")
|
t.Fatal("Did not receive expected error when connecting with invalid user")
|
||||||
@ -74,7 +74,7 @@ func TestConnectWithInvalidUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithPlainTextPassword(t *testing.T) {
|
func TestConnectWithPlainTextPassword(t *testing.T) {
|
||||||
conn, err := Connect(ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_pw", password: "secret", database: "pgx_test"})
|
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_pw", Password: "secret", Database: "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithMD5Password(t *testing.T) {
|
func TestConnectWithMD5Password(t *testing.T) {
|
||||||
conn, err := Connect(ConnectionParameters{socket: "/private/tmp/.s.PGSQL.5432", user: "pgx_md5", password: "secret", database: "pgx_test"})
|
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_md5", Password: "secret", Database: "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user