Skip to content

Commit

Permalink
builtin/time: support CURRENT_TIME(), CURTIME()
Browse files Browse the repository at this point in the history
For github issue pingcap#236.
  • Loading branch information
chenyanzhe committed Dec 19, 2015
1 parent 60ec9fc commit 1b0c2e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions expression/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ var Funcs = map[string]Func{
// time functions
"curdate": {builtinCurrentDate, 0, 0, false, false},
"current_date": {builtinCurrentDate, 0, 0, false, false},
"current_time": {builtinCurrentTime, 0, 0, false, false},
"current_timestamp": {builtinNow, 0, 1, false, false},
"curtime": {builtinCurrentTime, 0, 0, false, false},
"date": {builtinDate, 8, 8, true, false},
"day": {builtinDay, 1, 1, true, false},
"dayofmonth": {builtinDayOfMonth, 1, 1, true, false},
Expand Down
7 changes: 7 additions & 0 deletions expression/builtin/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ func builtinCurrentDate(args []interface{}, ctx map[interface{}]interface{}) (in
Type: mysql.TypeDate, Fsp: 0}, nil
}

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_curtime
func builtinCurrentTime(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
return mysql.Time{
Time: time.Now(),
Type: mysql.TypeDuration, Fsp: 0}, nil
}

func checkFsp(arg interface{}) (int, error) {
fsp, err := types.ToInt64(arg)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions mysql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var (

// Time format without fractional seconds precision.
const (
DateFormat = "2006-01-02"
TimeFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
CurtimeFormat = "15:04:05"
TimeFormat = "2006-01-02 15:04:05"
// TimeFSPFormat is time format with fractional seconds precision.
TimeFSPFormat = "2006-01-02 15:04:05.000000"
)
Expand Down Expand Up @@ -125,6 +126,10 @@ func (t Time) String() string {
return t.Time.Format(DateFormat)
}

if t.Type == TypeDuration {
return t.Time.Format(CurtimeFormat)
}

tfStr := TimeFormat
if t.Fsp > 0 {
tfStr = fmt.Sprintf("%s.%s", tfStr, strings.Repeat("0", t.Fsp))
Expand Down
10 changes: 10 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ import (
cross "CROSS"
curDate "CURDATE"
currentDate "CURRENT_DATE"
curTime "CURTIME"
currentTime "CURRENT_TIME"
currentUser "CURRENT_USER"
database "DATABASE"
databases "DATABASES"
Expand Down Expand Up @@ -2137,6 +2139,14 @@ FunctionCallNonKeyword:
{
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string))}
}
| "CURTIME" '(' ')'
{
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string))}
}
| "CURRENT_TIME" '(' ')'
{
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string))}
}
| "CURRENT_TIMESTAMP" FuncDatetimePrec
{
args := []ast.ExprNode{}
Expand Down
6 changes: 6 additions & 0 deletions parser/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ create {c}{r}{e}{a}{t}{e}
cross {c}{r}{o}{s}{s}
curdate {c}{u}{r}{d}{a}{t}{e}
current_date {c}{u}{r}{r}{e}{n}{t}_{d}{a}{t}{e}
curtime {c}{u}{r}{t}{i}{m}{e}
current_time {c}{u}{r}{r}{e}{n}{t}_{t}{i}{m}{e}
current_user {c}{u}{r}{r}{e}{n}{t}_{u}{s}{e}{r}
database {d}{a}{t}{a}{b}{a}{s}{e}
databases {d}{a}{t}{a}{b}{a}{s}{e}{s}
Expand Down Expand Up @@ -692,6 +694,10 @@ year_month {y}{e}{a}{r}_{m}{o}{n}{t}{h}
return curDate
{current_date} lval.item = string(l.val)
return currentDate
{curtime} lval.item = string(l.val)
return curTime
{current_time} lval.item = string(l.val)
return currentTime
{current_user} lval.item = string(l.val)
return currentUser
{database} lval.item = string(l.val)
Expand Down

0 comments on commit 1b0c2e3

Please sign in to comment.