Skip to content

Commit 5fec197

Browse files
committed
fix: fix wrong execution order
1 parent 4382510 commit 5fec197

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

multi_hook.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func (h *multiHook) BeforeConnect(ctx context.Context, err error) (context.Conte
2222
}
2323

2424
func (h *multiHook) AfterConnect(ctx context.Context, dc driver.Conn, err error) (context.Context, driver.Conn, error) {
25-
for _, hook := range h.hooks {
26-
ctx, dc, err = hook.AfterConnect(ctx, dc, err)
25+
for i := len(h.hooks) - 1; i >= 0; i-- {
26+
ctx, dc, err = h.hooks[i].AfterConnect(ctx, dc, err)
2727
}
2828

2929
return ctx, dc, err
@@ -38,8 +38,8 @@ func (h *multiHook) BeforeExecContext(ctx context.Context, query string, args []
3838
}
3939

4040
func (h *multiHook) AfterExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error) {
41-
for _, hook := range h.hooks {
42-
ctx, r, err = hook.AfterExecContext(ctx, query, args, r, err)
41+
for i := len(h.hooks) - 1; i >= 0; i-- {
42+
ctx, r, err = h.hooks[i].AfterExecContext(ctx, query, args, r, err)
4343
}
4444

4545
return ctx, r, err
@@ -54,8 +54,8 @@ func (h *multiHook) BeforeBeginTx(ctx context.Context, opts driver.TxOptions, er
5454
}
5555

5656
func (h *multiHook) AfterBeginTx(ctx context.Context, opts driver.TxOptions, dd driver.Tx, err error) (context.Context, driver.Tx, error) {
57-
for _, hook := range h.hooks {
58-
ctx, dd, err = hook.AfterBeginTx(ctx, opts, dd, err)
57+
for i := len(h.hooks) - 1; i >= 0; i-- {
58+
ctx, dd, err = h.hooks[i].AfterBeginTx(ctx, opts, dd, err)
5959
}
6060

6161
return ctx, dd, err
@@ -70,9 +70,8 @@ func (h *multiHook) BeforeQueryContext(ctx context.Context, query string, args [
7070
}
7171

7272
func (h *multiHook) AfterQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error) {
73-
for _, hook := range h.hooks {
74-
ctx, rows, err = hook.AfterQueryContext(ctx, query, args, rows, err)
75-
73+
for i := len(h.hooks) - 1; i >= 0; i-- {
74+
ctx, rows, err = h.hooks[i].AfterQueryContext(ctx, query, args, rows, err)
7675
}
7776

7877
return ctx, rows, err
@@ -87,8 +86,8 @@ func (h *multiHook) BeforePrepareContext(ctx context.Context, query string, err
8786
}
8887

8988
func (h *multiHook) AfterPrepareContext(ctx context.Context, query string, s driver.Stmt, err error) (context.Context, driver.Stmt, error) {
90-
for _, hook := range h.hooks {
91-
ctx, s, err = hook.AfterPrepareContext(ctx, query, s, err)
89+
for i := len(h.hooks) - 1; i >= 0; i-- {
90+
ctx, s, err = h.hooks[i].AfterPrepareContext(ctx, query, s, err)
9291
}
9392

9493
return ctx, s, err
@@ -103,8 +102,8 @@ func (h *multiHook) BeforeCommit(ctx context.Context, err error) (context.Contex
103102
}
104103

105104
func (h *multiHook) AfterCommit(ctx context.Context, err error) (context.Context, error) {
106-
for _, hook := range h.hooks {
107-
ctx, err = hook.AfterCommit(ctx, err)
105+
for i := len(h.hooks) - 1; i >= 0; i-- {
106+
ctx, err = h.hooks[i].AfterCommit(ctx, err)
108107
}
109108

110109
return ctx, err
@@ -119,8 +118,8 @@ func (h *multiHook) BeforeRollback(ctx context.Context, err error) (context.Cont
119118
}
120119

121120
func (h *multiHook) AfterRollback(ctx context.Context, err error) (context.Context, error) {
122-
for _, hook := range h.hooks {
123-
ctx, err = hook.AfterRollback(ctx, err)
121+
for i := len(h.hooks) - 1; i >= 0; i-- {
122+
ctx, err = h.hooks[i].AfterRollback(ctx, err)
124123
}
125124

126125
return ctx, err
@@ -135,8 +134,8 @@ func (h *multiHook) BeforeStmtQueryContext(ctx context.Context, query string, ar
135134
}
136135

137136
func (h *multiHook) AfterStmtQueryContext(ctx context.Context, query string, args []driver.NamedValue, rows driver.Rows, err error) (context.Context, driver.Rows, error) {
138-
for _, hook := range h.hooks {
139-
ctx, rows, err = hook.AfterStmtQueryContext(ctx, query, args, rows, err)
137+
for i := len(h.hooks) - 1; i >= 0; i-- {
138+
ctx, rows, err = h.hooks[i].AfterStmtQueryContext(ctx, query, args, rows, err)
140139
}
141140

142141
return ctx, rows, err
@@ -151,8 +150,8 @@ func (h *multiHook) BeforeStmtExecContext(ctx context.Context, query string, arg
151150
}
152151

153152
func (h *multiHook) AfterStmtExecContext(ctx context.Context, query string, args []driver.NamedValue, r driver.Result, err error) (context.Context, driver.Result, error) {
154-
for _, hook := range h.hooks {
155-
ctx, r, err = hook.AfterStmtExecContext(ctx, query, args, r, err)
153+
for i := len(h.hooks) - 1; i >= 0; i-- {
154+
ctx, r, err = h.hooks[i].AfterStmtExecContext(ctx, query, args, r, err)
156155
}
157156

158157
return ctx, r, err

0 commit comments

Comments
 (0)