Skip to content

Commit 20c6c44

Browse files
committed
Expose Conn.Config() and Pool.Config().
1 parent 33cbec3 commit 20c6c44

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

conn.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type ConnConfig struct {
3030
LogLevel LogLevel
3131

3232
// Original connection string that was parsed into config.
33-
ConnString string
33+
connString string
3434

3535
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
3636
// to nil to disable automatic prepared statements.
@@ -47,6 +47,8 @@ type ConnConfig struct {
4747
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
4848
}
4949

50+
func (cc *ConnConfig) ConnString() string { return cc.connString }
51+
5052
// BuildStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
5153
type BuildStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
5254

@@ -160,7 +162,7 @@ func ParseConfig(connString string) (*ConnConfig, error) {
160162
createdByParseConfig: true,
161163
LogLevel: LogLevelInfo,
162164
BuildStatementCache: buildStatementCache,
163-
ConnString: connString,
165+
connString: connString,
164166
}
165167

166168
return connConfig, nil
@@ -423,7 +425,10 @@ func (c *Conn) StatementCache() stmtcache.Cache { return c.stmtcache }
423425
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
424426

425427
// ConnString returns the connection string that was used to establish this connection.
426-
func (c *Conn) ConnString() string { return c.config.ConnString }
428+
func (c *Conn) ConnString() string { return c.config.ConnString() }
429+
430+
// Config returns config that was used to establish this connection.
431+
func (c *Conn) Config() *ConnConfig { return c.config }
427432

428433
// Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced
429434
// positionally from the sql string as $1, $2, etc.

conn_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestCrateDBConnect(t *testing.T) {
2828
require.Nil(t, err)
2929
defer closeConn(t, conn)
3030

31+
assert.Equal(t, connString, conn.Config().ConnString())
3132
assert.Equal(t, connString, conn.ConnString())
3233

3334
var result int
@@ -43,15 +44,16 @@ func TestCrateDBConnect(t *testing.T) {
4344
func TestConnect(t *testing.T) {
4445
t.Parallel()
4546

46-
connStr := os.Getenv("PGX_TEST_DATABASE")
47-
config := mustParseConfig(t, connStr)
47+
connString := os.Getenv("PGX_TEST_DATABASE")
48+
config := mustParseConfig(t, connString)
4849

4950
conn, err := pgx.ConnectConfig(context.Background(), config)
5051
if err != nil {
5152
t.Fatalf("Unable to establish connection: %v", err)
5253
}
5354

54-
assert.Equal(t, connStr, conn.ConnString())
55+
assert.Equal(t, connString, conn.Config().ConnString())
56+
assert.Equal(t, connString, conn.ConnString())
5557

5658
var currentDB string
5759
err = conn.QueryRow(context.Background(), "select current_database()").Scan(&currentDB)
@@ -113,7 +115,7 @@ func TestConfigContainsConnStr(t *testing.T) {
113115
connStr := os.Getenv("PGX_TEST_DATABASE")
114116
config, err := pgx.ParseConfig(connStr)
115117
require.NoError(t, err)
116-
assert.Equal(t, connStr, config.ConnString)
118+
assert.Equal(t, connStr, config.ConnString())
117119
}
118120

119121
func TestParseConfigExtractsStatementCacheOptions(t *testing.T) {

pgxpool/pool.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (cr *connResource) getPoolRows(c *Conn, r pgx.Rows) *poolRows {
6969

7070
type Pool struct {
7171
p *puddle.Pool
72-
connString string
72+
config *Config
7373
afterConnect func(context.Context, *pgx.Conn) error
7474
beforeAcquire func(context.Context, *pgx.Conn) bool
7575
afterRelease func(*pgx.Conn) bool
@@ -142,7 +142,7 @@ func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
142142
}
143143

144144
p := &Pool{
145-
connString: config.ConnConfig.ConnString,
145+
config: config,
146146
afterConnect: config.AfterConnect,
147147
beforeAcquire: config.BeforeAcquire,
148148
afterRelease: config.AfterRelease,
@@ -372,7 +372,10 @@ func (p *Pool) AcquireAllIdle(ctx context.Context) []*Conn {
372372
}
373373

374374
// ConnString returns the connection string that was used to initialize this pool.
375-
func (p *Pool) ConnString() string { return p.connString }
375+
func (p *Pool) ConnString() string { return p.config.ConnConfig.ConnString() }
376+
377+
// Config returns config that was used to initialize this pool.
378+
func (p *Pool) Config() *Config { return p.config }
376379

377380
func (p *Pool) Stat() *Stat {
378381
return &Stat{s: p.p.Stat()}

pgxpool/pool_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@ import (
1414

1515
func TestConnect(t *testing.T) {
1616
t.Parallel()
17-
connStr := os.Getenv("PGX_TEST_DATABASE")
18-
pool, err := pgxpool.Connect(context.Background(), connStr)
17+
connString := os.Getenv("PGX_TEST_DATABASE")
18+
pool, err := pgxpool.Connect(context.Background(), connString)
1919
require.NoError(t, err)
20-
assert.Equal(t, connStr, pool.ConnString())
20+
assert.Equal(t, connString, pool.ConnString())
21+
pool.Close()
22+
}
23+
24+
func TestConnectConfig(t *testing.T) {
25+
t.Parallel()
26+
connString := os.Getenv("PGX_TEST_DATABASE")
27+
config, err := pgxpool.ParseConfig(connString)
28+
require.NoError(t, err)
29+
pool, err := pgxpool.ConnectConfig(context.Background(), config)
30+
require.NoError(t, err)
31+
assert.Equal(t, config, pool.Config())
2132
pool.Close()
2233
}
2334

0 commit comments

Comments
 (0)