-
Notifications
You must be signed in to change notification settings - Fork 15
/
connection.go
288 lines (220 loc) · 7.74 KB
/
connection.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package sqldblogger
import (
"context"
"database/sql/driver"
"time"
)
// connection is a database connection wrapper which implements following interfaces:
// - driver.Conn
// - driver.ConnBeginTx
// - driver.ConnPrepareContext
// - driver.Pinger
// - driver.Execer
// - driver.ExecerContext
// - driver.Queryer
// - driver.QueryerContext
// - driver.SessionResetter
// - driver.NamedValueChecker
type connection struct {
driver.Conn
id string
logger *logger
}
// Begin implements driver.Conn
func (c *connection) Begin() (driver.Tx, error) {
lvl, start, id := LevelDebug, time.Now(), c.logger.opt.uidGenerator.UniqueID()
logs := append(c.logData(), c.logger.withUID(c.logger.opt.txIDFieldname, id))
connTx, err := c.Conn.Begin() // nolint // disable static check on deprecated driver method
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "Begin", start, err, logs...)
return c.transaction(connTx, err, id)
}
// Prepare implements driver.Conn
func (c *connection) Prepare(query string) (driver.Stmt, error) {
lvl, start, id := c.logger.opt.preparerLevel, time.Now(), c.logger.opt.uidGenerator.UniqueID()
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withUID(c.logger.opt.stmtIDFieldname, id))
driverStmt, err := c.Conn.Prepare(query)
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "Prepare", start, err, logs...)
return c.statement(driverStmt, err, id, query)
}
// Prepare implements driver.Conn
func (c *connection) Close() error {
lvl, start := LevelDebug, time.Now()
err := c.Conn.Close()
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "Close", start, err, c.logData()...)
return err
}
// BeginTx implements driver.ConnBeginTx
func (c *connection) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
drvTx, ok := c.Conn.(driver.ConnBeginTx)
if !ok {
return nil, driver.ErrSkip
}
lvl, start, id := LevelDebug, time.Now(), c.logger.opt.uidGenerator.UniqueID()
logs := append(c.logData(), c.logger.withUID(c.logger.opt.txIDFieldname, id))
connTx, err := drvTx.BeginTx(ctx, opts)
if err != nil {
lvl = LevelError
}
c.logger.log(ctx, lvl, "BeginTx", start, err, logs...)
return c.transaction(connTx, err, id)
}
// PrepareContext implements driver.ConnPrepareContext
func (c *connection) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
driverPrep, ok := c.Conn.(driver.ConnPrepareContext)
if !ok {
return nil, driver.ErrSkip
}
lvl, start, id := c.logger.opt.preparerLevel, time.Now(), c.logger.opt.uidGenerator.UniqueID()
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withUID(c.logger.opt.stmtIDFieldname, id))
driverStmt, err := driverPrep.PrepareContext(ctx, query)
if err != nil {
lvl = LevelError
}
c.logger.log(ctx, lvl, "PrepareContext", start, err, logs...)
return c.statement(driverStmt, err, id, query)
}
// Ping implements driver.Pinger
func (c *connection) Ping(ctx context.Context) error {
driverPinger, ok := c.Conn.(driver.Pinger)
if !ok {
return driver.ErrSkip
}
lvl, start := LevelDebug, time.Now()
err := driverPinger.Ping(ctx)
if err != nil {
lvl = LevelError
}
c.logger.log(ctx, lvl, "Ping", start, err, c.logData()...)
return err
}
// Exec implements driver.Execer
// Deprecated: use ExecContext() instead
func (c *connection) Exec(query string, args []driver.Value) (driver.Result, error) {
driverExecer, ok := c.Conn.(driver.Execer) // nolint // disable static check on deprecated driver method
if !ok {
return nil, driver.ErrSkip
}
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withArgs(args))
lvl, start := c.logger.opt.execerLevel, time.Now()
res, err := driverExecer.Exec(query, args)
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "Exec", start, err, logs...)
return c.result(res, err, query, args)
}
// ExecContext implements driver.ExecerContext
func (c *connection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
driverExecerContext, ok := c.Conn.(driver.ExecerContext)
if !ok {
return nil, driver.ErrSkip
}
logArgs := namedValuesToValues(args)
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withArgs(logArgs))
lvl, start := c.logger.opt.execerLevel, time.Now()
res, err := driverExecerContext.ExecContext(ctx, query, args)
if err != nil {
lvl = LevelError
}
c.logger.log(ctx, lvl, "ExecContext", start, err, logs...)
return c.result(res, err, query, logArgs)
}
// Query implements driver.Queryer
// Deprecated: use QueryContext() instead
func (c *connection) Query(query string, args []driver.Value) (driver.Rows, error) {
driverQueryer, ok := c.Conn.(driver.Queryer) // nolint // disable static check on deprecated driver method
if !ok {
return nil, driver.ErrSkip
}
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withArgs(args))
lvl, start := c.logger.opt.queryerLevel, time.Now()
res, err := driverQueryer.Query(query, args)
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "Query", start, err, logs...)
return c.rows(res, err, query, args)
}
// QueryContext implements driver.QueryerContext
func (c *connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
driverQueryerContext, ok := c.Conn.(driver.QueryerContext)
if !ok {
return nil, driver.ErrSkip
}
logArgs := namedValuesToValues(args)
logs := append(c.logData(), c.logger.withQuery(query), c.logger.withArgs(logArgs))
lvl, start := c.logger.opt.queryerLevel, time.Now()
res, err := driverQueryerContext.QueryContext(ctx, query, args)
if err != nil {
lvl = LevelError
}
c.logger.log(ctx, lvl, "QueryContext", start, err, logs...)
return c.rows(res, err, query, logArgs)
}
// ResetSession implements driver.SessionResetter
func (c *connection) ResetSession(ctx context.Context) error {
resetter, ok := c.Conn.(driver.SessionResetter)
if !ok {
return driver.ErrSkip
}
lvl, start := LevelTrace, time.Now()
err := resetter.ResetSession(ctx)
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "ResetSession", start, err, c.logData()...)
return err
}
// CheckNamedValue implements driver.NamedValueChecker
func (c *connection) CheckNamedValue(nm *driver.NamedValue) error {
checker, ok := c.Conn.(driver.NamedValueChecker)
if !ok {
return driver.ErrSkip
}
lvl, start := LevelTrace, time.Now()
err := checker.CheckNamedValue(nm)
if err != nil {
lvl = LevelError
}
c.logger.log(context.Background(), lvl, "CheckNamedValue", start, err, c.logData()...)
return err
}
func (c *connection) transaction(tx driver.Tx, err error, id string) (driver.Tx, error) {
if err != nil {
return tx, err
}
return &transaction{Tx: tx, logger: c.logger, connID: c.id, id: id}, nil
}
func (c *connection) statement(stmt driver.Stmt, err error, id, query string) (driver.Stmt, error) {
if err != nil {
return stmt, err
}
return &statement{Stmt: stmt, query: query, logger: c.logger, connID: c.id, id: id}, nil
}
func (c *connection) rows(res driver.Rows, err error, query string, args []driver.Value) (driver.Rows, error) {
if !c.logger.opt.wrapResult || err != nil {
return res, err
}
return &rows{Rows: res, logger: c.logger, connID: c.id, query: query, args: args}, nil
}
func (c *connection) result(res driver.Result, err error, query string, args []driver.Value) (driver.Result, error) {
if !c.logger.opt.wrapResult || err != nil {
return res, err
}
return &result{Result: res, logger: c.logger, connID: c.id, query: query, args: args}, nil
}
// logData default log data for connection.
func (c *connection) logData() []dataFunc {
return []dataFunc{
c.logger.withUID(c.logger.opt.connIDFieldname, c.id),
}
}