-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
executable file
·192 lines (170 loc) · 5.01 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package sqlxplus
import (
"context"
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
)
type Tx struct {
SqlxTx sqlxer
Log Logger
}
func (that *Tx) GetSqlxTx() *sqlx.Tx {
return that.SqlxTx.(*sqlx.Tx)
}
func (that *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
that.Log.Printf(ctx, query, args...)
rows, err = that.SqlxTx.QueryContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (rows *sqlx.Rows, err error) {
that.Log.Printf(ctx, query, args...)
rows, err = that.SqlxTx.QueryxContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
that.Log.Printf(ctx, query, args...)
return that.SqlxTx.QueryRowxContext(ctx, query, args...)
}
func (that *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
that.Log.Printf(ctx, query, args...)
result, err = that.SqlxTx.ExecContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *Tx) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error) {
that.Log.Println(ctx, query)
stmt, err = that.SqlxTx.PrepareContext(ctx, query)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *Tx) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
that.Log.Printf(context.Background(), query, args...)
rows, err = that.SqlxTx.Query(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *Tx) Queryx(query string, args ...interface{}) (rows *sqlx.Rows, err error) {
that.Log.Printf(context.Background(), query, args...)
rows, err = that.SqlxTx.Queryx(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row {
that.Log.Printf(context.Background(), query, args...)
return that.SqlxTx.QueryRowx(query, args...)
}
func (that *Tx) Exec(query string, args ...interface{}) (result sql.Result, err error) {
that.Log.Printf(context.Background(), query, args...)
result, err = that.SqlxTx.Exec(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *Tx) Prepare(query string) (stmt *sql.Stmt, err error) {
that.Log.Println(context.Background(), query)
stmt, err = that.SqlxTx.Prepare(query)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
type CreateSqlxTranContextFunc func(ctx context.Context, tx *sqlx.Tx) context.Context
type TransactionFunc func(context.Context) error
type ExecTransactionOptions struct {
DB *sqlx.DB
Log Logger
CreateSqlxTranContextFunc CreateSqlxTranContextFunc
}
type ExecTransactionOption func(o *ExecTransactionOptions)
func newExecTransactionOptions(opts ...ExecTransactionOption) ExecTransactionOptions {
var o ExecTransactionOptions
for _, opt := range opts {
opt(&o)
}
if o.Log == nil {
o.Log = &StdLogger{}
}
if o.CreateSqlxTranContextFunc == nil {
o.CreateSqlxTranContextFunc = func(ctx context.Context, tx *sqlx.Tx) context.Context {
return NewSqlxTranContext(ctx, &Tx{SqlxTx: tx, Log: o.Log})
}
}
return o
}
func WithExecTransactionDB(db *sqlx.DB) ExecTransactionOption {
return func(o *ExecTransactionOptions) {
o.DB = db
}
}
func WithExecTransactionLogger(log Logger) ExecTransactionOption {
return func(o *ExecTransactionOptions) {
o.Log = log
}
}
func WithExecTransactionCreateSqlxTranContextFunc(f CreateSqlxTranContextFunc) ExecTransactionOption {
return func(o *ExecTransactionOptions) {
o.CreateSqlxTranContextFunc = f
}
}
var ErrExecTransactionOptionDBIsNil = errors.New("db in options is nil")
func ExecTransaction(ctx context.Context, f TransactionFunc, opts ...ExecTransactionOption) (err error) {
op := newExecTransactionOptions(opts...)
var tran *sqlx.Tx
// 判断上下文是否存在事务,如果不存在事务,则开启事务
tranCreateFlag := false
if !CheckSqlxTranContextExist(ctx) {
// 判断上下文是否存在数据库,如果不存在数据库,则使用可选参数中的数据库
var conn *DBConnect
if conn, err = FromSqlxContext(ctx); err != nil {
if errors.Is(err, ErrContextNotFoundSqlx) {
err = nil
if op.DB == nil {
err = ErrExecTransactionOptionDBIsNil
return
}
if tran, err = op.DB.Beginx(); err != nil {
return
}
} else {
return
}
} else {
if tran, err = conn.DB().GetSqlxDB().Beginx(); err != nil {
return
}
}
defer func() {
if reErr := recover(); reErr != nil {
err = reErr.(error)
tran.Rollback()
} else if err != nil {
tran.Rollback()
}
}()
ctx = op.CreateSqlxTranContextFunc(ctx, tran)
tranCreateFlag = true
}
if err = f(ctx); err != nil {
return
}
if tranCreateFlag {
err = tran.Commit()
}
return
}