7
7
"strconv"
8
8
"strings"
9
9
"text/template"
10
+ "time"
10
11
)
11
12
12
13
type param struct {
@@ -29,22 +30,28 @@ type context struct {
29
30
namedArgs []sql.NamedArg
30
31
values []interface {}
31
32
paramMap map [string ]interface {}
33
+ timer * timer
32
34
}
33
35
34
- func newContext (named bool , dialect Dialect , m map [string ]interface {}) * context {
36
+ func newContext (named bool , dialect Dialect , timeFn func () time. Time , m map [string ]interface {}) * context {
35
37
params := make ([]* param , len (m ))
36
38
i := 0
37
39
for k , v := range m {
38
40
params [i ] = newParam (k , v )
39
41
i ++
40
42
}
43
+ fn := time .Now
44
+ if timeFn != nil {
45
+ fn = timeFn
46
+ }
41
47
return & context {
42
48
named : named ,
43
49
dialect : dialect ,
44
50
params : params ,
45
51
namedArgs : []sql.NamedArg {},
46
52
values : []interface {}{},
47
53
paramMap : m ,
54
+ timer : newTimer (fn ),
48
55
}
49
56
}
50
57
@@ -120,11 +127,48 @@ func (c *context) in(name string) string {
120
127
return "(" + strings .Join (placeholders , ", " ) + ")"
121
128
}
122
129
130
+ func (c * context ) time () string {
131
+ name := "time__"
132
+ if c .named {
133
+ c .addNamed (name , c .timer .time ())
134
+ return c .dialect .NamedPlaceholderPrefix () + name
135
+ }
136
+
137
+ if c .dialect .IsOrdinalPlaceholderSupported () {
138
+ if c .timer .cacheIndex == 0 {
139
+ c .values = append (c .values , c .timer .time ())
140
+ c .timer .cacheIndex = len (c .values )
141
+ }
142
+ return c .dialect .OrdinalPlaceHolderPrefix () + strconv .Itoa (c .timer .cacheIndex )
143
+ }
144
+
145
+ c .values = append (c .values , c .timer .time ())
146
+ return c .dialect .Placeholder ()
147
+ }
148
+
149
+ func (c * context ) now () string {
150
+ name := "now__" + strconv .Itoa (c .timer .nowCnt )
151
+ if c .named {
152
+ c .addNamed (name , c .timer .now ())
153
+ return c .dialect .NamedPlaceholderPrefix () + name
154
+ }
155
+
156
+ if c .dialect .IsOrdinalPlaceholderSupported () {
157
+ c .values = append (c .values , c .timer .now ())
158
+ return c .dialect .OrdinalPlaceHolderPrefix () + strconv .Itoa (len (c .values ))
159
+ }
160
+
161
+ c .values = append (c .values , c .timer .now ())
162
+ return c .dialect .Placeholder ()
163
+ }
164
+
123
165
func (c * context ) funcMap () template.FuncMap {
124
166
return template.FuncMap {
125
167
"param" : c .param ,
126
168
"p" : c .param ,
127
169
"in" : c .in ,
170
+ "time" : c .time ,
171
+ "now" : c .now ,
128
172
}
129
173
}
130
174
0 commit comments