forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
138 lines (117 loc) · 2.61 KB
/
hook.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
package pg
import (
"fmt"
"runtime"
"strings"
"time"
"github.com/go-pg/pg/orm"
)
type dummyDB struct {
orm.DB
}
var _ orm.DB = dummyDB{}
func (dummyDB) FormatQuery(dst []byte, query string, params ...interface{}) []byte {
return append(dst, query...)
}
type QueryProcessedEvent struct {
StartTime time.Time
Func string
File string
Line int
DB orm.DB
Query interface{}
Params []interface{}
Result orm.Result
Error error
}
func (ev *QueryProcessedEvent) UnformattedQuery() (string, error) {
b, err := queryString(ev.Query)
if err != nil {
return "", err
}
return string(b), nil
}
func (ev *QueryProcessedEvent) FormattedQuery() (string, error) {
b, err := appendQuery(nil, ev.DB, ev.Query, ev.Params...)
if err != nil {
return "", err
}
return string(b), nil
}
func queryString(query interface{}) ([]byte, error) {
switch query := query.(type) {
case orm.QueryAppender:
query = query.Copy()
query.Query().DB(dummyDB{})
return query.AppendQuery(nil)
case string:
return dummyDB{}.FormatQuery(nil, query), nil
default:
return nil, fmt.Errorf("pg: can't append %T", query)
}
}
type queryProcessedHook func(*QueryProcessedEvent)
// OnQueryProcessed calls the fn with QueryProcessedEvent
// when query is processed.
func (db *DB) OnQueryProcessed(fn func(*QueryProcessedEvent)) {
db.queryProcessedHooks = append(db.queryProcessedHooks, fn)
}
func (db *DB) queryProcessed(
ormDB orm.DB,
start time.Time,
query interface{},
params []interface{},
res orm.Result,
err error,
) {
if len(db.queryProcessedHooks) == 0 {
return
}
funcName, file, line := fileLine(2)
event := &QueryProcessedEvent{
StartTime: start,
Func: funcName,
File: file,
Line: line,
DB: ormDB,
Query: query,
Params: params,
Result: res,
Error: err,
}
for _, hook := range db.queryProcessedHooks {
hook(event)
}
}
const packageName = "github.com/go-pg/pg"
func fileLine(depth int) (string, string, int) {
for i := depth; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if strings.Contains(file, packageName) {
continue
}
_, funcName := packageFuncName(pc)
return funcName, file, line
}
return "", "", 0
}
func packageFuncName(pc uintptr) (string, string) {
f := runtime.FuncForPC(pc)
if f == nil {
return "", ""
}
packageName := ""
funcName := f.Name()
if ind := strings.LastIndex(funcName, "/"); ind > 0 {
packageName += funcName[:ind+1]
funcName = funcName[ind+1:]
}
if ind := strings.Index(funcName, "."); ind > 0 {
packageName += funcName[:ind]
funcName = funcName[ind+1:]
}
return packageName, funcName
}