forked from j2gg0s/otsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
75 lines (59 loc) · 1.42 KB
/
event.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
package otsql
import (
"context"
"time"
)
type Hook interface {
Before(context.Context, *Event) context.Context
After(context.Context, *Event)
}
func before(hooks []Hook, ctx context.Context, evt *Event) context.Context {
for _, hook := range hooks {
ctx = hook.Before(ctx, evt)
}
return ctx
}
func after(hooks []Hook, ctx context.Context, evt *Event) {
for _, hook := range hooks {
hook.After(ctx, evt)
}
}
type Method string
var (
MethodPing Method = "ping"
MethodExec Method = "exec"
MethodQuery Method = "query"
MethodPrepare Method = "prepare"
MethodBegin Method = "begin"
MethodCommit Method = "commit"
MethodRollback Method = "rollback"
MethodLastInsertId Method = "last_insert_id"
MethodRowsAffected Method = "rows_affected"
MethodRowsClose Method = "rows_close"
MethodRowsNext Method = "rows_next"
MethodCreateConn Method = "create_conn"
MethodCloseConn Method = "close_conn"
MethodResetSession Method = "reset_session"
)
type Event struct {
Instance string
Database string
Method Method
Query string
Args interface{}
BeginAt time.Time
Err error
CloseFuncs []func(context.Context, error)
Conn string
}
func newEvent(o *Options, conn string, method Method, query string, args interface{}) *Event {
return &Event{
Instance: o.Instance,
Database: o.Database,
Conn: conn,
Method: method,
Query: query,
Args: args,
BeginAt: time.Now(),
}
}