Merge branch 'store-connection-string' of git://github.com/georgysavva/pgx into georgysavva-store-connection-string
This commit is contained in:
@@ -29,6 +29,9 @@ type ConnConfig struct {
|
|||||||
Logger Logger
|
Logger Logger
|
||||||
LogLevel LogLevel
|
LogLevel LogLevel
|
||||||
|
|
||||||
|
// Original connection string that was parsed into config.
|
||||||
|
connString string
|
||||||
|
|
||||||
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
||||||
// to nil to disable automatic prepared statements.
|
// to nil to disable automatic prepared statements.
|
||||||
BuildStatementCache BuildStatementCacheFunc
|
BuildStatementCache BuildStatementCacheFunc
|
||||||
@@ -44,6 +47,8 @@ type ConnConfig struct {
|
|||||||
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cc *ConnConfig) ConnString() string { return cc.connString }
|
||||||
|
|
||||||
// BuildStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
|
// BuildStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
|
||||||
type BuildStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
|
type BuildStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
|
||||||
|
|
||||||
@@ -157,6 +162,7 @@ func ParseConfig(connString string) (*ConnConfig, error) {
|
|||||||
createdByParseConfig: true,
|
createdByParseConfig: true,
|
||||||
LogLevel: LogLevelInfo,
|
LogLevel: LogLevelInfo,
|
||||||
BuildStatementCache: buildStatementCache,
|
BuildStatementCache: buildStatementCache,
|
||||||
|
connString: connString,
|
||||||
}
|
}
|
||||||
|
|
||||||
return connConfig, nil
|
return connConfig, nil
|
||||||
@@ -418,6 +424,12 @@ func (c *Conn) StatementCache() stmtcache.Cache { return c.stmtcache }
|
|||||||
// ConnInfo returns the connection info used for this connection.
|
// ConnInfo returns the connection info used for this connection.
|
||||||
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
|
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
|
||||||
|
|
||||||
|
// ConnString returns the connection string that was used to establish this connection.
|
||||||
|
func (c *Conn) ConnString() string { return c.config.ConnString() }
|
||||||
|
|
||||||
|
// Config returns config that was used to establish this connection.
|
||||||
|
func (c *Conn) Config() *ConnConfig { return c.config }
|
||||||
|
|
||||||
// Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced
|
// 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.
|
// positionally from the sql string as $1, $2, etc.
|
||||||
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||||
|
|||||||
+15
-1
@@ -28,6 +28,9 @@ func TestCrateDBConnect(t *testing.T) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
|
assert.Equal(t, connString, conn.Config().ConnString())
|
||||||
|
assert.Equal(t, connString, conn.ConnString())
|
||||||
|
|
||||||
var result int
|
var result int
|
||||||
err = conn.QueryRow(context.Background(), "select 1 +1").Scan(&result)
|
err = conn.QueryRow(context.Background(), "select 1 +1").Scan(&result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -41,13 +44,17 @@ func TestCrateDBConnect(t *testing.T) {
|
|||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
connString := os.Getenv("PGX_TEST_DATABASE")
|
||||||
|
config := mustParseConfig(t, connString)
|
||||||
|
|
||||||
conn, err := pgx.ConnectConfig(context.Background(), config)
|
conn, err := pgx.ConnectConfig(context.Background(), config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, connString, conn.Config().ConnString())
|
||||||
|
assert.Equal(t, connString, conn.ConnString())
|
||||||
|
|
||||||
var currentDB string
|
var currentDB string
|
||||||
err = conn.QueryRow(context.Background(), "select current_database()").Scan(¤tDB)
|
err = conn.QueryRow(context.Background(), "select current_database()").Scan(¤tDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -104,6 +111,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.ConnString())
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseConfigExtractsStatementCacheOptions(t *testing.T) {
|
func TestParseConfigExtractsStatementCacheOptions(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ func (cr *connResource) getPoolRows(c *Conn, r pgx.Rows) *poolRows {
|
|||||||
|
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
p *puddle.Pool
|
p *puddle.Pool
|
||||||
|
config *Config
|
||||||
afterConnect func(context.Context, *pgx.Conn) error
|
afterConnect func(context.Context, *pgx.Conn) error
|
||||||
beforeAcquire func(context.Context, *pgx.Conn) bool
|
beforeAcquire func(context.Context, *pgx.Conn) bool
|
||||||
afterRelease func(*pgx.Conn) bool
|
afterRelease func(*pgx.Conn) bool
|
||||||
@@ -141,6 +142,7 @@ func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p := &Pool{
|
p := &Pool{
|
||||||
|
config: config,
|
||||||
afterConnect: config.AfterConnect,
|
afterConnect: config.AfterConnect,
|
||||||
beforeAcquire: config.BeforeAcquire,
|
beforeAcquire: config.BeforeAcquire,
|
||||||
afterRelease: config.AfterRelease,
|
afterRelease: config.AfterRelease,
|
||||||
@@ -369,6 +371,12 @@ func (p *Pool) AcquireAllIdle(ctx context.Context) []*Conn {
|
|||||||
return conns
|
return conns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConnString returns the connection string that was used to initialize this pool.
|
||||||
|
func (p *Pool) ConnString() string { return p.config.ConnConfig.ConnString() }
|
||||||
|
|
||||||
|
// Config returns config that was used to initialize this pool.
|
||||||
|
func (p *Pool) Config() *Config { return p.config }
|
||||||
|
|
||||||
func (p *Pool) Stat() *Stat {
|
func (p *Pool) Stat() *Stat {
|
||||||
return &Stat{s: p.p.Stat()}
|
return &Stat{s: p.p.Stat()}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -14,9 +14,21 @@ import (
|
|||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
connString := os.Getenv("PGX_TEST_DATABASE")
|
||||||
pool, err := pgxpool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
|
pool, err := pgxpool.Connect(context.Background(), connString)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, connString, pool.ConnString())
|
||||||
|
pool.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConnectConfig(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
connString := os.Getenv("PGX_TEST_DATABASE")
|
||||||
|
config, err := pgxpool.ParseConfig(connString)
|
||||||
|
require.NoError(t, err)
|
||||||
|
pool, err := pgxpool.ConnectConfig(context.Background(), config)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, config, pool.Config())
|
||||||
pool.Close()
|
pool.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user