Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 9cdcea0

Browse files
authored
Merge pull request #568 from erizocosmico/feature/now
function: implement NOW function
2 parents 12091f6 + 8747984 commit 9cdcea0

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ We support and actively test against certain third-party clients to ensure compa
6565
- `SUBSTRING(str, pos)`, `SUBSTRING(str, pos, len)` : Return a substring from the provided string.
6666
- `SUBSTR(str, pos)`, `SUBSTR(str, pos, len)` : Return a substring from the provided string.
6767
- `MID(str, pos)`, `MID(str, pos, len)` : Return a substring from the provided string.
68-
- Date and Timestamp functions: `YEAR(date)`, `MONTH(date)`, `DAY(date)`, `WEEKDAY(date)`, `HOUR(date)`, `MINUTE(date)`, `SECOND(date)`, `DAYOFWEEK(date)`, `DAYOFYEAR(date)`.
68+
- Date and Timestamp functions: `YEAR(date)`, `MONTH(date)`, `DAY(date)`, `WEEKDAY(date)`, `HOUR(date)`, `MINUTE(date)`, `SECOND(date)`, `DAYOFWEEK(date)`, `DAYOFYEAR(date)`, `NOW()`.
6969
- `ARRAY_LENGTH(json)`: If the json representation is an array, this function returns its size.
7070
- `SPLIT(str,sep)`: Receives a string and a separator and returns the parts of the string split by the separator as a JSON array of strings.
7171
- `CONCAT(...)`: Concatenate any group of fields into a single string.

SUPPORTED.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@
120120
- MONTH
121121
- SECOND
122122
- YEAR
123+
- NOW

sql/expression/function/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ var Defaults = sql.Functions{
6868
"replace": sql.Function3(NewReplace),
6969
"ifnull": sql.Function2(NewIfNull),
7070
"nullif": sql.Function2(NewNullIf),
71+
"now": sql.Function0(NewNow),
7172
}

sql/expression/function/time.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,41 @@ var (
327327
dayOfWeek = datePartFunc(func(t time.Time) int { return int(t.Weekday()) + 1 })
328328
dayOfYear = datePartFunc((time.Time).YearDay)
329329
)
330+
331+
type clock func() time.Time
332+
333+
var defaultClock = time.Now
334+
335+
// Now is a function that returns the current time.
336+
type Now struct {
337+
clock
338+
}
339+
340+
// NewNow returns a new Now node.
341+
func NewNow() sql.Expression {
342+
return &Now{defaultClock}
343+
}
344+
345+
// Type implements the sql.Expression interface.
346+
func (*Now) Type() sql.Type { return sql.Timestamp }
347+
348+
func (*Now) String() string { return "NOW()" }
349+
350+
// IsNullable implements the sql.Expression interface.
351+
func (*Now) IsNullable() bool { return false }
352+
353+
// Resolved implements the sql.Expression interface.
354+
func (*Now) Resolved() bool { return true }
355+
356+
// Children implements the sql.Expression interface.
357+
func (*Now) Children() []sql.Expression { return nil }
358+
359+
// Eval implements the sql.Expression interface.
360+
func (n *Now) Eval(*sql.Context, sql.Row) (interface{}, error) {
361+
return n.clock(), nil
362+
}
363+
364+
// TransformUp implements the sql.Expression interface.
365+
func (n *Now) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {
366+
return f(n)
367+
}

sql/expression/function/time_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,16 @@ func TestTime_DayOfYear(t *testing.T) {
291291
})
292292
}
293293
}
294+
295+
func TestNow(t *testing.T) {
296+
require := require.New(t)
297+
date := time.Date(2018, time.December, 2, 16, 25, 0, 0, time.Local)
298+
clock := clock(func() time.Time {
299+
return date
300+
})
301+
f := &Now{clock}
302+
303+
result, err := f.Eval(nil, nil)
304+
require.NoError(err)
305+
require.Equal(date, result)
306+
}

0 commit comments

Comments
 (0)