mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Use struct instead of map[string]string for connection params
This commit is contained in:
parent
70e461b769
commit
460acd7757
@ -10,32 +10,35 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ConnectionParameters struct {
|
||||||
|
socket string // path to unix domain socket (e.g. /private/tmp/.s.PGSQL.5432)
|
||||||
|
database string
|
||||||
|
user string
|
||||||
|
password string
|
||||||
|
}
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
conn net.Conn // the underlying TCP or unix domain socket connection
|
conn net.Conn // the underlying TCP or unix domain socket connection
|
||||||
buf []byte // work buffer to avoid constant alloc and dealloc
|
buf []byte // work buffer to avoid constant alloc and dealloc
|
||||||
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
|
||||||
options map[string]string // options used when establishing connection
|
parameters ConnectionParameters // parameters used when establishing this connection
|
||||||
txStatus byte
|
txStatus byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// options:
|
// options:
|
||||||
// socket: path to unix domain socket
|
// socket: path to unix domain socket
|
||||||
|
// host: TCP address
|
||||||
|
// port:
|
||||||
// database: name of database
|
// database: name of database
|
||||||
func Connect(options map[string]string) (c *Connection, err error) {
|
func Connect(paramaters ConnectionParameters) (c *Connection, err error) {
|
||||||
c = new(Connection)
|
c = new(Connection)
|
||||||
|
|
||||||
c.options = make(map[string]string)
|
c.parameters = paramaters
|
||||||
for k, v := range options {
|
|
||||||
c.options[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
var present bool
|
if c.parameters.socket != "" {
|
||||||
var socket string
|
c.conn, err = net.Dial("unix", c.parameters.socket)
|
||||||
|
|
||||||
if socket, present = options["socket"]; present {
|
|
||||||
c.conn, err = net.Dial("unix", socket)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -46,12 +49,10 @@ func Connect(options map[string]string) (c *Connection, err error) {
|
|||||||
|
|
||||||
// conn, err := net.Dial("tcp", "localhost:5432")
|
// conn, err := net.Dial("tcp", "localhost:5432")
|
||||||
|
|
||||||
var database string
|
|
||||||
|
|
||||||
msg := newStartupMessage()
|
msg := newStartupMessage()
|
||||||
msg.options["user"], _ = options["user"]
|
msg.options["user"] = c.parameters.user
|
||||||
if database, present = options["database"]; present {
|
if c.parameters.database != "" {
|
||||||
msg.options["database"] = database
|
msg.options["database"] = c.parameters.database
|
||||||
}
|
}
|
||||||
c.txStartupMessage(msg)
|
c.txStartupMessage(msg)
|
||||||
|
|
||||||
@ -240,10 +241,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.options["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.options["password"]+c.options["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")
|
||||||
|
@ -2,25 +2,22 @@ package pgx
|
|||||||
|
|
||||||
type ConnectionPool struct {
|
type ConnectionPool struct {
|
||||||
connectionChannel chan *Connection
|
connectionChannel chan *Connection
|
||||||
options map[string]string // options used when establishing connection
|
parameters ConnectionParameters // options used when establishing connection
|
||||||
MaxConnections int
|
MaxConnections int
|
||||||
}
|
}
|
||||||
|
|
||||||
// options: options used by Connect
|
// options: options used by Connect
|
||||||
// MaxConnections: max simultaneous connections to use (currently all are immediately connected)
|
// MaxConnections: max simultaneous connections to use (currently all are immediately connected)
|
||||||
func NewConnectionPool(options map[string]string, MaxConnections int) (p *ConnectionPool, err error) {
|
func NewConnectionPool(parameters ConnectionParameters, MaxConnections int) (p *ConnectionPool, err error) {
|
||||||
p = new(ConnectionPool)
|
p = new(ConnectionPool)
|
||||||
p.connectionChannel = make(chan *Connection, MaxConnections)
|
p.connectionChannel = make(chan *Connection, MaxConnections)
|
||||||
p.MaxConnections = MaxConnections
|
p.MaxConnections = MaxConnections
|
||||||
|
|
||||||
p.options = make(map[string]string)
|
p.parameters = parameters
|
||||||
for k, v := range options {
|
|
||||||
p.options[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < p.MaxConnections; i++ {
|
for i := 0; i < p.MaxConnections; i++ {
|
||||||
var c *Connection
|
var c *Connection
|
||||||
c, err = Connect(options)
|
c, err = Connect(p.parameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func createConnectionPool(maxConnections int) *ConnectionPool {
|
func createConnectionPool(maxConnections int) *ConnectionPool {
|
||||||
connectionOptions := map[string]string{"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 := map[string]string{"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(map[string]string{"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(map[string]string{"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 TestConnectWithInvalidUser(t *testing.T) {
|
func TestConnectWithInvalidUser(t *testing.T) {
|
||||||
_, err := Connect(map[string]string{"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")
|
||||||
@ -62,7 +62,7 @@ func TestConnectWithInvalidUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithPlainTextPassword(t *testing.T) {
|
func TestConnectWithPlainTextPassword(t *testing.T) {
|
||||||
conn, err := Connect(map[string]string{"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())
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithMD5Password(t *testing.T) {
|
func TestConnectWithMD5Password(t *testing.T) {
|
||||||
conn, err := Connect(map[string]string{"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