Skip to content

Commit

Permalink
fn: add now() function
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Nov 28, 2023
1 parent 986a493 commit 9992339
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/expr/functions/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"fmt"
"time"

"github.com/cockroachdb/errors"
"github.com/genjidb/genji/document"
Expand Down Expand Up @@ -74,6 +75,13 @@ var builtinFunctions = Definitions{
return &Coalesce{Exprs: args}, nil
},
},
"now": &definition{
name: "now",
arity: 0,
constructorFn: func(args ...expr.Expr) (expr.Function, error) {
return &Now{}, nil
},
},
}

// BuiltinDefinitions returns a map of builtin functions.
Expand Down Expand Up @@ -749,3 +757,24 @@ func (c *Coalesce) String() string {
func (c *Coalesce) Params() []expr.Expr {
return c.Exprs
}

type Now struct{}

func (n *Now) Eval(env *environment.Environment) (types.Value, error) {
return types.NewTimestampValue(time.Now()), nil
}

func (n *Now) IsEqual(other expr.Expr) bool {
if other == nil {
return false
}

_, ok := other.(*Now)
return ok
}

func (n *Now) Params() []expr.Expr { return nil }

func (n *Now) String() string {
return "NOW()"
}
4 changes: 4 additions & 0 deletions internal/expr/functions/testdata/builtin_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@

> typeof(NULL)
'null'

-- test: now
> typeof(now())
'timestamp'

0 comments on commit 9992339

Please sign in to comment.