Skip to content

Commit e867f71

Browse files
committed
added TransactionResult, IsolatedTransactionResult, OptionalTransactionResult
1 parent 4a5bac0 commit e867f71

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

db/transaction.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error
5555
})
5656
}
5757

58+
// IsolatedTransactionResult executes txFunc within a database transaction that is passed in to txFunc as tx Connection.
59+
// IsolatedTransactionResult returns all errors from txFunc or transaction commit errors happening after txFunc.
60+
// If parentConn is already a transaction, a brand new transaction will begin on the parent's connection.
61+
// Errors and panics from txFunc will rollback the transaction.
62+
// Recovered panics are re-paniced and rollback errors after a panic are logged with ErrLogger.
63+
func IsolatedTransactionResult[T any](ctx context.Context, txFunc func(context.Context) (T, error)) (result T, err error) {
64+
err = IsolatedTransaction(ctx, func(ctx context.Context) error {
65+
result, err = txFunc(ctx)
66+
return err
67+
})
68+
return result, err
69+
}
70+
5871
// Transaction executes txFunc within a database transaction that is passed in to txFunc via the context.
5972
// Use db.Conn(ctx) to get the transaction connection within txFunc.
6073
// Transaction returns all errors from txFunc or transaction commit errors happening after txFunc.
@@ -68,17 +81,43 @@ func Transaction(ctx context.Context, txFunc func(context.Context) error) error
6881
})
6982
}
7083

71-
// OptionalTransaction executes txFunc within a database transaction if doTransaction is true.
72-
// If doTransaction is false, then txFunc is executed without a transaction.
84+
// TransactionResult executes txFunc within a database transaction and returns the result of txFunc.
85+
// Use db.Conn(ctx) to get the transaction connection within txFunc.
86+
// Transaction returns all errors from txFunc or transaction commit errors happening after txFunc.
87+
// If parentConn is already a transaction, then it is passed through to txFunc unchanged as tx sqldb.Connection
88+
// and no parentConn.Begin, Commit, or Rollback calls will occour within this TransactionResult call.
89+
// Errors and panics from txFunc will rollback the transaction if parentConn was not already a transaction.
90+
// Recovered panics are re-paniced and rollback errors after a panic are logged with sqldb.ErrLogger.
91+
func TransactionResult[T any](ctx context.Context, txFunc func(context.Context) (T, error)) (result T, err error) {
92+
err = Transaction(ctx, func(ctx context.Context) error {
93+
result, err = txFunc(ctx)
94+
return err
95+
})
96+
return result, err
97+
}
98+
99+
// OptionalTransaction executes txFunc within a database transaction if useTransaction is true.
100+
// If useTransaction is false, then txFunc is executed without a transaction.
73101
//
74102
// See [Transaction] for more details.
75-
func OptionalTransaction(ctx context.Context, doTransaction bool, txFunc func(context.Context) error) error {
76-
if !doTransaction {
103+
func OptionalTransaction(ctx context.Context, useTransaction bool, txFunc func(context.Context) error) error {
104+
if !useTransaction {
77105
return txFunc(ctx)
78106
}
79107
return Transaction(ctx, txFunc)
80108
}
81109

110+
// OptionalTransactionResult executes txFunc within a database transaction if useTransaction is true.
111+
// If useTransaction is false, then txFunc is executed without a transaction.
112+
//
113+
// See [TransactionResult] for more details.
114+
func OptionalTransactionResult[T any](ctx context.Context, useTransaction bool, txFunc func(context.Context) (T, error)) (result T, err error) {
115+
if !useTransaction {
116+
return txFunc(ctx)
117+
}
118+
return TransactionResult(ctx, txFunc)
119+
}
120+
82121
// SerializedTransaction executes txFunc "serially" within a database transaction that is passed in to txFunc via the context.
83122
// Use db.Conn(ctx) to get the transaction connection within txFunc.
84123
// Transaction returns all errors from txFunc or transaction commit errors happening after txFunc.

0 commit comments

Comments
 (0)