-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcallbacks.go
163 lines (125 loc) · 3.24 KB
/
callbacks.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
package gormexpect
import (
"bytes"
"fmt"
"regexp"
"github.com/jinzhu/gorm"
)
// Recorder satisfies the logger interface
type Recorder struct {
blankColumns []string
stmts []Stmt
preload []Preload // store it on Recorder
}
// Record records a Stmt for use when SQL is finally executed
// By default, it escapes with regexp.EscapeMeta
func (r *Recorder) Record(stmt Stmt, shouldEscape bool) {
if shouldEscape {
stmt.sql = regexp.QuoteMeta(stmt.sql)
}
r.stmts = append(r.stmts, stmt)
}
// GetFirst returns the first recorded sql statement logged. If there are no
// statements, false is returned
func (r *Recorder) GetFirst() (Stmt, bool) {
defer func() {
if len(r.stmts) > 1 {
r.stmts = r.stmts[1:]
return
}
r.stmts = []Stmt{}
}()
if len(r.stmts) > 0 {
return r.stmts[0], true
}
return Stmt{}, false
}
// IsEmpty returns true if the statements slice is empty
func (r *Recorder) IsEmpty() bool {
return len(r.stmts) == 0
}
// Stmt represents a sql statement. It can be an Exec, Query, or QueryRow
type Stmt struct {
kind string // can be Query, Exec, QueryRow
preload string // contains schema if it is a preload query
sql string
args []interface{}
}
func recordExecCallback(scope *gorm.Scope) {
r, ok := scope.Get("gorm:recorder")
recorder := r.(*Recorder)
if !ok {
panic(fmt.Errorf("Expected a recorder to be set, but got none"))
}
if scope.SQL == "" {
return
}
stmt := Stmt{
kind: "exec",
sql: scope.SQL,
args: scope.SQLVars,
}
if blankColumns, ok := scope.InstanceGet("gorm:blank_columns_with_default_value"); ok {
// use this hack to retrieve our columns later
recorder.blankColumns = blankColumns.([]string)
}
strs, cols := parseUpdateColumns(stmt.sql)
if len(cols) > 1 {
// we generate a better regex
newRegexp := bytes.NewBufferString("")
newRegexp.WriteString(strs[0])
newRegexp.WriteString("(")
for i, col := range cols {
if i == 0 {
newRegexp.WriteString(fmt.Sprintf("%s,?|", col))
continue
}
if i == len(cols)-1 {
newRegexp.WriteString(fmt.Sprintf(" %s,?)*", col))
continue
}
newRegexp.WriteString(fmt.Sprintf(" %s,?|", col))
}
newRegexp.WriteString(strs[1])
stmt.sql = newRegexp.String()
recorder.Record(stmt, false)
return
}
recorder.Record(stmt, true)
}
func populateScopeValueCallback(scope *gorm.Scope) {
// we need to see if we have a valid outval
returnValue, ok := scope.Get("gorm_expect:ret")
if ok {
scope.Value = returnValue
}
}
func recordQueryCallback(scope *gorm.Scope) {
r, ok := scope.Get("gorm:recorder")
if !ok {
panic(fmt.Errorf("Expected a recorder to be set, but got none"))
}
recorder := r.(*Recorder)
stmt := Stmt{
kind: "query",
sql: scope.SQL,
args: scope.SQLVars,
}
if len(recorder.preload) > 0 {
stmt.preload = recorder.preload[0].schema
// we just want to pop the first element off
recorder.preload = recorder.preload[1:]
}
recorder.Record(stmt, true)
}
func recordPreloadCallback(scope *gorm.Scope) {
// this callback runs _before_ gorm:preload
recorder, ok := scope.Get("gorm:recorder")
if !ok {
panic(fmt.Errorf("Expected a recorder to be set, but got none"))
}
preload := getPreload(scope)
if len(preload) > 0 {
recorder.(*Recorder).preload = preload
}
}