diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-08-01 00:37:31 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-08-01 00:37:31 +0200 |
| commit | 70a14c4d03b2489b39a35cbcaf15613952ffcfaa (patch) | |
| tree | c3fe71bf052042cf5009a3a14515670d229613e4 /interface.go | |
| parent | 8871cb8f2b8163d803f8f5ad2c2dc2a3f345bb0b (diff) | |
| download | go-gg-pgx-70a14c4d03b2489b39a35cbcaf15613952ffcfaa.tar.gz | |
Diffstat (limited to 'interface.go')
| -rw-r--r-- | interface.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/interface.go b/interface.go index b9d7def..2b9b8cf 100644 --- a/interface.go +++ b/interface.go @@ -45,6 +45,15 @@ func (conn *Conn) BeginTx(ctx context.Context, opts pgx.TxOptions) (*Tx, error) return maybeNewTx(tx), err } +// WithinTransaction executes an action within a database transaction. +// The transaction will be committed if the callback returns successfully, or rolled back otherwise. +// +// This is equivalent to the GSQLTransact() method of conn's [gsql.ConnectionHandle] implementation, +// but the callback receives the concrete type [*Tx] instead of a generic [gsql.Handle]. +func (conn *Conn) WithinTransaction(ctx context.Context, action func(*Tx) error) error { + return withinTransactionOfConn(ctx, conn.Conn, action) +} + // Pool wraps [*pgxpool.Pool] into a [gsql.Handle]. // // Because this type has [*pgxpool.Pool] as an embedded field, @@ -94,6 +103,15 @@ func (pool *Pool) BeginTx(ctx context.Context, opts pgx.TxOptions) (*Tx, error) return maybeNewTx(tx), err } +// WithinTransaction executes an action within a database transaction. +// The transaction will be committed if the callback returns successfully, or rolled back otherwise. +// +// This is equivalent to the GSQLTransact() method of pool's [gsql.ConnectionHandle] implementation, +// but the callback receives the concrete type [*Tx] instead of a generic [gsql.Handle]. +func (pool *Pool) WithinTransaction(ctx context.Context, action func(*Tx) error) error { + return withinTransactionOfPool(ctx, pool.Pool, action) +} + // PoolConn wraps [*pgxpool.Conn] into a [gsql.Handle]. // // Because this type has [*pgxpool.Conn] as an embedded field, @@ -132,6 +150,15 @@ func (conn *PoolConn) Hijack() *Conn { return maybe(NewConn, conn.Conn.Hijack()) } +// WithinTransaction executes an action within a database transaction. +// The transaction will be committed if the callback returns successfully, or rolled back otherwise. +// +// This is equivalent to the GSQLTransact() method of conn's [gsql.ConnectionHandle] implementation, +// but the callback receives the concrete type [*Tx] instead of a generic [gsql.Handle]. +func (conn *PoolConn) WithinTransaction(ctx context.Context, action func(*Tx) error) error { + return withinTransactionOfPoolConn(ctx, conn.Conn, action) +} + // Tx wraps [pgx.Tx] into a [gsql.Handle]. // // Because this type has [pgx.Tx] as an embedded field, |
