@@ -55,6 +55,19 @@ func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error
55
55
})
56
56
}
57
57
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
+
58
71
// Transaction executes txFunc within a database transaction that is passed in to txFunc via the context.
59
72
// Use db.Conn(ctx) to get the transaction connection within txFunc.
60
73
// 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
68
81
})
69
82
}
70
83
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.
73
101
//
74
102
// 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 {
77
105
return txFunc (ctx )
78
106
}
79
107
return Transaction (ctx , txFunc )
80
108
}
81
109
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
+
82
121
// SerializedTransaction executes txFunc "serially" within a database transaction that is passed in to txFunc via the context.
83
122
// Use db.Conn(ctx) to get the transaction connection within txFunc.
84
123
// Transaction returns all errors from txFunc or transaction commit errors happening after txFunc.
0 commit comments