forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
146 lines (134 loc) · 3.64 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"strings"
"time"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
)
func boolToInt64(v bool) int64 {
if v {
return 1
}
return 0
}
// IsCurrentTimestampExpr returns whether e is CurrentTimestamp expression.
func IsCurrentTimestampExpr(e ast.ExprNode) bool {
if fn, ok := e.(*ast.FuncCallExpr); ok && fn.FnName.L == ast.CurrentTimestamp {
return true
}
return false
}
// GetTimeValue gets the time value with type tp.
func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d types.Datum, err error) {
value := types.Time{
Type: tp,
Fsp: fsp,
}
defaultTime, err := getSystemTimestamp(ctx)
if err != nil {
return d, errors.Trace(err)
}
sc := ctx.GetSessionVars().StmtCtx
switch x := v.(type) {
case string:
upperX := strings.ToUpper(x)
if upperX == strings.ToUpper(ast.CurrentTimestamp) {
value.Time = types.FromGoTime(defaultTime)
if tp == mysql.TypeTimestamp {
err = value.ConvertTimeZone(time.Local, ctx.GetSessionVars().GetTimeZone())
if err != nil {
return d, errors.Trace(err)
}
}
} else if upperX == types.ZeroDatetimeStr {
value, err = types.ParseTimeFromNum(sc, 0, tp, fsp)
terror.Log(errors.Trace(err))
} else {
value, err = types.ParseTime(sc, x, tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
}
case *ast.ValueExpr:
switch x.Kind() {
case types.KindString:
value, err = types.ParseTime(sc, x.GetString(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
case types.KindInt64:
value, err = types.ParseTimeFromNum(sc, x.GetInt64(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
case types.KindNull:
return d, nil
default:
return d, errors.Trace(errDefaultValue)
}
case *ast.FuncCallExpr:
if x.FnName.L == ast.CurrentTimestamp {
d.SetString(strings.ToUpper(ast.CurrentTimestamp))
return d, nil
}
return d, errors.Trace(errDefaultValue)
case *ast.UnaryOperationExpr:
// support some expression, like `-1`
v, err := EvalAstExpr(ctx, x)
if err != nil {
return d, errors.Trace(err)
}
ft := types.NewFieldType(mysql.TypeLonglong)
xval, err := v.ConvertTo(ctx.GetSessionVars().StmtCtx, ft)
if err != nil {
return d, errors.Trace(err)
}
value, err = types.ParseTimeFromNum(sc, xval.GetInt64(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
default:
return d, nil
}
d.SetMysqlTime(value)
return d, nil
}
func getSystemTimestamp(ctx sessionctx.Context) (time.Time, error) {
now := time.Now()
if ctx == nil {
return now, nil
}
sessionVars := ctx.GetSessionVars()
timestampStr, err := variable.GetSessionSystemVar(sessionVars, "timestamp")
if err != nil {
return now, errors.Trace(err)
}
if timestampStr == "" {
return now, nil
}
timestamp, err := types.StrToInt(sessionVars.StmtCtx, timestampStr)
if err != nil {
return time.Time{}, errors.Trace(err)
}
if timestamp <= 0 {
return now, nil
}
return time.Unix(timestamp, 0), nil
}