forked from shogo82148/go-sql-proxy
/
proxy.go
250 lines (223 loc) · 9.02 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
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
// a proxy package is a proxy driver for dabase/sql.
package proxy
import "database/sql/driver"
type Proxy struct {
Driver driver.Driver
Hooks *Hooks
}
type Hooks struct {
// PreOpen is a callback that gets called before any
// attempt to open the sql connection is made, and is ALWAYS
// called.
//
// The first return value is passed to both `Hooks.Open` and
// `Hooks.PostOpen` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Open` method and `Hooks.Open`
// methods are not called.
PreOpen func(name string) (interface{}, error)
// Open is called after the underlying driver's `Driver.Open` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
//
// If this callback returns an error, then the `conn` object is
// closed by calling the `Close` method, and the error from this
// callback is returned by the `db.Open` method.
Open func(ctx interface{}, conn driver.Conn) error
// PostOpen is a callback that gets called at the end of
// the call to `db.Open(). It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreOpen` method, and may be nil.
PostOpen func(ctx interface{}, conn driver.Conn) error
// PreExec is a callback that gets called prior to calling
// `Stmt.Exec`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Exec` and `Hooks.Exec` methods
// are not called.
//
// The first return value is passed to both `Hooks.Exec` and
// `Hooks.PostExec` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Driver.Exec` method and `Hooks.Exec`
// methods are not called.
PreExec func(stmt *Stmt, args []driver.Value) (interface{}, error)
// Exec is called after the underlying driver's `Driver.Exec` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Exec` method.
Exec func(ctx interface{}, stmt *Stmt, args []driver.Value, result driver.Result) error
// PostExec is a callback that gets called at the end of
// the call to `Stmt.Exec`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreExec` method, and may be nil.
PostExec func(ctx interface{}, stmt *Stmt, args []driver.Value, result driver.Result) error
// PreQuery is a callback that gets called prior to calling
// `Stmt.Query`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Stmt.Query` and `Hooks.Query` methods
// are not called.
//
// The first return value is passed to both `Hooks.Query` and
// `Hooks.PostQuery` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Stmt.Query` method and `Hooks.Query`
// methods are not called.
PreQuery func(stmt *Stmt, args []driver.Value) (interface{}, error)
// Query is called after the underlying driver's `Stmt.Query` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Stmt.Query` method.
Query func(ctx interface{}, stmt *Stmt, args []driver.Value, rows driver.Rows) error
// PostQuery is a callback that gets called at the end of
// the call to `Stmt.Query`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreQuery` method, and may be nil.
PostQuery func(ctx interface{}, stmt *Stmt, args []driver.Value, rows driver.Rows) error
// PreBegin is a callback that gets called prior to calling
// `Stmt.Begin`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Conn.Begin` and `Hooks.Begin` methods
// are not called.
//
// The first return value is passed to both `Hooks.Begin` and
// `Hooks.PostBegin` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Conn.Begin` method and `Hooks.Begin`
// methods are not called.
PreBegin func(conn *Conn) (interface{}, error)
// Begin is called after the underlying driver's `Conn.Begin` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Conn.Begin` method.
Begin func(ctx interface{}, conn *Conn) error
// PostBegin is a callback that gets called at the end of
// the call to `Conn.Begin`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreBegin` method, and may be nil.
PostBegin func(ctx interface{}, conn *Conn) error
// PreCommit is a callback that gets called prior to calling
// `Tx.Commit`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Commit` and `Hooks.Commit` methods
// are not called.
//
// The first return value is passed to both `Hooks.Commit` and
// `Hooks.PostCommit` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Commit` method and `Hooks.Commit`
// methods are not called.
PreCommit func(tx *Tx) (interface{}, error)
// Commit is called after the underlying driver's `Tx.Commit` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Commit` method.
Commit func(ctx interface{}, tx *Tx) error
// PostCommit is a callback that gets called at the end of
// the call to `Tx.Commit`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreCommit` method, and may be nil.
PostCommit func(ctx interface{}, tx *Tx) error
// PreRollback is a callback that gets called prior to calling
// `Tx.Rollback`, and is ALWAYS called. If this callback returns an
// error, the underlying driver's `Tx.Rollback` and `Hooks.Rollback` methods
// are not called.
//
// The first return value is passed to both `Hooks.Rollback` and
// `Hooks.PostRollback` callbacks. You may specify anything you want.
// Return nil if you do not need to use it.
//
// The second return value is indicates the error found while
// executing this hook. If this callback returns an error,
// the underlying driver's `Tx.Rollback` method and `Hooks.Rollback`
PreRollback func(tx *Tx) (interface{}, error)
// Rollback is called after the underlying driver's `Tx.Rollback` method
// returns without any errors.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Tx.Rollback` method.
Rollback func(ctx interface{}, tx *Tx) error
// PostRollback is a callback that gets called at the end of
// the call to `Tx.Rollback`. It is ALWAYS called.
//
// The `ctx` parameter is the return value supplied from the
// `Hooks.PreRollback` method, and may be nil.
PostRollback func(ctx interface{}, tx *Tx) error
}
func NewProxy(driver driver.Driver, hooks *Hooks) *Proxy {
if hooks == nil {
hooks = &Hooks{}
}
return &Proxy{
Driver: driver,
Hooks: hooks,
}
}
func (p *Proxy) Open(name string) (driver.Conn, error) {
var err error
var ctx interface{}
var conn driver.Conn
if h := p.Hooks.PostOpen; h != 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() { h(ctx, conn) }()
}
if h := p.Hooks.PreOpen; h != nil {
if ctx, err = h(name); err != nil {
return nil, err
}
}
conn, err = p.Driver.Open(name)
if err != nil {
return nil, err
}
conn = &Conn{
Conn: conn,
Proxy: p,
}
if hook := p.Hooks.Open; hook != nil {
if err = hook(ctx, conn); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
}