mirror of https://github.com/jackc/pgx.git
Add ConnStr getter to Pool and Conn structs.
parent
1b54d15e93
commit
a62de87342
7
conn.go
7
conn.go
|
@ -29,6 +29,9 @@ type ConnConfig struct {
|
|||
Logger Logger
|
||||
LogLevel LogLevel
|
||||
|
||||
// Original connection string that was parsed into config.
|
||||
ConnStr string
|
||||
|
||||
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
||||
// to nil to disable automatic prepared statements.
|
||||
BuildStatementCache BuildStatementCacheFunc
|
||||
|
@ -157,6 +160,7 @@ func ParseConfig(connString string) (*ConnConfig, error) {
|
|||
createdByParseConfig: true,
|
||||
LogLevel: LogLevelInfo,
|
||||
BuildStatementCache: buildStatementCache,
|
||||
ConnStr: connString,
|
||||
}
|
||||
|
||||
return connConfig, nil
|
||||
|
@ -418,6 +422,9 @@ func (c *Conn) StatementCache() stmtcache.Cache { return c.stmtcache }
|
|||
// ConnInfo returns the connection info used for this connection.
|
||||
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
|
||||
|
||||
// ConnStr returns the connection string that was used to establish this connection.
|
||||
func (c *Conn) ConnStr() string { return c.config.ConnStr }
|
||||
|
||||
// Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced
|
||||
// positionally from the sql string as $1, $2, etc.
|
||||
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||
|
|
14
conn_test.go
14
conn_test.go
|
@ -28,6 +28,8 @@ func TestCrateDBConnect(t *testing.T) {
|
|||
require.Nil(t, err)
|
||||
defer closeConn(t, conn)
|
||||
|
||||
assert.Equal(t, connString, conn.ConnStr())
|
||||
|
||||
var result int
|
||||
err = conn.QueryRow(context.Background(), "select 1 +1").Scan(&result)
|
||||
if err != nil {
|
||||
|
@ -41,13 +43,16 @@ func TestCrateDBConnect(t *testing.T) {
|
|||
func TestConnect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
||||
connStr := os.Getenv("PGX_TEST_DATABASE")
|
||||
config := mustParseConfig(t, connStr)
|
||||
|
||||
conn, err := pgx.ConnectConfig(context.Background(), config)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to establish connection: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, connStr, conn.ConnStr())
|
||||
|
||||
var currentDB string
|
||||
err = conn.QueryRow(context.Background(), "select current_database()").Scan(¤tDB)
|
||||
if err != nil {
|
||||
|
@ -104,6 +109,13 @@ func TestConnectConfigRequiresConnConfigFromParseConfig(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestConfigContainsConnStr(t *testing.T) {
|
||||
connStr := os.Getenv("PGX_TEST_DATABASE")
|
||||
config, err := pgx.ParseConfig(connStr)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, connStr, config.ConnStr)
|
||||
}
|
||||
|
||||
func TestParseConfigExtractsStatementCacheOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
1
go.sum
1
go.sum
|
@ -69,6 +69,7 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b h1:cIcUpcEP55F/QuZWEt
|
|||
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9 h1:KLBBPU++1T3DHtm1B1QaIHy80Vhu0wNMErIFCNgAL8Y=
|
||||
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.1.1 h1:PJAw7H/9hoWC4Kf3J8iNmL1SwA6E8vfsLqBiL+F6CtI=
|
||||
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
|
|
|
@ -69,6 +69,7 @@ func (cr *connResource) getPoolRows(c *Conn, r pgx.Rows) *poolRows {
|
|||
|
||||
type Pool struct {
|
||||
p *puddle.Pool
|
||||
connStr string
|
||||
afterConnect func(context.Context, *pgx.Conn) error
|
||||
beforeAcquire func(context.Context, *pgx.Conn) bool
|
||||
afterRelease func(*pgx.Conn) bool
|
||||
|
@ -141,6 +142,7 @@ func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
|
|||
}
|
||||
|
||||
p := &Pool{
|
||||
connStr: config.ConnConfig.ConnStr,
|
||||
afterConnect: config.AfterConnect,
|
||||
beforeAcquire: config.BeforeAcquire,
|
||||
afterRelease: config.AfterRelease,
|
||||
|
@ -369,6 +371,9 @@ func (p *Pool) AcquireAllIdle(ctx context.Context) []*Conn {
|
|||
return conns
|
||||
}
|
||||
|
||||
// ConnStr returns the connection string that was used to initialize this pool.
|
||||
func (p *Pool) ConnStr() string { return p.connStr }
|
||||
|
||||
func (p *Pool) Stat() *Stat {
|
||||
return &Stat{s: p.p.Stat()}
|
||||
}
|
||||
|
|
|
@ -14,9 +14,10 @@ import (
|
|||
|
||||
func TestConnect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pool, err := pgxpool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
|
||||
connStr := os.Getenv("PGX_TEST_DATABASE")
|
||||
pool, err := pgxpool.Connect(context.Background(), connStr)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, connStr, pool.ConnStr())
|
||||
pool.Close()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue