From 143bc3165d8db50877aa745ce40f687a7e1fd406 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 12 Oct 2019 09:16:26 -0500 Subject: [PATCH] Add Conn() to Tx interface. This is necessary to allow code using a Tx to access the *Conn (and pgconn.PgConn) on which the Tx is executing. --- CHANGELOG.md | 7 ++++++- pgxpool/tx.go | 4 ++++ tx.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d17b01bb..a4fdaf92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Unreleased -* Conn.Begin and Conn.BeginTx return a Tx interface instead of the internal dbTx struct. This is necessary for the Conn.Begin method to signature as other methods that begin a transaction. This is technically a breaking change but practically is just a bug fix that is extremely unlikely to break any existing code. +## Potentially Breaking Changes + +Technically, two changes are breaking changes, but in practice these are extremely unlikely to break existing code. + +* Conn.Begin and Conn.BeginTx return a Tx interface instead of the internal dbTx struct. This is necessary for the Conn.Begin method to signature as other methods that begin a transaction. +* Add Conn() to Tx interface. This is necessary to allow code using a Tx to access the *Conn (and pgconn.PgConn) on which the Tx is executing. # 4.0.1 (September 19, 2019) diff --git a/pgxpool/tx.go b/pgxpool/tx.go index c1a3c8ad..3ff5cb95 100644 --- a/pgxpool/tx.go +++ b/pgxpool/tx.go @@ -61,3 +61,7 @@ func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.R func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row { return tx.t.QueryRow(ctx, sql, args...) } + +func (tx *Tx) Conn() *pgx.Conn { + return tx.t.Conn() +} diff --git a/tx.go b/tx.go index 5822e9f5..05a7256e 100644 --- a/tx.go +++ b/tx.go @@ -111,6 +111,9 @@ type Tx interface { Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) QueryRow(ctx context.Context, sql string, args ...interface{}) Row + + // Conn returns the underlying *Conn that on which this transaction is executing. + Conn() *Conn } // dbTx represents a database transaction. @@ -234,6 +237,10 @@ func (tx *dbTx) LargeObjects() LargeObjects { return LargeObjects{tx: tx} } +func (tx *dbTx) Conn() *Conn { + return tx.conn +} + // dbSavepoint represents a nested transaction implemented by a savepoint. type dbSavepoint struct { tx Tx @@ -330,3 +337,7 @@ func (sp *dbSavepoint) SendBatch(ctx context.Context, b *Batch) BatchResults { func (sp *dbSavepoint) LargeObjects() LargeObjects { return LargeObjects{tx: sp} } + +func (sp *dbSavepoint) Conn() *Conn { + return sp.tx.Conn() +}