Skip to content

Commit

Permalink
Merge pull request #13 from pinzolo/time-funcs
Browse files Browse the repository at this point in the history
Time funcs
  • Loading branch information
pinzolo committed Jun 24, 2018
2 parents 70c35b8 + 10c7733 commit 0e3f2d5
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 230 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

## 2018-05-16 JST: v0.0.1

First implementation

## 2018-06-25 JST: v1.0.0

Add `time` and `now` template funcs.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ ORDER BY /*% .order %*/id

```go
// sql is generated SQL from template.
// vals are arguments for generated SQL.
sql, vals, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
// args are arguments for generated SQL.
sql, args, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
"ids": []int{1, 2, 3},
"order": "name DESC",
"onlyMale": false,
"name": "Alex",
})
rows, err := db.Query(sql, args...)
```

### Generated SQL
Expand Down Expand Up @@ -76,7 +77,7 @@ ORDER BY name DESC
$ go get github.com/pinzolo/sqlt
```

## Support
## Suppor

### Go version

Expand Down
46 changes: 45 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"text/template"
"time"
)

type param struct {
Expand All @@ -29,22 +30,28 @@ type context struct {
namedArgs []sql.NamedArg
values []interface{}
paramMap map[string]interface{}
timer *timer
}

func newContext(named bool, dialect Dialect, m map[string]interface{}) *context {
func newContext(named bool, dialect Dialect, timeFn func() time.Time, m map[string]interface{}) *context {
params := make([]*param, len(m))
i := 0
for k, v := range m {
params[i] = newParam(k, v)
i++
}
fn := time.Now
if timeFn != nil {
fn = timeFn
}
return &context{
named: named,
dialect: dialect,
params: params,
namedArgs: []sql.NamedArg{},
values: []interface{}{},
paramMap: m,
timer: newTimer(fn),
}
}

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

func (c *context) time() string {
name := "time__"
if c.named {
c.addNamed(name, c.timer.time())
return c.dialect.NamedPlaceholderPrefix() + name
}

if c.dialect.IsOrdinalPlaceholderSupported() {
if c.timer.cacheIndex == 0 {
c.values = append(c.values, c.timer.time())
c.timer.cacheIndex = len(c.values)
}
return c.dialect.OrdinalPlaceHolderPrefix() + strconv.Itoa(c.timer.cacheIndex)
}

c.values = append(c.values, c.timer.time())
return c.dialect.Placeholder()
}

func (c *context) now() string {
name := "now__" + strconv.Itoa(c.timer.nowCnt)
if c.named {
c.addNamed(name, c.timer.now())
return c.dialect.NamedPlaceholderPrefix() + name
}

if c.dialect.IsOrdinalPlaceholderSupported() {
c.values = append(c.values, c.timer.now())
return c.dialect.OrdinalPlaceHolderPrefix() + strconv.Itoa(len(c.values))
}

c.values = append(c.values, c.timer.now())
return c.dialect.Placeholder()
}

func (c *context) funcMap() template.FuncMap {
return template.FuncMap{
"param": c.param,
"p": c.param,
"in": c.in,
"time": c.time,
"now": c.now,
}
}

Expand Down
Loading

0 comments on commit 0e3f2d5

Please sign in to comment.