-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
177 lines (142 loc) · 4.12 KB
/
sql.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
package testutil
import (
"context"
"database/sql"
"fmt"
"testing"
)
type db interface {
Exec(query string, args ...any) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
// DumpSQL ...
func DumpSQL(t *testing.T, db db, driverName string, opts ...SQLOption) *SQLDumper {
d := &SQLDumper{
t: t,
db: db,
driverName: driverName,
optsByCall: make(map[int][]SQLOption),
resultsByCall: make(map[int]any),
opts: opts,
seen: make(map[string]int),
}
t.Cleanup(d.dump)
return d
}
var _ db = (*SQLDumper)(nil)
// SQLDumper logs the query and args.
type SQLDumper struct {
t *testing.T
db db
driverName string
i int
optsByCall map[int][]SQLOption
resultsByCall map[int]any
opts []SQLOption
seen map[string]int
calls []SQLCall
}
type SQLCall struct {
params *SQL
driverName string
}
// SetDB sets the db.
func (r *SQLDumper) SetDB(db db) {
r.db = db
}
// SetDriverName sets the driver name.
func (r *SQLDumper) SetDriverName(driverName string) {
r.driverName = driverName
}
func (r *SQLDumper) SetResult(v any) {
r.resultsByCall[r.i-1] = v
}
// Calls returns the number of calls made to the db.
func (r *SQLDumper) Calls() int {
return r.i
}
// SetCallOptions sets the options for the i-th call.
func (r *SQLDumper) SetCallOptions(i int, optsByCall ...SQLOption) {
r.optsByCall[i] = optsByCall
}
// Options returns the options for the i-th call.
func (r *SQLDumper) Options(i int) []SQLOption {
return append(r.opts, r.optsByCall[i]...)
}
func (r *SQLDumper) log(method, query string, args ...any) {
defer func() {
r.i++
}()
var fileName string
opts := r.Options(r.i)
for _, o := range opts {
switch v := o.(type) {
case FileName:
fileName = string(v)
}
}
if fileName == "" {
fileName = method
}
r.seen[fileName]++
if n := r.seen[fileName]; n > 1 {
fileName = fmt.Sprintf("%s#%d", fileName, n-1)
}
r.optsByCall[r.i] = append(opts, FileName(fileName))
r.calls = append(r.calls, SQLCall{
params: &SQL{Args: args, Query: query},
driverName: r.driverName,
})
}
func (r *SQLDumper) dump() {
for i, call := range r.calls {
call := call
call.params.Result = r.resultsByCall[i]
switch call.driverName {
case "postgres":
DumpPostgres(r.t, call.params, r.Options(i)...)
case "mysql":
DumpMySQL(r.t, call.params, r.Options(i)...)
default:
panic("unsupported driver: " + r.driverName)
}
}
}
func (r *SQLDumper) Exec(query string, args ...any) (sql.Result, error) {
r.log("exec", query, args...)
return r.db.Exec(query, args...)
}
func (r *SQLDumper) Prepare(query string) (*sql.Stmt, error) {
r.log("prepare", query)
return r.db.Prepare(query)
}
func (r *SQLDumper) Query(query string, args ...any) (*sql.Rows, error) {
r.log("query", query, args...)
return r.db.Query(query, args...)
}
func (r *SQLDumper) QueryRow(query string, args ...any) *sql.Row {
r.log("query_row", query, args...)
return r.db.QueryRow(query, args...)
}
func (r *SQLDumper) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
r.log("exec_context", query, args...)
return r.db.ExecContext(ctx, query, args...)
}
func (r *SQLDumper) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
r.log("prepare_context", query)
return r.db.PrepareContext(ctx, query)
}
func (r *SQLDumper) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
r.log("query_context", query, args...)
return r.db.QueryContext(ctx, query, args...)
}
func (r *SQLDumper) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
r.log("query_row_context", query, args...)
return r.db.QueryRowContext(ctx, query, args...)
}