forked from temporalio/temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
72 lines (60 loc) · 1.63 KB
/
query.go
File metadata and controls
72 lines (60 loc) · 1.63 KB
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
package sqlquery
import (
"fmt"
"strconv"
"strings"
"time"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/primitives/timestamp"
)
const (
QueryTemplate = "select * from dummy where %s"
DefaultDateTimeFormat = time.RFC3339
)
func ConvertToTime(timeStr string) (time.Time, error) {
ts, err := strconv.ParseInt(timeStr, 10, 64)
if err == nil {
return timestamp.UnixOrZeroTime(ts), nil
}
timestampStr, err := ExtractStringValue(timeStr)
if err != nil {
return time.Time{}, err
}
parsedTime, err := time.Parse(DefaultDateTimeFormat, timestampStr)
if err != nil {
return time.Time{}, err
}
return parsedTime, nil
}
func ExtractStringValue(s string) (string, error) {
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
return s[1 : len(s)-1], nil
}
return "", fmt.Errorf("value %s is not a string value", s)
}
func ExtractIntValue(s string) (int, error) {
intValue, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return intValue, nil
}
// ParseValue returns a string, int64 or float64 if the parsing succeeds.
func ParseValue(sqlValue string) (interface{}, error) {
if sqlValue == "" {
return "", nil
}
if sqlValue[0] == '\'' && sqlValue[len(sqlValue)-1] == '\'' {
strValue := strings.Trim(sqlValue, "'")
return strValue, nil
}
// Unquoted value must be a number. Try int64 first.
if intValue, err := strconv.ParseInt(sqlValue, 10, 64); err == nil {
return intValue, nil
}
// Then float64.
if floatValue, err := strconv.ParseFloat(sqlValue, 64); err == nil {
return floatValue, nil
}
return nil, serviceerror.NewInvalidArgumentf("invalid expression: unable to parse %s", sqlValue)
}