-
Notifications
You must be signed in to change notification settings - Fork 15
/
result.go
55 lines (44 loc) · 1.13 KB
/
result.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
package sqldblogger
import (
"context"
"database/sql/driver"
"time"
)
// result is a wrapper for driver.Result.
type result struct {
driver.Result
logger *logger
connID string
stmtID string
query string
args []driver.Value
}
// LastInsertId implement driver.Result
func (r *result) LastInsertId() (int64, error) {
lvl, start := LevelTrace, time.Now()
id, err := r.Result.LastInsertId()
if err != nil {
lvl = LevelError
}
r.logger.log(context.Background(), lvl, "ResultLastInsertId", start, err, r.logData()...)
return id, err
}
// RowsAffected implement driver.Result
func (r *result) RowsAffected() (int64, error) {
lvl, start := LevelTrace, time.Now()
num, err := r.Result.RowsAffected()
if err != nil {
lvl = LevelError
}
r.logger.log(context.Background(), lvl, "ResultRowsAffected", start, err, r.logData()...)
return num, err
}
// logData default log data for result.
func (r *result) logData() []dataFunc {
return []dataFunc{
r.logger.withUID(r.logger.opt.connIDFieldname, r.connID),
r.logger.withUID(r.logger.opt.stmtIDFieldname, r.stmtID),
r.logger.withQuery(r.query),
r.logger.withArgs(r.args),
}
}