-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
126 lines (107 loc) · 2.88 KB
/
options.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
package dbwrap
import (
"context"
"database/sql/driver"
)
// Middleware returns instrumented context and finalizer callback.
//
// Middleware is invoked before operation.
// Returned onFinish function is invoked after the operation.
type Middleware func(
ctx context.Context,
operation Operation,
statement string,
args []driver.NamedValue,
) (nCtx context.Context, onFinish func(error))
// Option allows for managing wrapper configuration using functional options.
type Option func(o *Options)
// Options holds configuration of our wrapper.
// By default all options are set to false intentionally when creating a wrapped
// driver and provide the most sensible default with both performance and
// security in mind.
type Options struct {
// Middlewares wrap operations.
Middlewares []Middleware
// Intercept mutates statement and/or parameters.
Intercept func(
ctx context.Context,
operation Operation,
statement string,
args []driver.NamedValue,
) (context.Context, string, []driver.NamedValue)
// Operations lists which operations should be wrapped.
Operations []Operation
operations map[Operation]bool
}
// WithOptions sets our wrapper options through a single
// Options object.
func WithOptions(options Options) Option {
return func(o *Options) {
*o = options
}
}
// WithMiddleware adds one or multiple middlewares to a db wrapper.
func WithMiddleware(mw ...Middleware) Option {
return func(o *Options) {
o.Middlewares = append(o.Middlewares, mw...)
}
}
// WithInterceptor sets statement interceptor to a db wrapper.
// Interceptor receives every statement that is to be requested
// and can change it.
func WithInterceptor(i func(
ctx context.Context,
operation Operation,
statement string,
args []driver.NamedValue,
) (context.Context, string, []driver.NamedValue),
) Option {
return func(o *Options) {
o.Intercept = i
}
}
// WithOperations controls which operations should be wrapped with middlewares.
// It does not affect statement interceptor.
func WithOperations(op ...Operation) Option {
return func(o *Options) {
o.Operations = append(o.Operations, op...)
}
}
// WithAllOperations enables all operations to be wrapped with middleware.
// This also includes RowsNext.
func WithAllOperations() Option {
return WithOperations(
Ping,
Exec,
Query,
Prepare,
Begin,
LastInsertID,
RowsAffected,
StmtExec,
StmtQuery,
StmtClose,
RowsClose,
RowsNext,
Commit,
Rollback,
)
}
// prepareOptions returns prepared Options and flag if they are operational.
func prepareOptions(options []Option) (Options, bool) {
o := Options{}
for _, option := range options {
option(&o)
}
if len(o.Middlewares) == 0 && o.Intercept == nil {
return o, false
}
o.operations = defaultOperations
if len(o.Operations) > 0 {
o.operations = make(map[Operation]bool, len(o.Operations))
for _, op := range o.Operations {
o.operations[op] = true
}
}
return o, true
}