-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
210 lines (185 loc) · 4.22 KB
/
template.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package influxql
import (
"fmt"
"regexp"
"strings"
"text/template"
"time"
)
const placeholder = "?"
type nullValue struct{}
var (
reWhiteChars = regexp.MustCompile(`[\s\t\r\n]+`)
reSpacesBetweenTags = regexp.MustCompile(`}}[\s\t\r\n]+{{`)
)
var (
orKeyword = &keyword{"OR"}
andKeyword = &keyword{"AND"}
)
func cleanTemplate(s string) string {
s = reWhiteChars.ReplaceAllString(s, " ")
s = reSpacesBetweenTags.ReplaceAllString(s, "}}{{")
return strings.TrimSpace(s)
}
const selectTemplateText = `
SELECT
{{if .Fields}}
{{.Fields | joinWithCommas}}
{{else}}
*
{{end}}
FROM
{{.Measurement}}
{{if .Where}}
WHERE
{{.Where | joinWithSpace }}
{{end}}
{{if .GroupBy}}
GROUP BY
{{.GroupBy | joinWithCommas }}
{{end}}
{{if .Fill}} fill({{.Fill}}){{end}}
`
type selectTemplateValues struct {
Measurement string
Fields []string
Where []string
GroupBy []string
Fill string
}
func joinWithCommas(in []string) string {
return strings.Join(in, ", ")
}
func joinWithSpace(in []string) string {
return strings.Join(in, " ")
}
var selectTemplate = template.Must(
template.New("select").Funcs(
map[string]interface{}{
"joinWithCommas": joinWithCommas,
"joinWithSpace": joinWithSpace,
},
).Parse(cleanTemplate(selectTemplateText)),
)
type keyword struct {
v string
}
func (k *keyword) Build() (string, error) {
return k.v, nil
}
// Expr represents an expression.
type Expr struct {
expr string
values []interface{}
}
// Build satisfies Builder.
func (e *Expr) Build() (string, error) {
placeholders := strings.Count(e.expr, placeholder)
if placeholders > 0 {
// Where("foo = ?", "bar")
if placeholders != len(e.values) {
return "", fmt.Errorf("Mismatched number of placeholders (%d) and values (%d)", strings.Count(e.expr, placeholder), len(e.values))
}
} else {
if len(e.values) > 0 {
parts := strings.Split(strings.TrimSpace(reWhiteChars.ReplaceAllString(e.expr, " ")), " ")
lparts := len(parts)
if lparts < 1 {
return "", fmt.Errorf("Expecting statement.")
} else if lparts < 2 {
// Where("foo", "bar")
if len(e.values) != 1 {
return "", fmt.Errorf("Expecting exactly one value.")
}
e.expr = fmt.Sprintf("%q = ?", parts[0])
} else if lparts < 3 {
// Where("foo =", "bar")
if len(e.values) != 1 {
return "", fmt.Errorf("Expecting exactly one value.")
}
e.expr = fmt.Sprintf("%q %s ?", parts[0], parts[1])
} else {
return "", fmt.Errorf("Unsupported expression %q", e.expr)
}
}
}
compiled := make([]interface{}, 0, len(e.values))
for i := range e.values {
lit := &value{e.values[i]}
c, err := lit.Build()
if err != nil {
return "", err
}
compiled = append(compiled, c)
}
s := strings.Replace(e.expr, "?", "%s", -1)
return fmt.Sprintf(s, compiled...), nil
}
type value struct {
v interface{}
}
func (v *value) Build() (string, error) {
switch t := v.v.(type) {
case string:
return fmt.Sprintf(`'%s'`, t), nil
case int:
return fmt.Sprintf("%d", t), nil
case uint:
return fmt.Sprintf("%d", t), nil
case int64:
return fmt.Sprintf("%d", t), nil
case uint64:
return fmt.Sprintf("%d", t), nil
case int32:
return fmt.Sprintf("%d", t), nil
case uint32:
return fmt.Sprintf("%d", t), nil
case int8:
return fmt.Sprintf("%d", t), nil
case uint8:
return fmt.Sprintf("%d", t), nil
case time.Time:
return fmt.Sprintf(`'%s'`, t.Format("2006-01-02T15:04:05Z")), nil
case time.Duration:
return timeFormat(t), nil
default:
return fmt.Sprintf(`'%v'`, t), nil
}
panic("reached")
}
type literal struct {
v interface{}
}
func (l *literal) Build() (string, error) {
switch v := l.v.(type) {
case Builder:
return v.Build()
case time.Duration:
t := Time(v)
return t.Build()
case string:
if strings.ContainsAny(v, `".`) {
return fmt.Sprintf(`%s`, v), nil
}
return fmt.Sprintf(`%q`, v), nil
default:
return fmt.Sprintf(`"%v"`, v), nil
}
panic("reached")
}
func compileInto(src Builder, dst *string) (err error) {
*dst, err = src.Build()
return
}
func compileArrayInto(src []Builder, dst *[]string) error {
v := make([]string, 0, len(src))
for i := range src {
s, err := src[i].Build()
if err != nil {
return err
}
v = append(v, s)
}
*dst = v
return nil
}