forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_parameters.go
More file actions
74 lines (63 loc) · 2.05 KB
/
query_parameters.go
File metadata and controls
74 lines (63 loc) · 2.05 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
73
74
package clickhouse
import (
"errors"
"regexp"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)
var (
ErrInvalidValueInNamedDateValue = errors.New("invalid value in NamedDateValue for query parameter")
ErrUnsupportedQueryParameter = errors.New("unsupported query parameter type")
hasQueryParamsRe = regexp.MustCompile("{.+:.+}")
)
func bindQueryOrAppendParameters(paramsProtocolSupport bool, options *QueryOptions, query string, timezone *time.Location, args ...any) (string, error) {
// prefer native query parameters over legacy bind if query parameters provided explicit
if len(options.parameters) > 0 {
return query, nil
}
// validate if query contains a {<name>:<data type>} syntax, so it's intentional use of query parameters
// parameter values will be loaded from `args ...any` for compatibility
if paramsProtocolSupport &&
len(args) > 0 &&
hasQueryParamsRe.MatchString(query) {
options.parameters = make(Parameters, len(args))
for _, a := range args {
switch p := a.(type) {
case driver.NamedValue:
if str, ok := p.Value.(string); ok {
options.parameters[p.Name] = str
continue
}
// using the same format logic for NamedValue typed value in function bindNamed
strVal, err := format(timezone, Seconds, p.Value)
if err != nil {
return "", err
}
options.parameters[p.Name] = strVal
case driver.NamedDateValue:
if !p.Value.IsZero() && p.Name != "" {
formatted := formatTimeWithScale(p.Value, TimeUnit(p.Scale))
options.parameters[p.Name] = formatted
continue
}
return "", ErrInvalidValueInNamedDateValue
default:
return "", ErrUnsupportedQueryParameter
}
}
return query, nil
}
return bind(timezone, query, args...)
}
func formatTimeWithScale(t time.Time, scale TimeUnit) string {
switch scale {
case MicroSeconds:
return t.Format("2006-01-02 15:04:05.000000")
case MilliSeconds:
return t.Format("2006-01-02 15:04:05.000")
case NanoSeconds:
return t.Format("2006-01-02 15:04:05.000000000")
default:
return t.Format("2006-01-02 15:04:05")
}
}