Skip to content

Commit 0e3f2d5

Browse files
authored
Merge pull request #13 from pinzolo/time-funcs
Time funcs
2 parents 70c35b8 + 10c7733 commit 0e3f2d5

File tree

10 files changed

+741
-230
lines changed

10 files changed

+741
-230
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
3+
## 2018-05-16 JST: v0.0.1
4+
5+
First implementation
6+
7+
## 2018-06-25 JST: v1.0.0
8+
9+
Add `time` and `now` template funcs.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ ORDER BY /*% .order %*/id
3636

3737
```go
3838
// sql is generated SQL from template.
39-
// vals are arguments for generated SQL.
40-
sql, vals, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
39+
// args are arguments for generated SQL.
40+
sql, args, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
4141
"ids": []int{1, 2, 3},
4242
"order": "name DESC",
4343
"onlyMale": false,
4444
"name": "Alex",
4545
})
46+
rows, err := db.Query(sql, args...)
4647
```
4748

4849
### Generated SQL
@@ -76,7 +77,7 @@ ORDER BY name DESC
7677
$ go get github.com/pinzolo/sqlt
7778
```
7879

79-
## Support
80+
## Suppor
8081

8182
### Go version
8283

context.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"strings"
99
"text/template"
10+
"time"
1011
)
1112

1213
type param struct {
@@ -29,22 +30,28 @@ type context struct {
2930
namedArgs []sql.NamedArg
3031
values []interface{}
3132
paramMap map[string]interface{}
33+
timer *timer
3234
}
3335

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 {
3537
params := make([]*param, len(m))
3638
i := 0
3739
for k, v := range m {
3840
params[i] = newParam(k, v)
3941
i++
4042
}
43+
fn := time.Now
44+
if timeFn != nil {
45+
fn = timeFn
46+
}
4147
return &context{
4248
named: named,
4349
dialect: dialect,
4450
params: params,
4551
namedArgs: []sql.NamedArg{},
4652
values: []interface{}{},
4753
paramMap: m,
54+
timer: newTimer(fn),
4855
}
4956
}
5057

@@ -120,11 +127,48 @@ func (c *context) in(name string) string {
120127
return "(" + strings.Join(placeholders, ", ") + ")"
121128
}
122129

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+
123165
func (c *context) funcMap() template.FuncMap {
124166
return template.FuncMap{
125167
"param": c.param,
126168
"p": c.param,
127169
"in": c.in,
170+
"time": c.time,
171+
"now": c.now,
128172
}
129173
}
130174

0 commit comments

Comments
 (0)