-
Notifications
You must be signed in to change notification settings - Fork 15
/
rows.go
150 lines (119 loc) · 3.47 KB
/
rows.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
package sqldblogger
import (
"context"
"database/sql/driver"
"io"
"reflect"
"time"
)
// rows is a wrapper which implements:
// - driver.Rows
// - driver.RowsNextResultSet
// - driver.RowsColumnTypeScanType
// - driver.RowsColumnTypeDatabaseTypeName
// - driver.RowsColumnTypeLength
// - driver.RowsColumnTypeNullable
// - driver.RowsColumnTypePrecisionScale
type rows struct {
driver.Rows
logger *logger
connID string
stmtID string
query string
args []driver.Value
}
// Columns implement driver.Rows
func (r *rows) Columns() []string {
return r.Rows.Columns()
}
// Close implement driver.Rows
func (r *rows) Close() error {
lvl, start := LevelTrace, time.Now()
err := r.Rows.Close()
if err != nil {
lvl = LevelError
}
r.logger.log(context.Background(), lvl, "RowsClose", start, err, r.logData()...)
return err
}
// Next implement driver.Rows
func (r *rows) Next(dest []driver.Value) error {
logs := r.logData()
// dest contain value from database.
// If query arg not logged, dest arg here will also not logged.
if r.logger.opt.logArgs {
logs = append(logs, r.logger.withKeyArgs("rows_dest", dest))
}
lvl, start := LevelTrace, time.Now()
err := r.Rows.Next(dest)
if err != nil && err != io.EOF {
lvl = LevelError
}
r.logger.log(context.Background(), lvl, "RowsNext", start, err, logs...)
return err
}
// HasNextResultSet implement driver.RowsNextResultSet
func (r *rows) HasNextResultSet() bool {
if rs, ok := r.Rows.(driver.RowsNextResultSet); ok {
return rs.HasNextResultSet()
}
return false
}
// NextResultSet implement driver.RowsNextResultSet
func (r *rows) NextResultSet() error {
rs, ok := r.Rows.(driver.RowsNextResultSet)
if !ok {
return io.EOF
}
lvl, start := LevelTrace, time.Now()
err := rs.NextResultSet()
if err != nil && err != io.EOF {
lvl = LevelError
}
r.logger.log(context.Background(), lvl, "RowsNextResultSet", start, err, r.logData()...)
return err
}
// ColumnTypeScanType implement driver.RowsColumnTypeScanType
func (r *rows) ColumnTypeScanType(index int) reflect.Type {
if rs, ok := r.Rows.(driver.RowsColumnTypeScanType); ok {
return rs.ColumnTypeScanType(index)
}
return reflect.SliceOf(reflect.TypeOf(""))
}
// ColumnTypeDatabaseTypeName driver.RowsColumnTypeDatabaseTypeName
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
if rs, ok := r.Rows.(driver.RowsColumnTypeDatabaseTypeName); ok {
return rs.ColumnTypeDatabaseTypeName(index)
}
return ""
}
// ColumnTypeLength implement driver.RowsColumnTypeLength
func (r *rows) ColumnTypeLength(index int) (length int64, ok bool) {
if rs, ok := r.Rows.(driver.RowsColumnTypeLength); ok {
return rs.ColumnTypeLength(index)
}
return 0, false
}
// ColumnTypeNullable implement driver.RowsColumnTypeNullable
func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) {
if rs, ok := r.Rows.(driver.RowsColumnTypeNullable); ok {
return rs.ColumnTypeNullable(index)
}
return false, false
}
// ColumnTypePrecisionScale implement driver.RowsColumnTypePrecisionScale
func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
if rs, ok := r.Rows.(driver.RowsColumnTypePrecisionScale); ok {
return rs.ColumnTypePrecisionScale(index)
}
return 0, 0, false
}
// logData default log data for rows.
func (r *rows) 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),
}
}