Source File
tx.go
Belonging Package
github.com/jackc/pgx/v4
package pgx
import (
)
type TxIsoLevel string
const (
Serializable = TxIsoLevel("serializable")
RepeatableRead = TxIsoLevel("repeatable read")
ReadCommitted = TxIsoLevel("read committed")
ReadUncommitted = TxIsoLevel("read uncommitted")
)
type TxAccessMode string
const (
ReadWrite = TxAccessMode("read write")
ReadOnly = TxAccessMode("read only")
)
type TxDeferrableMode string
const (
Deferrable = TxDeferrableMode("deferrable")
NotDeferrable = TxDeferrableMode("not deferrable")
)
type TxOptions struct {
IsoLevel TxIsoLevel
AccessMode TxAccessMode
DeferrableMode TxDeferrableMode
}
func ( TxOptions) () string {
:= &bytes.Buffer{}
.WriteString("begin")
if .IsoLevel != "" {
fmt.Fprintf(, " isolation level %s", .IsoLevel)
}
if .AccessMode != "" {
fmt.Fprintf(, " %s", .AccessMode)
}
if .DeferrableMode != "" {
fmt.Fprintf(, " %s", .DeferrableMode)
}
return .String()
}
var ErrTxClosed = errors.New("tx is closed")
var ErrTxCommitRollback = errors.New("commit unexpectedly resulted in rollback")
func ( *Conn) ( context.Context, TxOptions, func(Tx) error) ( error) {
var Tx
, = .BeginTx(, )
if != nil {
return
}
defer func() {
:= .Rollback()
if !( == nil || errors.Is(, ErrTxClosed)) {
=
}
}()
:= ()
if != nil {
_ = .Rollback() // ignore rollback error as there is already an error to return
return
}
return .Commit()
}
Rollback(ctx context.Context) error
CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error)
SendBatch(ctx context.Context, b *Batch) BatchResults
LargeObjects() LargeObjects
Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)
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
QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)
Conn() *Conn
}
func ( *dbTx) ( context.Context) (Tx, error) {
if .closed {
return nil, ErrTxClosed
}
.savepointNum++
, := .conn.Exec(, "savepoint sp_"+strconv.FormatInt(.savepointNum, 10))
if != nil {
return nil,
}
return &dbSavepoint{tx: , savepointNum: .savepointNum}, nil
}
func ( *dbTx) ( context.Context, func(Tx) error) ( error) {
if .closed {
return ErrTxClosed
}
var Tx
, = .Begin()
if != nil {
return
}
defer func() {
:= .Rollback()
if !( == nil || errors.Is(, ErrTxClosed)) {
=
}
}()
:= ()
if != nil {
_ = .Rollback() // ignore rollback error as there is already an error to return
return
}
return .Commit()
}
func ( *dbTx) ( context.Context, string, []interface{}, []interface{}, func(QueryFuncRow) error) (pgconn.CommandTag, error) {
if .closed {
return nil, ErrTxClosed
}
return .conn.QueryFunc(, , , , )
}
func ( *dbTx) ( context.Context, Identifier, []string, CopyFromSource) (int64, error) {
if .closed {
return 0, ErrTxClosed
}
return .conn.CopyFrom(, , , )
}
func ( *dbTx) ( context.Context, *Batch) BatchResults {
if .closed {
return &batchResults{err: ErrTxClosed}
}
return .conn.SendBatch(, )
}
func ( *dbTx) () LargeObjects {
return LargeObjects{tx: }
}
func ( *dbTx) () *Conn {
return .conn
}
type dbSavepoint struct {
tx Tx
savepointNum int64
closed bool
}
func ( *dbSavepoint) ( context.Context) (Tx, error) {
if .closed {
return nil, ErrTxClosed
}
return .tx.Begin()
}
func ( *dbSavepoint) ( context.Context, func(Tx) error) ( error) {
if .closed {
return ErrTxClosed
}
return .tx.BeginFunc(, )
}
func ( *dbSavepoint) ( context.Context) error {
if .closed {
return ErrTxClosed
}
, := .Exec(, "release savepoint sp_"+strconv.FormatInt(.savepointNum, 10))
.closed = true
return
}
func ( *dbSavepoint) ( context.Context) error {
if .closed {
return ErrTxClosed
}
, := .Exec(, "rollback to savepoint sp_"+strconv.FormatInt(.savepointNum, 10))
.closed = true
return
}
func ( *dbSavepoint) ( context.Context, string, ...interface{}) ( pgconn.CommandTag, error) {
if .closed {
return nil, ErrTxClosed
}
return .tx.Exec(, , ...)
}
func ( *dbSavepoint) ( context.Context, , string) (*pgconn.StatementDescription, error) {
if .closed {
return nil, ErrTxClosed
}
return .tx.Prepare(, , )
}
func ( *dbSavepoint) ( context.Context, string, []interface{}, []interface{}, func(QueryFuncRow) error) (pgconn.CommandTag, error) {
if .closed {
return nil, ErrTxClosed
}
return .tx.QueryFunc(, , , , )
}
func ( *dbSavepoint) ( context.Context, Identifier, []string, CopyFromSource) (int64, error) {
if .closed {
return 0, ErrTxClosed
}
return .tx.CopyFrom(, , , )
}
func ( *dbSavepoint) ( context.Context, *Batch) BatchResults {
if .closed {
return &batchResults{err: ErrTxClosed}
}
return .tx.SendBatch(, )
}
func ( *dbSavepoint) () LargeObjects {
return LargeObjects{tx: }
}
func ( *dbSavepoint) () *Conn {
return .tx.Conn()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |