Use Tx.Status() internally catch failed transactions
This commit is contained in:
@@ -117,7 +117,7 @@ func (tx *Tx) Commit() error {
|
||||
|
||||
// CommitEx commits the transaction with a context.
|
||||
func (tx *Tx) CommitEx(ctx context.Context) error {
|
||||
if !(tx.status == TxStatusInProgress || tx.status == TxStatusInFailure) {
|
||||
if !(tx.Status() == TxStatusInProgress || tx.Status() == TxStatusInFailure) {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func (tx *Tx) Rollback() error {
|
||||
|
||||
// RollbackEx is the context version of Rollback
|
||||
func (tx *Tx) RollbackEx(ctx context.Context) error {
|
||||
if !(tx.status == TxStatusInProgress || tx.status == TxStatusInFailure) {
|
||||
if !(tx.Status() == TxStatusInProgress || tx.Status() == TxStatusInFailure) {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
@@ -179,10 +179,10 @@ func (tx *Tx) Exec(sql string, arguments ...interface{}) (commandTag CommandTag,
|
||||
|
||||
// ExecEx delegates to the underlying *Conn
|
||||
func (tx *Tx) ExecEx(ctx context.Context, sql string, options *QueryExOptions, arguments ...interface{}) (commandTag CommandTag, err error) {
|
||||
if tx.status == TxStatusInFailure {
|
||||
if tx.Status() == TxStatusInFailure {
|
||||
return CommandTag(""), ErrTxInFailure
|
||||
}
|
||||
if tx.status != TxStatusInProgress {
|
||||
if tx.Status() != TxStatusInProgress {
|
||||
return CommandTag(""), ErrTxClosed
|
||||
}
|
||||
|
||||
@@ -196,10 +196,10 @@ func (tx *Tx) Prepare(name, sql string) (*PreparedStatement, error) {
|
||||
|
||||
// PrepareEx delegates to the underlying *Conn
|
||||
func (tx *Tx) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (*PreparedStatement, error) {
|
||||
if tx.status == TxStatusInFailure {
|
||||
if tx.Status() == TxStatusInFailure {
|
||||
return nil, ErrTxInFailure
|
||||
}
|
||||
if tx.status != TxStatusInProgress {
|
||||
if tx.Status() != TxStatusInProgress {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
@@ -213,12 +213,12 @@ func (tx *Tx) Query(sql string, args ...interface{}) (*Rows, error) {
|
||||
|
||||
// QueryEx delegates to the underlying *Conn
|
||||
func (tx *Tx) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (*Rows, error) {
|
||||
if tx.status == TxStatusInFailure {
|
||||
if tx.Status() == TxStatusInFailure {
|
||||
// Because checking for errors can be deferred to the *Rows, build one with the error
|
||||
err := ErrTxInFailure
|
||||
return &Rows{closed: true, err: err}, err
|
||||
}
|
||||
if tx.status != TxStatusInProgress {
|
||||
if tx.Status() != TxStatusInProgress {
|
||||
// Because checking for errors can be deferred to the *Rows, build one with the error
|
||||
err := ErrTxClosed
|
||||
return &Rows{closed: true, err: err}, err
|
||||
@@ -241,7 +241,7 @@ func (tx *Tx) QueryRowEx(ctx context.Context, sql string, options *QueryExOption
|
||||
|
||||
// CopyFrom delegates to the underlying *Conn
|
||||
func (tx *Tx) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int, error) {
|
||||
if tx.status != TxStatusInProgress {
|
||||
if tx.Status() != TxStatusInProgress {
|
||||
return 0, ErrTxClosed
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user