add hooks for Commit and Rollback (#1733)
add hooks for Commit and Rollback Related issue: https://gitea.com/xorm/xorm/issues/1732 Co-authored-by: MURAOKA Taro <koron.kaoriya@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1733 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
677c0d7411
commit
14a98ca9d2
31
core/tx.go
31
core/tx.go
|
@ -19,6 +19,7 @@ var (
|
|||
type Tx struct {
|
||||
*sql.Tx
|
||||
db *DB
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
||||
|
@ -32,13 +33,41 @@ func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
|||
if err := db.afterProcess(hookCtx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Tx{tx, db}, nil
|
||||
return &Tx{tx, db, ctx}, nil
|
||||
}
|
||||
|
||||
func (db *DB) Begin() (*Tx, error) {
|
||||
return db.BeginTx(context.Background(), nil)
|
||||
}
|
||||
|
||||
func (tx *Tx) Commit() error {
|
||||
hookCtx := contexts.NewContextHook(tx.ctx, "COMMIT", nil)
|
||||
ctx, err := tx.db.beforeProcess(hookCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Tx.Commit()
|
||||
hookCtx.End(ctx, nil, err)
|
||||
if err := tx.db.afterProcess(hookCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *Tx) Rollback() error {
|
||||
hookCtx := contexts.NewContextHook(tx.ctx, "ROLLBACK", nil)
|
||||
ctx, err := tx.db.beforeProcess(hookCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Tx.Rollback()
|
||||
hookCtx.End(ctx, nil, err)
|
||||
if err := tx.db.afterProcess(hookCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
|
||||
names := make(map[string]int)
|
||||
var i int
|
||||
|
|
Loading…
Reference in New Issue