Revert "use callback to handle transaction"

This reverts commit 93f28bc116.
This commit is contained in:
Jinzhu 2022-07-07 15:06:48 +08:00
parent fe01e1b9f4
commit 9fd73ae4f1
2 changed files with 21 additions and 32 deletions

View File

@ -2,7 +2,6 @@ package gorm
import ( import (
"context" "context"
"database/sql"
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
@ -16,13 +15,12 @@ import (
func initializeCallbacks(db *DB) *callbacks { func initializeCallbacks(db *DB) *callbacks {
return &callbacks{ return &callbacks{
processors: map[string]*processor{ processors: map[string]*processor{
"create": {db: db}, "create": {db: db},
"query": {db: db}, "query": {db: db},
"update": {db: db}, "update": {db: db},
"delete": {db: db}, "delete": {db: db},
"row": {db: db}, "row": {db: db},
"raw": {db: db}, "raw": {db: db},
"transaction": {db: db},
}, },
} }
} }
@ -74,29 +72,6 @@ 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 {

View File

@ -619,13 +619,27 @@ 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]
} }
return tx.callbacks.Transaction().Begin(tx, opt) 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
} }
// Commit commit a transaction // Commit commit a transaction