/
connection_callback.go
274 lines (245 loc) · 7.42 KB
/
connection_callback.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
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dml
import (
"context"
"database/sql/driver"
"fmt"
"github.com/corestoreio/errors"
)
// wrapDriver is used to create a new instrumented driver, it takes a vendor specific
// driver, and a call back instance to produce a new driver instance. It's usually
// used inside a sql.Register() statement
func wrapDriver(drv driver.Driver, cb DriverCallBack, hasUniqueIDFn bool) driver.Driver {
return cbDriver{drv: drv, cb: cb, hasUniqueIDFn: hasUniqueIDFn}
}
// DriverCallBack defines the call back signature used in every driver function.
// The returned function gets called in a defer. `fnName` states the name of the
// parent function like PrepareContext or Query, etc. The call to the first
// function can be used to e.g. start a timer. The call to second function can
// log the query and its args and also measure the time spend. The error as
// first argument in the returned function comes from the parent called function
// and should be returned or wrapped into a new one. `namedArgs` contains the,
// sometimes, named arguments. It can also be nil. context.Context can be added
// later.
type DriverCallBack func(fnName string) func(err error, query string, args []driver.NamedValue) error
// cbDriver implements a database/sql/driver.Driver
type cbDriver struct {
drv driver.Driver
cb DriverCallBack
hasUniqueIDFn bool
}
func (drv cbDriver) Open(name string) (driver.Conn, error) {
conn, err := drv.drv.Open(name)
if err != nil {
return nil, err
}
fc, ok := conn.(fullConner)
if !ok {
return nil, errors.NotSupported.Newf("[dml] Driver does not support all required interfaces (fullConner)")
}
return cbConn{Conn: fc, cb: drv.cb, hasUniqueIDFn: drv.hasUniqueIDFn}, nil
}
type fullConner interface {
driver.Conn
driver.ConnPrepareContext
driver.ExecerContext
driver.QueryerContext
driver.Pinger
driver.ConnBeginTx
// driver.ResetSessioner // later
}
type cbConn struct {
Conn fullConner
cb DriverCallBack
hasUniqueIDFn bool
}
func (c cbConn) queryWithID(query *string) {
}
func (c cbConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
fn := c.cb("Conn.PrepareContext")
var id string
if c.hasUniqueIDFn {
id, _ = extractSQLIDPrefix(query)
}
defer func() {
if errFn := fn(err, query, nil); err == nil && errFn != nil {
err = errFn
}
}()
if stmt, err = c.Conn.PrepareContext(ctx, query); err != nil {
return nil, err
}
fStmt, ok := stmt.(fullStmter)
if !ok {
return nil, errors.NotSupported.Newf("[dml] Driver does not support all required interfaces (fullStmter) ID:%s", id)
}
if id == "" {
id = query
}
return &cbStmt{Stmt: fStmt, cb: c.cb, query: fmt.Sprintf("Prepared ID:%s", id)}, nil
}
func (c cbConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (res driver.Result, err error) {
fn := c.cb("Conn.ExecContext")
defer func() {
if errFn := fn(err, query, args); err == nil && errFn != nil {
err = errFn
}
}()
res, err = c.Conn.ExecContext(ctx, query, args)
return // do not write `return c.Conn.ExecContext` because of the defer
}
func (c cbConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rws driver.Rows, err error) {
fn := c.cb("Conn.QueryContext")
defer func() {
if errFn := fn(err, query, args); err == nil && errFn != nil {
err = errFn
}
}()
rws, err = c.Conn.QueryContext(ctx, query, args)
return // do not write `return c.Conn.QueryContext` because of the defer
}
func (c cbConn) Prepare(query string) (stmt driver.Stmt, err error) {
var id string
if c.hasUniqueIDFn {
id, _ = extractSQLIDPrefix(query)
}
fn := c.cb("Conn.Prepare")
defer func() {
if errFn := fn(err, query, nil); err == nil && errFn != nil {
err = errFn
}
}()
if stmt, err = c.Conn.Prepare(query); err != nil {
return nil, err
}
fStmt, ok := stmt.(fullStmter)
if !ok {
return nil, errors.NotSupported.Newf("[dml] Driver does not support all required interfaces (fullStmter) ID:%s", id)
}
if id == "" {
id = query
}
return &cbStmt{Stmt: fStmt, cb: c.cb, query: fmt.Sprintf("Prepared ID:%s", id)}, nil
}
func (c cbConn) Close() (err error) {
fn := c.cb("Conn.Close")
defer func() {
if errFn := fn(err, "", nil); err == nil && errFn != nil {
err = errFn
}
}()
err = c.Conn.Close()
return
}
func (c cbConn) Begin() (tx driver.Tx, err error) {
fn := c.cb("Conn.Begin")
defer func() {
if errFn := fn(err, "", nil); err == nil && errFn != nil {
err = errFn
}
}()
tx, err = c.Conn.Begin()
return
}
func (c cbConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
fn := c.cb("Conn.BeginTx")
defer func() {
if errFn := fn(err, "", nil); err == nil && errFn != nil {
err = errFn
}
}()
tx, err = c.Conn.BeginTx(ctx, opts)
return
}
func (c cbConn) Ping(ctx context.Context) (err error) {
fn := c.cb("Conn.Ping")
defer func() {
if errFn := fn(err, "/*PING*/", nil); err == nil && errFn != nil {
err = errFn
}
}()
err = c.Conn.Ping(ctx)
return
}
type fullStmter interface {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
}
// Stmt implements a database/sql/driver.Stmt
type cbStmt struct {
Stmt fullStmter
cb DriverCallBack
query string
}
func (stmt *cbStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
fn := stmt.cb("Stmt.ExecContext")
defer func() {
if errFn := fn(err, stmt.query, args); err == nil && errFn != nil {
err = errFn
}
}()
res, err = stmt.Stmt.ExecContext(ctx, args)
return
}
func (stmt *cbStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rws driver.Rows, err error) {
fn := stmt.cb("Stmt.QueryContext")
defer func() {
if errFn := fn(err, stmt.query, args); err == nil && errFn != nil {
err = errFn
}
}()
rws, err = stmt.Stmt.QueryContext(ctx, args)
return
}
func (stmt *cbStmt) Close() (err error) {
fn := stmt.cb("Stmt.Close")
defer func() {
if errFn := fn(err, stmt.query, nil); err == nil && errFn != nil {
err = errFn
}
}()
err = stmt.Stmt.Close()
return
}
func (stmt *cbStmt) NumInput() int { return stmt.Stmt.NumInput() }
func driverValueToNamed(args []driver.Value) []driver.NamedValue {
ret := make([]driver.NamedValue, len(args))
for i, a := range args {
ret[i].Ordinal = i + 1
ret[i].Value = a
}
return ret
}
func (stmt *cbStmt) Exec(args []driver.Value) (res driver.Result, err error) {
fn := stmt.cb("Stmt.Exec")
defer func() {
if errFn := fn(err, stmt.query, driverValueToNamed(args)); err == nil && errFn != nil {
err = errFn
}
}()
res, err = stmt.Stmt.Exec(args)
return
}
func (stmt *cbStmt) Query(args []driver.Value) (rws driver.Rows, err error) {
fn := stmt.cb("Stmt.Query")
defer func() {
if errFn := fn(err, stmt.query, driverValueToNamed(args)); err == nil && errFn != nil {
err = errFn
}
}()
rws, err = stmt.Stmt.Query(args)
return
}