-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqlhooks.go
420 lines (347 loc) · 10.7 KB
/
sqlhooks.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package korm
import (
"context"
"database/sql/driver"
"errors"
"strings"
"time"
"github.com/kamalshkeir/ksmux"
"github.com/kamalshkeir/lg"
)
var (
onInsert DbHook
onSet DbHook
onDelete func(database, table string, query string, args ...any) error
onDrop func(database, table string) error
)
type logAndCacheHook struct{}
func (h *logAndCacheHook) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
if useCache && len(query) > 6 {
if !onceDone {
go RunEvery(FlushCacheEvery, func(cancelChan chan struct{}) {
if !useCache {
cancelChan <- struct{}{}
}
flushCache()
})
onceDone = true
}
q := strings.TrimSpace(strings.ToLower(query[:6]))
if q != "select" && q != "pragma" {
flushCache()
}
}
if logQueries {
lg.Printfs("yl> %s %v", query, args)
return context.WithValue(ctx, ksmux.ContextKey("begin"), time.Now()), nil
}
return context.Background(), nil
}
func (h *logAndCacheHook) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
if logQueries {
begin := ctx.Value(ksmux.ContextKey("begin")).(time.Time)
lg.Printfs("yl, took: %v\n", time.Since(begin))
return ctx, nil
}
return context.Background(), nil
}
type DbHook func(database, table string, data map[string]any) error
func OnInsert(fn DbHook) {
onInsert = fn
}
func OnSet(fn DbHook) {
onSet = fn
}
func OnDelete(fn func(database, table string, query string, args ...any) error) {
onDelete = fn
}
func OnDrop(fn func(database, table string) error) {
onDrop = fn
}
// Hook is the hook callback signature
type Hook func(ctx context.Context, query string, args ...interface{}) (context.Context, error)
// ErrorHook is the error handling callback signature
type ErrorHook func(ctx context.Context, err error, query string, args ...interface{}) error
// Hooks instances may be passed to Wrap() to define an instrumented driver
type Hooks interface {
Before(ctx context.Context, query string, args ...interface{}) (context.Context, error)
After(ctx context.Context, query string, args ...interface{}) (context.Context, error)
}
// OnErrorer instances will be called if any error happens
type OnErrorer interface {
OnError(ctx context.Context, err error, query string, args ...interface{}) error
}
func handlerErr(ctx context.Context, hooks Hooks, err error, query string, args ...interface{}) error {
h, ok := hooks.(OnErrorer)
if !ok {
return err
}
if err := h.OnError(ctx, err, query, args...); err != nil {
return err
}
return err
}
// Driver implements a database/sql/driver.Driver
type Driver struct {
driver.Driver
hooks Hooks
}
// Open opens a connection
func (drv *Driver) Open(name string) (driver.Conn, error) {
conn, err := drv.Driver.Open(name)
if err != nil {
return conn, err
}
// Drivers that don't implement driver.ConnBeginTx are not supported.
if _, ok := conn.(driver.ConnBeginTx); !ok {
return nil, errors.New("driver must implement driver.ConnBeginTx")
}
wrapped := &driverConn{conn, drv.hooks}
if isExecer(conn) && isQueryer(conn) && isSessionResetter(conn) {
return &ExecerQueryerContextWithSessionResetter{wrapped,
&execerContext{wrapped}, &queryerContext{wrapped},
&SessionResetter{wrapped}}, nil
} else if isExecer(conn) && isQueryer(conn) {
return &ExecerQueryerContext{wrapped, &execerContext{wrapped},
&queryerContext{wrapped}}, nil
} else if isExecer(conn) {
// If conn implements an Execer interface, return a driver.Conn which
// also implements Execer
return &execerContext{wrapped}, nil
} else if isQueryer(conn) {
// If conn implements an Queryer interface, return a driver.Conn which
// also implements Queryer
return &queryerContext{wrapped}, nil
}
return wrapped, nil
}
// driverConn implements a database/sql.driver.driverConn
type driverConn struct {
Conn driver.Conn
hooks Hooks
}
func isSessionResetter(conn driver.Conn) bool {
_, ok := conn.(driver.SessionResetter)
return ok
}
func (conn *driverConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
var (
stmt driver.Stmt
err error
)
if c, ok := conn.Conn.(driver.ConnPrepareContext); ok {
stmt, err = c.PrepareContext(ctx, query)
} else {
stmt, err = conn.Prepare(query)
}
if err != nil {
return stmt, err
}
return &Stmt{stmt, conn.hooks, query}, nil
}
func (conn *driverConn) Prepare(query string) (driver.Stmt, error) {
return conn.Conn.Prepare(query)
}
func (conn *driverConn) Close() error { return conn.Conn.Close() }
func (conn *driverConn) Begin() (driver.Tx, error) {
return conn.BeginTx(context.Background(), driver.TxOptions{})
}
func (conn *driverConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return conn.Conn.(driver.ConnBeginTx).BeginTx(ctx, opts)
}
// execerContext implements a database/sql.driver.execerContext
type execerContext struct {
*driverConn
}
func isExecer(conn driver.Conn) bool {
switch conn.(type) {
case driver.ExecerContext:
return true
default:
return false
}
}
func (conn *execerContext) execContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
switch c := conn.Conn.(type) {
case driver.ExecerContext:
return c.ExecContext(ctx, query, args)
default:
// This should not happen
return nil, errors.New("ExecerContext created for a non Execer driver.Conn")
}
}
func (conn *execerContext) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
var err error
list := namedValueToAny(args)
// Exec `Before` Hooks
if ctx, err = conn.hooks.Before(ctx, query, list...); err != nil {
return nil, err
}
results, err := conn.execContext(ctx, query, args)
if err != nil {
return results, handlerErr(ctx, conn.hooks, err, query, list...)
}
if _, err := conn.hooks.After(ctx, query, list...); err != nil {
return nil, err
}
return results, err
}
func (conn *execerContext) Exec(query string, args []driver.Value) (driver.Result, error) {
// We have to implement Exec since it is required in the current version of
// Go for it to run ExecContext. From Go 10 it will be optional. However,
// this code should never run since database/sql always prefers to run
// ExecContext.
return nil, errors.New("Exec was called when ExecContext was implemented")
}
// queryerContext implements a database/sql.driver.queryerContext
type queryerContext struct {
*driverConn
}
func isQueryer(conn driver.Conn) bool {
switch conn.(type) {
case driver.QueryerContext:
return true
default:
return false
}
}
func (conn *queryerContext) queryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
switch c := conn.Conn.(type) {
case driver.QueryerContext:
return c.QueryContext(ctx, query, args)
default:
// This should not happen
return nil, errors.New("QueryerContext created for a non Queryer driver.Conn")
}
}
func (conn *queryerContext) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
var err error
list := namedValueToAny(args)
// Query `Before` Hooks
if ctx, err = conn.hooks.Before(ctx, query, list...); err != nil {
return nil, err
}
results, err := conn.queryContext(ctx, query, args)
if err != nil {
return results, handlerErr(ctx, conn.hooks, err, query, list...)
}
if _, err := conn.hooks.After(ctx, query, list...); err != nil {
return nil, err
}
return results, err
}
// ExecerQueryerContext implements database/sql.driver.ExecerContext and
// database/sql.driver.QueryerContext
type ExecerQueryerContext struct {
*driverConn
*execerContext
*queryerContext
}
// ExecerQueryerContext implements database/sql.driver.ExecerContext and
// database/sql.driver.QueryerContext
type ExecerQueryerContextWithSessionResetter struct {
*driverConn
*execerContext
*queryerContext
*SessionResetter
}
type SessionResetter struct {
*driverConn
}
// Stmt implements a database/sql/driver.Stmt
type Stmt struct {
Stmt driver.Stmt
hooks Hooks
query string
}
func (stmt *Stmt) execContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if s, ok := stmt.Stmt.(driver.StmtExecContext); ok {
return s.ExecContext(ctx, args)
}
values := make([]driver.Value, len(args))
for _, arg := range args {
values[arg.Ordinal-1] = arg.Value
}
return stmt.Exec(values)
}
func (stmt *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
var err error
list := namedValueToAny(args)
// Exec `Before` Hooks
if ctx, err = stmt.hooks.Before(ctx, stmt.query, list...); err != nil {
return nil, err
}
results, err := stmt.execContext(ctx, args)
if err != nil {
return results, handlerErr(ctx, stmt.hooks, err, stmt.query, list...)
}
if _, err := stmt.hooks.After(ctx, stmt.query, list...); err != nil {
return nil, err
}
return results, err
}
func (stmt *Stmt) queryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if s, ok := stmt.Stmt.(driver.StmtQueryContext); ok {
return s.QueryContext(ctx, args)
}
values := make([]driver.Value, len(args))
for _, arg := range args {
values[arg.Ordinal-1] = arg.Value
}
return stmt.Query(values)
}
func (stmt *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
var err error
list := namedValueToAny(args)
// Exec Before Hooks
if ctx, err = stmt.hooks.Before(ctx, stmt.query, list...); err != nil {
return nil, err
}
rows, err := stmt.queryContext(ctx, args)
if err != nil {
return rows, handlerErr(ctx, stmt.hooks, err, stmt.query, list...)
}
if _, err := stmt.hooks.After(ctx, stmt.query, list...); err != nil {
return nil, err
}
return rows, err
}
func (stmt *Stmt) Close() error { return stmt.Stmt.Close() }
func (stmt *Stmt) NumInput() int { return stmt.Stmt.NumInput() }
func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error) {
named := make([]driver.NamedValue, 0, len(args))
for i, a := range args {
v := driver.NamedValue{
Ordinal: i + 1,
Name: "",
Value: a,
}
named = append(named, v)
}
return stmt.ExecContext(context.Background(), named)
}
func (stmt *Stmt) Query(args []driver.Value) (driver.Rows, error) {
named := make([]driver.NamedValue, 0, len(args))
for i, a := range args {
v := driver.NamedValue{
Ordinal: i + 1,
Name: "",
Value: a,
}
named = append(named, v)
}
return stmt.QueryContext(context.Background(), named)
}
func Wrap(driver driver.Driver, hooks Hooks) driver.Driver {
return &Driver{driver, hooks}
}
func WrapConn(conn driver.Conn, hooks Hooks) driver.Conn {
return &driverConn{conn, hooks}
}
func namedValueToAny(args []driver.NamedValue) []interface{} {
list := make([]interface{}, len(args))
for i, a := range args {
list[i] = a.Value
}
return list
}