-
Notifications
You must be signed in to change notification settings - Fork 27
/
proxy.go
127 lines (113 loc) · 2.77 KB
/
proxy.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
// a proxy package is a proxy driver for database/sql.
package proxy
import (
"context"
"database/sql/driver"
)
// namedValueChecker is the same as driver.NamedValueChecker.
// Copied from database/sql/driver/driver.go for supporting Go 1.8.
type namedValueChecker interface {
// CheckNamedValue is called before passing arguments to the driver
// and is called in place of any ColumnConverter. CheckNamedValue must do type
// validation and conversion as appropriate for the driver.
CheckNamedValue(*driver.NamedValue) error
}
// Proxy is a sql driver.
// It adds hook points to other sql drivers.
type Proxy struct {
Driver driver.Driver
hooks hooks
}
// NewProxy creates new Proxy driver.
// Deprecated: You should use NewProxyContext instead.
func NewProxy(driver driver.Driver, hs ...*Hooks) *Proxy {
switch {
case len(hs) == 0:
return &Proxy{
Driver: driver,
}
case len(hs) == 1 && hs[0] != nil:
return &Proxy{
Driver: driver,
hooks: hs[0],
}
}
hooksSlice := make([]hooks, 0, len(hs))
for _, hk := range hs {
if hk != nil {
hooksSlice = append(hooksSlice, hk)
}
}
return &Proxy{
Driver: driver,
hooks: multipleHooks(hooksSlice),
}
}
// NewProxyContext creates new Proxy driver.
func NewProxyContext(driver driver.Driver, hs ...*HooksContext) *Proxy {
switch {
case len(hs) == 0:
return &Proxy{
Driver: driver,
}
case len(hs) == 1 && hs[0] != nil:
return &Proxy{
Driver: driver,
hooks: hs[0],
}
}
hooksSlice := make([]hooks, 0, len(hs))
for _, hk := range hs {
if hk != nil {
hooksSlice = append(hooksSlice, hk)
}
}
return &Proxy{
Driver: driver,
hooks: multipleHooks(hooksSlice),
}
}
func (p *Proxy) getHooks(ctx context.Context) hooks {
if h, ok := ctx.Value(contextHooksKey{}).(hooks); ok {
// Make the caller nil check easy.
if h == (*Hooks)(nil) || h == (*HooksContext)(nil) {
return nil
}
return h
}
return p.hooks
}
// Open creates new connection which is wrapped by Conn.
// It will triggers PreOpen, Open, PostOpen hooks.
func (p *Proxy) Open(name string) (driver.Conn, error) {
c := context.Background()
var err error
var ctx interface{}
var conn driver.Conn
var myconn *Conn
if p.hooks != nil {
// Setup PostOpen. This needs to be a closure like this
// or otherwise changes to the `ctx` and `conn` parameters
// within this Open() method does not get applied at the
// time defer is fired
defer func() { p.hooks.postOpen(c, ctx, myconn, err) }()
if ctx, err = p.hooks.preOpen(c, name); err != nil {
return nil, err
}
}
conn, err = p.Driver.Open(name)
if err != nil {
return nil, err
}
myconn = &Conn{
Conn: conn,
Proxy: p,
}
if p.hooks != nil {
if err = p.hooks.open(c, ctx, myconn); err != nil {
conn.Close()
return nil, err
}
}
return myconn, nil
}