-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
statement.go
237 lines (197 loc) · 6.28 KB
/
statement.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
package otelsql
import (
"context"
"database/sql/driver"
)
const (
metricMethodStmtExec = "go.sql.stmt.exec"
traceMethodStmtExec = "exec"
metricMethodStmtQuery = "go.sql.stmt.query"
traceMethodStmtQuery = "query"
)
// Deprecated: Drivers should implement NamedValueChecker.
type columnConverter interface {
ColumnConverter(idx int) driver.ValueConverter
}
type stmt struct {
stmtQuery string
exec execContextFunc
execContext execContextFunc
query queryContextFunc
queryContext queryContextFunc
close func() error
numInput func() int
}
func (s stmt) Exec(args []driver.Value) (res driver.Result, err error) {
return s.exec(context.Background(), s.stmtQuery, valuesToNamedValues(args))
}
func (s stmt) Close() error {
return s.close()
}
func (s stmt) NumInput() int {
return s.numInput()
}
func (s stmt) Query(args []driver.Value) (rows driver.Rows, err error) {
return s.query(context.Background(), s.stmtQuery, valuesToNamedValues(args))
}
func (s stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
return s.execContext(ctx, s.stmtQuery, args)
}
func (s stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
return s.queryContext(ctx, s.stmtQuery, args)
}
type stmtConfig struct {
query string
execFuncMiddlewares []execContextFuncMiddleware
execContextFuncMiddlewares []execContextFuncMiddleware
queryFuncMiddlewares []queryContextFuncMiddleware
queryContextFuncMiddlewares []queryContextFuncMiddleware
}
// nolint: cyclop,funlen,gocyclo
func wrapStmt(parent driver.Stmt, cfg stmtConfig) driver.Stmt {
s := makeStmt(parent, cfg)
var (
_, hasExeCtx = parent.(driver.StmtExecContext)
_, hasQryCtx = parent.(driver.StmtQueryContext)
c, hasColConv = parent.(columnConverter)
n, hasNamValChk = parent.(driver.NamedValueChecker)
)
switch {
default:
// case !hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
}{s}
case !hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
}{s, s}
case hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
}{s, s}
case hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
}{s, s, s}
case !hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
columnConverter
}{s, c}
case !hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
columnConverter
}{s, s, c}
case hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
columnConverter
}{s, s, c}
case hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
columnConverter
}{s, s, s, c}
case !hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.NamedValueChecker
}{s, n}
case !hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, n}
case hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.NamedValueChecker
}{s, s, n}
case hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, s, n}
case !hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
columnConverter
driver.NamedValueChecker
}{s, c, n}
case !hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
columnConverter
driver.NamedValueChecker
}{s, s, c, n}
case hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
columnConverter
driver.NamedValueChecker
}{s, s, c, n}
case hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
columnConverter
driver.NamedValueChecker
}{s, s, s, c, n}
}
}
func makeStmt(parent driver.Stmt, cfg stmtConfig) stmt {
return stmt{
stmtQuery: cfg.query,
exec: makeStmtExecFunc(parent, cfg.execFuncMiddlewares),
execContext: makeStmtExecContextFunc(parent, cfg.execContextFuncMiddlewares),
query: makeStmtQueryFunc(parent, cfg.queryFuncMiddlewares),
queryContext: makeStmtQueryContextFunc(parent, cfg.queryContextFuncMiddlewares),
close: parent.Close,
numInput: parent.NumInput,
}
}
func makeStmtExecFunc(parent driver.Stmt, execContextFuncMiddlewares []middleware[execContextFunc]) execContextFunc {
return chainMiddlewares(execContextFuncMiddlewares, func(_ context.Context, _ string, args []driver.NamedValue) (driver.Result, error) {
return parent.Exec(namedValuesToValues(args)) // nolint: staticcheck
})
}
func makeStmtExecContextFunc(parent driver.Stmt, execContextFuncMiddlewares []middleware[execContextFunc]) execContextFunc {
execer, ok := parent.(driver.StmtExecContext)
if !ok {
return nopExecContext
}
return chainMiddlewares(execContextFuncMiddlewares, func(ctx context.Context, _ string, args []driver.NamedValue) (driver.Result, error) {
return execer.ExecContext(ctx, args)
})
}
func makeStmtQueryFunc(parent driver.Stmt, queryContextFuncMiddlewares []middleware[queryContextFunc]) queryContextFunc {
return chainMiddlewares(queryContextFuncMiddlewares, func(_ context.Context, _ string, args []driver.NamedValue) (driver.Rows, error) {
return parent.Query(namedValuesToValues(args)) // nolint: staticcheck
})
}
func makeStmtQueryContextFunc(parent driver.Stmt, queryContextFuncMiddlewares []middleware[queryContextFunc]) queryContextFunc {
queryer, ok := parent.(driver.StmtQueryContext)
if !ok {
return nopQueryContext
}
return chainMiddlewares(queryContextFuncMiddlewares, func(ctx context.Context, _ string, args []driver.NamedValue) (driver.Rows, error) {
return queryer.QueryContext(ctx, args)
})
}