forked from mirror/gorm
use callback to handle transaction
- make transaction have before and after hooks, so plugin can have hack before or after transaction
This commit is contained in:
parent
a70af2a4c0
commit
93f28bc116
25
callbacks.go
25
callbacks.go
|
@ -2,6 +2,7 @@ package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -21,6 +22,7 @@ func initializeCallbacks(db *DB) *callbacks {
|
||||||
"delete": {db: db},
|
"delete": {db: db},
|
||||||
"row": {db: db},
|
"row": {db: db},
|
||||||
"raw": {db: db},
|
"raw": {db: db},
|
||||||
|
"transaction": {db: db},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,6 +74,29 @@ func (cs *callbacks) Raw() *processor {
|
||||||
return cs.processors["raw"]
|
return cs.processors["raw"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cs *callbacks) Transaction() *processor {
|
||||||
|
return cs.processors["transaction"]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) Begin(tx *DB, opt *sql.TxOptions) *DB {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
switch beginner := tx.Statement.ConnPool.(type) {
|
||||||
|
case TxBeginner:
|
||||||
|
tx.Statement.ConnPool, err = beginner.BeginTx(tx.Statement.Context, opt)
|
||||||
|
case ConnPoolBeginner:
|
||||||
|
tx.Statement.ConnPool, err = beginner.BeginTx(tx.Statement.Context, opt)
|
||||||
|
default:
|
||||||
|
err = ErrInvalidTransaction
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
tx.AddError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
func (p *processor) Execute(db *DB) *DB {
|
func (p *processor) Execute(db *DB) *DB {
|
||||||
// call scopes
|
// call scopes
|
||||||
for len(db.Statement.scopes) > 0 {
|
for len(db.Statement.scopes) > 0 {
|
||||||
|
|
|
@ -619,27 +619,13 @@ func (db *DB) Begin(opts ...*sql.TxOptions) *DB {
|
||||||
// clone statement
|
// clone statement
|
||||||
tx = db.getInstance().Session(&Session{Context: db.Statement.Context, NewDB: db.clone == 1})
|
tx = db.getInstance().Session(&Session{Context: db.Statement.Context, NewDB: db.clone == 1})
|
||||||
opt *sql.TxOptions
|
opt *sql.TxOptions
|
||||||
err error
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(opts) > 0 {
|
if len(opts) > 0 {
|
||||||
opt = opts[0]
|
opt = opts[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
switch beginner := tx.Statement.ConnPool.(type) {
|
return tx.callbacks.Transaction().Begin(tx, opt)
|
||||||
case TxBeginner:
|
|
||||||
tx.Statement.ConnPool, err = beginner.BeginTx(tx.Statement.Context, opt)
|
|
||||||
case ConnPoolBeginner:
|
|
||||||
tx.Statement.ConnPool, err = beginner.BeginTx(tx.Statement.Context, opt)
|
|
||||||
default:
|
|
||||||
err = ErrInvalidTransaction
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
tx.AddError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return tx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit commit a transaction
|
// Commit commit a transaction
|
||||||
|
|
Loading…
Reference in New Issue