Rename dbSavepoint to dbSimulatedNestedTx

https://github.com/jackc/pgx/issues/1161
This commit is contained in:
Jack Christensen 2022-03-05 08:41:02 -06:00
parent a86ece025c
commit 3ce50c079e

32
tx.go
View File

@ -192,7 +192,7 @@ func (tx *dbTx) Begin(ctx context.Context) (Tx, error) {
return nil, err return nil, err
} }
return &dbSavepoint{tx: tx, savepointNum: tx.savepointNum}, nil return &dbSimulatedNestedTx{tx: tx, savepointNum: tx.savepointNum}, nil
} }
func (tx *dbTx) BeginFunc(ctx context.Context, f func(Tx) error) (err error) { func (tx *dbTx) BeginFunc(ctx context.Context, f func(Tx) error) (err error) {
@ -329,15 +329,15 @@ func (tx *dbTx) Conn() *Conn {
return tx.conn return tx.conn
} }
// dbSavepoint represents a nested transaction implemented by a savepoint. // dbSimulatedNestedTx represents a simulated nested transaction implemented by a savepoint.
type dbSavepoint struct { type dbSimulatedNestedTx struct {
tx Tx tx Tx
savepointNum int64 savepointNum int64
closed bool closed bool
} }
// Begin starts a pseudo nested transaction implemented with a savepoint. // Begin starts a pseudo nested transaction implemented with a savepoint.
func (sp *dbSavepoint) Begin(ctx context.Context) (Tx, error) { func (sp *dbSimulatedNestedTx) Begin(ctx context.Context) (Tx, error) {
if sp.closed { if sp.closed {
return nil, ErrTxClosed return nil, ErrTxClosed
} }
@ -345,7 +345,7 @@ func (sp *dbSavepoint) Begin(ctx context.Context) (Tx, error) {
return sp.tx.Begin(ctx) return sp.tx.Begin(ctx)
} }
func (sp *dbSavepoint) BeginFunc(ctx context.Context, f func(Tx) error) (err error) { func (sp *dbSimulatedNestedTx) BeginFunc(ctx context.Context, f func(Tx) error) (err error) {
if sp.closed { if sp.closed {
return ErrTxClosed return ErrTxClosed
} }
@ -354,7 +354,7 @@ func (sp *dbSavepoint) BeginFunc(ctx context.Context, f func(Tx) error) (err err
} }
// Commit releases the savepoint essentially committing the pseudo nested transaction. // Commit releases the savepoint essentially committing the pseudo nested transaction.
func (sp *dbSavepoint) Commit(ctx context.Context) error { func (sp *dbSimulatedNestedTx) Commit(ctx context.Context) error {
if sp.closed { if sp.closed {
return ErrTxClosed return ErrTxClosed
} }
@ -367,7 +367,7 @@ func (sp *dbSavepoint) Commit(ctx context.Context) error {
// Rollback rolls back to the savepoint essentially rolling back the pseudo nested transaction. Rollback will return // Rollback rolls back to the savepoint essentially rolling back the pseudo nested transaction. Rollback will return
// ErrTxClosed if the dbSavepoint is already closed, but is otherwise safe to call multiple times. Hence, a defer sp.Rollback() // ErrTxClosed if the dbSavepoint is already closed, but is otherwise safe to call multiple times. Hence, a defer sp.Rollback()
// is safe even if sp.Commit() will be called first in a non-error condition. // is safe even if sp.Commit() will be called first in a non-error condition.
func (sp *dbSavepoint) Rollback(ctx context.Context) error { func (sp *dbSimulatedNestedTx) Rollback(ctx context.Context) error {
if sp.closed { if sp.closed {
return ErrTxClosed return ErrTxClosed
} }
@ -378,7 +378,7 @@ func (sp *dbSavepoint) Rollback(ctx context.Context) error {
} }
// Exec delegates to the underlying Tx // Exec delegates to the underlying Tx
func (sp *dbSavepoint) Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) { func (sp *dbSimulatedNestedTx) Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) {
if sp.closed { if sp.closed {
return nil, ErrTxClosed return nil, ErrTxClosed
} }
@ -387,7 +387,7 @@ func (sp *dbSavepoint) Exec(ctx context.Context, sql string, arguments ...interf
} }
// Prepare delegates to the underlying Tx // Prepare delegates to the underlying Tx
func (sp *dbSavepoint) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) { func (sp *dbSimulatedNestedTx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
if sp.closed { if sp.closed {
return nil, ErrTxClosed return nil, ErrTxClosed
} }
@ -396,7 +396,7 @@ func (sp *dbSavepoint) Prepare(ctx context.Context, name, sql string) (*pgconn.S
} }
// Query delegates to the underlying Tx // Query delegates to the underlying Tx
func (sp *dbSavepoint) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) { func (sp *dbSimulatedNestedTx) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
if sp.closed { if sp.closed {
// Because checking for errors can be deferred to the *Rows, build one with the error // Because checking for errors can be deferred to the *Rows, build one with the error
err := ErrTxClosed err := ErrTxClosed
@ -407,13 +407,13 @@ func (sp *dbSavepoint) Query(ctx context.Context, sql string, args ...interface{
} }
// QueryRow delegates to the underlying Tx // QueryRow delegates to the underlying Tx
func (sp *dbSavepoint) QueryRow(ctx context.Context, sql string, args ...interface{}) Row { func (sp *dbSimulatedNestedTx) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
rows, _ := sp.Query(ctx, sql, args...) rows, _ := sp.Query(ctx, sql, args...)
return (*connRow)(rows.(*connRows)) return (*connRow)(rows.(*connRows))
} }
// QueryFunc delegates to the underlying Tx. // QueryFunc delegates to the underlying Tx.
func (sp *dbSavepoint) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) { func (sp *dbSimulatedNestedTx) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) {
if sp.closed { if sp.closed {
return nil, ErrTxClosed return nil, ErrTxClosed
} }
@ -422,7 +422,7 @@ func (sp *dbSavepoint) QueryFunc(ctx context.Context, sql string, args []interfa
} }
// CopyFrom delegates to the underlying *Conn // CopyFrom delegates to the underlying *Conn
func (sp *dbSavepoint) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error) { func (sp *dbSimulatedNestedTx) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error) {
if sp.closed { if sp.closed {
return 0, ErrTxClosed return 0, ErrTxClosed
} }
@ -431,7 +431,7 @@ func (sp *dbSavepoint) CopyFrom(ctx context.Context, tableName Identifier, colum
} }
// SendBatch delegates to the underlying *Conn // SendBatch delegates to the underlying *Conn
func (sp *dbSavepoint) SendBatch(ctx context.Context, b *Batch) BatchResults { func (sp *dbSimulatedNestedTx) SendBatch(ctx context.Context, b *Batch) BatchResults {
if sp.closed { if sp.closed {
return &batchResults{err: ErrTxClosed} return &batchResults{err: ErrTxClosed}
} }
@ -439,10 +439,10 @@ func (sp *dbSavepoint) SendBatch(ctx context.Context, b *Batch) BatchResults {
return sp.tx.SendBatch(ctx, b) return sp.tx.SendBatch(ctx, b)
} }
func (sp *dbSavepoint) LargeObjects() LargeObjects { func (sp *dbSimulatedNestedTx) LargeObjects() LargeObjects {
return LargeObjects{tx: sp} return LargeObjects{tx: sp}
} }
func (sp *dbSavepoint) Conn() *Conn { func (sp *dbSimulatedNestedTx) Conn() *Conn {
return sp.tx.Conn() return sp.tx.Conn()
} }