File tree 2 files changed +32
-21
lines changed
2 files changed +32
-21
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package gorm
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
"errors"
6
7
"fmt"
7
8
"reflect"
@@ -15,12 +16,13 @@ import (
15
16
func initializeCallbacks (db * DB ) * callbacks {
16
17
return & callbacks {
17
18
processors : map [string ]* processor {
18
- "create" : {db : db },
19
- "query" : {db : db },
20
- "update" : {db : db },
21
- "delete" : {db : db },
22
- "row" : {db : db },
23
- "raw" : {db : db },
19
+ "create" : {db : db },
20
+ "query" : {db : db },
21
+ "update" : {db : db },
22
+ "delete" : {db : db },
23
+ "row" : {db : db },
24
+ "raw" : {db : db },
25
+ "transaction" : {db : db },
24
26
},
25
27
}
26
28
}
@@ -72,6 +74,29 @@ func (cs *callbacks) Raw() *processor {
72
74
return cs .processors ["raw" ]
73
75
}
74
76
77
+ func (cs * callbacks ) Transaction () * processor {
78
+ return cs .processors ["transaction" ]
79
+ }
80
+
81
+ func (p * processor ) Begin (tx * DB , opt * sql.TxOptions ) * DB {
82
+ var err error
83
+
84
+ switch beginner := tx .Statement .ConnPool .(type ) {
85
+ case TxBeginner :
86
+ tx .Statement .ConnPool , err = beginner .BeginTx (tx .Statement .Context , opt )
87
+ case ConnPoolBeginner :
88
+ tx .Statement .ConnPool , err = beginner .BeginTx (tx .Statement .Context , opt )
89
+ default :
90
+ err = ErrInvalidTransaction
91
+ }
92
+
93
+ if err != nil {
94
+ _ = tx .AddError (err )
95
+ }
96
+
97
+ return tx
98
+ }
99
+
75
100
func (p * processor ) Execute (db * DB ) * DB {
76
101
// call scopes
77
102
for len (db .Statement .scopes ) > 0 {
Original file line number Diff line number Diff line change @@ -619,27 +619,13 @@ func (db *DB) Begin(opts ...*sql.TxOptions) *DB {
619
619
// clone statement
620
620
tx = db .getInstance ().Session (& Session {Context : db .Statement .Context , NewDB : db .clone == 1 })
621
621
opt * sql.TxOptions
622
- err error
623
622
)
624
623
625
624
if len (opts ) > 0 {
626
625
opt = opts [0 ]
627
626
}
628
627
629
- switch beginner := tx .Statement .ConnPool .(type ) {
630
- case TxBeginner :
631
- tx .Statement .ConnPool , err = beginner .BeginTx (tx .Statement .Context , opt )
632
- case ConnPoolBeginner :
633
- tx .Statement .ConnPool , err = beginner .BeginTx (tx .Statement .Context , opt )
634
- default :
635
- err = ErrInvalidTransaction
636
- }
637
-
638
- if err != nil {
639
- tx .AddError (err )
640
- }
641
-
642
- return tx
628
+ return tx .callbacks .Transaction ().Begin (tx , opt )
643
629
}
644
630
645
631
// Commit commit a transaction
You can’t perform that action at this time.
0 commit comments