-
Notifications
You must be signed in to change notification settings - Fork 11
/
options.go
118 lines (97 loc) · 2.79 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
package otsql
// Option allows for managing otsql configuration using functional options.
type Option func(*Options)
// Options holds configuration of our otsql hook.
// 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 {
// Instance, default parse from dsn.
Instance string
// Database, default parse from dsn.
Database string
// PingB, if set to true, will enable the hook of Ping requests.
PingB bool
// RowsAffectedB, if set to true, will enable the hook of RowsAffected calls.
RowsAffectedB bool
// LastInsertIdB, if set to true, will enable the hook LastInsertId calls.
LastInsertIdB bool
// RowsNextB, if set to true, will enable the hook of calls.
// This can result in many calls.
RowsNextB bool
// RowsCloseB, if set to true, will enable the hook of RowsClose calls.
RowsCloseB bool
// ResetSessionB, if set to true, will enable the hook of ResetSession calls.
ResetSessionB bool
// Hooks, enabled hooks.
Hooks []Hook
}
func newOptions(opts []Option) *Options {
o := &Options{}
for _, option := range opts {
option(o)
}
return o
}
// WithOptions sets our otsql options through a single
// Options object.
func WithOptions(options Options) Option {
return func(o *Options) {
*o = options
}
}
// WithInstance sets instance name, default parse from dsn when create conn.
func WithInstance(name string) Option {
return func(o *Options) {
o.Instance = name
}
}
// WithDatabase sets instance name, default parse from dsn when create conn.
func WithDatabaase(name string) Option {
return func(o *Options) {
o.Database = name
}
}
// WithPing if set to true, will enable the hook of Ping requests.
func WithPing(b bool) Option {
return func(o *Options) {
o.PingB = b
}
}
// WithRowsNext if set to true, will enable of RowsNext calls.
// This can result in many calls.
func WithRowsNext(b bool) Option {
return func(o *Options) {
o.RowsNextB = b
}
}
// WithRowsClose if set to true, will enable the of RowsClose calls.
func WithRowsClose(b bool) Option {
return func(o *Options) {
o.RowsCloseB = b
}
}
// WithRowsAffected if set to true, will enable the of RowsAffected calls.
func WithRowsAffected(b bool) Option {
return func(o *Options) {
o.RowsAffectedB = b
}
}
// WithLastInsertID if set to true, will enable the hook of LastInsertId calls.
func WithLastInsertID(b bool) Option {
return func(o *Options) {
o.LastInsertIdB = b
}
}
// WithResetSession if set to true, will enable the hook of ResetSession calls.
func WithResetSession(b bool) Option {
return func(o *Options) {
o.ResetSessionB = b
}
}
// WithHooks set hook.
func WithHooks(hooks ...Hook) Option {
return func(o *Options) {
o.Hooks = append(o.Hooks, hooks...)
}
}