2
0

Add pgx.Tx interface and pseudo nested transaction support

This complicates the idea of a persistent transaction status and error
so that concept was removed.
This commit is contained in:
Jack Christensen
2019-08-17 17:22:14 -05:00
parent 64b4414efc
commit 99e5461522
7 changed files with 267 additions and 143 deletions
+2 -2
View File
@@ -66,11 +66,11 @@ func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNam
return c.Conn().CopyFrom(ctx, tableName, columnNames, rowSrc)
}
func (c *Conn) Begin(ctx context.Context) (*pgx.Tx, error) {
func (c *Conn) Begin(ctx context.Context) (pgx.Tx, error) {
return c.Conn().Begin(ctx)
}
func (c *Conn) BeginEx(ctx context.Context, txOptions pgx.TxOptions) (*pgx.Tx, error) {
func (c *Conn) BeginEx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
return c.Conn().BeginEx(ctx, txOptions)
}
+2 -2
View File
@@ -352,10 +352,10 @@ func (p *Pool) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
return &poolBatchResults{br: br, c: c}
}
func (p *Pool) Begin(ctx context.Context) (*Tx, error) {
func (p *Pool) Begin(ctx context.Context) (pgx.Tx, error) {
return p.BeginEx(ctx, pgx.TxOptions{})
}
func (p *Pool) BeginEx(ctx context.Context, txOptions pgx.TxOptions) (*Tx, error) {
func (p *Pool) BeginEx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
c, err := p.Acquire(ctx)
if err != nil {
return nil, err
+26 -18
View File
@@ -8,10 +8,14 @@ import (
)
type Tx struct {
t *pgx.Tx
t pgx.Tx
c *Conn
}
func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error) {
return tx.t.Begin(ctx)
}
func (tx *Tx) Commit(ctx context.Context) error {
err := tx.t.Commit(ctx)
if tx.c != nil {
@@ -30,26 +34,30 @@ func (tx *Tx) Rollback(ctx context.Context) error {
return err
}
func (tx *Tx) Err() error {
return tx.t.Err()
}
func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
return tx.c.Exec(ctx, sql, arguments...)
}
func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
return tx.c.Query(ctx, sql, args...)
}
func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
return tx.c.QueryRow(ctx, sql, args...)
func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
return tx.t.CopyFrom(ctx, tableName, columnNames, rowSrc)
}
func (tx *Tx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
return tx.c.SendBatch(ctx, b)
return tx.t.SendBatch(ctx, b)
}
func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
return tx.c.CopyFrom(ctx, tableName, columnNames, rowSrc)
func (tx *Tx) LargeObjects() pgx.LargeObjects {
return tx.t.LargeObjects()
}
func (tx *Tx) Prepare(ctx context.Context, name, sql string) (*pgx.PreparedStatement, error) {
return tx.t.Prepare(ctx, name, sql)
}
func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
return tx.t.Exec(ctx, sql, arguments...)
}
func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
return tx.t.Query(ctx, sql, args...)
}
func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
return tx.t.QueryRow(ctx, sql, args...)
}