Skip to content
This repository was archived by the owner on Jul 14, 2022. It is now read-only.

Commit e4d8b88

Browse files
committed
Add after commit feature
1 parent 97eab81 commit e4d8b88

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

orm/db.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type DBTx struct {
178178
err error
179179
rowsAffected int64
180180
wrappers []database.Wrapper
181+
afterCommit func(err error)
181182
}
182183

183184
func (store *DBStore) BeginTx() (*DBTx, error) {
@@ -202,7 +203,9 @@ func (tx *DBTx) Close() error {
202203
if tx.err != nil {
203204
return tx.tx.Rollback()
204205
}
205-
return tx.tx.Commit()
206+
err := tx.tx.Commit()
207+
tx.afterCommit(err)
208+
return err
206209
}
207210

208211
func (tx *DBTx) Query(sql string, args ...interface{}) (*sql.Rows, error) {
@@ -279,6 +282,10 @@ func (tx *DBTx) SetError(err error) {
279282
tx.err = err
280283
}
281284

285+
func (tx *DBTx) AfterCommit(afterCommit func(err error)) {
286+
tx.afterCommit = afterCommit
287+
}
288+
282289
func TransactFunc(db *DBStore, txFunc func(*DBTx) error) (err error) {
283290
tx, err := db.BeginTx()
284291
if err != nil {
@@ -338,3 +345,14 @@ type TransactorWithContext interface {
338345
func TransactContext(ctx context.Context, db *DBStore, t TransactorWithContext) error {
339346
return TransactFuncContext(ctx, db, t.TransactContext)
340347
}
348+
349+
func BeginTx(ctx context.Context, db *sql.DB) (*DBTx, error) {
350+
tx, err := db.BeginTx(ctx, nil)
351+
if err != nil {
352+
return nil, err
353+
}
354+
355+
return &DBTx{
356+
tx: tx,
357+
}, nil
358+
}

0 commit comments

Comments
 (0)