-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathgeneric.go
260 lines (210 loc) · 7.2 KB
/
generic.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package snowflake
import (
"bytes"
"fmt"
"strings"
"text/template"
)
type EntityType string
const (
ApiIntegrationType EntityType = "API INTEGRATION"
DatabaseType EntityType = "DATABASE"
ManagedAccountType EntityType = "MANAGED ACCOUNT"
ResourceMonitorType EntityType = "RESOURCE MONITOR"
RoleType EntityType = "ROLE"
ShareType EntityType = "SHARE"
StorageIntegrationType EntityType = "STORAGE INTEGRATION"
NotificationIntegrationType EntityType = "NOTIFICATION INTEGRATION"
SecurityIntegrationType EntityType = "SECURITY INTEGRATION"
UserType EntityType = "USER"
WarehouseType EntityType = "WAREHOUSE"
)
type Builder struct {
entityType EntityType
name string
}
func (b *Builder) Show() string {
return fmt.Sprintf(`SHOW %sS LIKE '%s'`, b.entityType, b.name)
}
func (b *Builder) Describe() string {
return fmt.Sprintf(`DESCRIBE %s "%s"`, b.entityType, b.name)
}
func (b *Builder) Drop() string {
return fmt.Sprintf(`DROP %s "%s"`, b.entityType, b.name)
}
func (b *Builder) Rename(newName string) string {
return fmt.Sprintf(`ALTER %s "%s" RENAME TO "%s"`, b.entityType, b.name, newName)
}
// SettingBuilder is an interface for a builder that allows you to set key value pairs
type SettingBuilder interface {
SetString(string, string)
SetStringList(string, []string)
SetBool(string, bool)
SetInt(string, int)
SetFloat(string, float64)
SetRaw(string)
}
type AlterPropertiesBuilder struct {
name string
entityType EntityType
stringProperties map[string]string
stringListProperties map[string][]string
boolProperties map[string]bool
intProperties map[string]int
floatProperties map[string]float64
rawStatement string
tags []TagValue
}
func (b *Builder) Alter() *AlterPropertiesBuilder {
return &AlterPropertiesBuilder{
name: b.name,
entityType: b.entityType,
stringProperties: make(map[string]string),
stringListProperties: make(map[string][]string),
boolProperties: make(map[string]bool),
intProperties: make(map[string]int),
floatProperties: make(map[string]float64),
}
}
func (ab *AlterPropertiesBuilder) SetString(key, value string) {
ab.stringProperties[key] = value
}
func (ab *AlterPropertiesBuilder) SetStringList(key string, value []string) {
ab.stringListProperties[key] = value
}
func (ab *AlterPropertiesBuilder) SetBool(key string, value bool) {
ab.boolProperties[key] = value
}
func (ab *AlterPropertiesBuilder) SetInt(key string, value int) {
ab.intProperties[key] = value
}
func (ab *AlterPropertiesBuilder) SetFloat(key string, value float64) {
ab.floatProperties[key] = value
}
func (ab *AlterPropertiesBuilder) SetRaw(rawStatement string) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf(`%s %s`, ab.rawStatement, rawStatement))
ab.rawStatement = sb.String()
}
func (b *AlterPropertiesBuilder) SetTags(tags []TagValue) {
b.tags = tags
}
func (ab *AlterPropertiesBuilder) Statement() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf(`ALTER %s "%s" SET`, ab.entityType, ab.name)) // TODO handle error
sb.WriteString(ab.rawStatement)
for _, k := range sortStrings(ab.stringProperties) {
sb.WriteString(fmt.Sprintf(" %s='%s'", strings.ToUpper(k), EscapeString(ab.stringProperties[k])))
}
for _, k := range sortStringList(ab.stringListProperties) {
sb.WriteString(fmt.Sprintf(" %s=%s", strings.ToUpper(k), formatStringList(ab.stringListProperties[k])))
}
for _, k := range sortStringsBool(ab.boolProperties) {
sb.WriteString(fmt.Sprintf(" %s=%t", strings.ToUpper(k), ab.boolProperties[k]))
}
for _, k := range sortStringsInt(ab.intProperties) {
sb.WriteString(fmt.Sprintf(" %s=%d", strings.ToUpper(k), ab.intProperties[k]))
}
for _, k := range sortStringsFloat(ab.floatProperties) {
sb.WriteString(fmt.Sprintf(" %s=%.2f", strings.ToUpper(k), ab.floatProperties[k]))
}
for _, t := range sortTags(ab.tags) {
sb.WriteString(fmt.Sprintf(` TAG "%v"."%v"."%v" = "%v"`, t.Database, t.Schema, t.Name, t.Value))
}
return sb.String()
}
type CreateBuilder struct {
name string
entityType EntityType
stringProperties map[string]string
stringListProperties map[string][]string
boolProperties map[string]bool
intProperties map[string]int
floatProperties map[string]float64
rawStatement string
tags []TagValue
}
func (b *Builder) Create() *CreateBuilder {
return &CreateBuilder{
name: b.name,
entityType: b.entityType,
stringProperties: make(map[string]string),
stringListProperties: make(map[string][]string),
boolProperties: make(map[string]bool),
intProperties: make(map[string]int),
floatProperties: make(map[string]float64),
}
}
func (b *CreateBuilder) SetString(key, value string) {
b.stringProperties[key] = value
}
func (b *CreateBuilder) SetStringList(key string, value []string) {
b.stringListProperties[key] = value
}
func (b *CreateBuilder) SetBool(key string, value bool) {
b.boolProperties[key] = value
}
func (b *CreateBuilder) SetInt(key string, value int) {
b.intProperties[key] = value
}
func (b *CreateBuilder) SetFloat(key string, value float64) {
b.floatProperties[key] = value
}
func (b *CreateBuilder) SetRaw(rawStatement string) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf(`%s %s`, b.rawStatement, rawStatement))
b.rawStatement = sb.String()
}
func (b *CreateBuilder) SetTags(tags []TagValue) {
b.tags = tags
}
func (b *CreateBuilder) Statement() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf(`CREATE %s "%s"`, b.entityType, b.name)) // TODO handle error
sb.WriteString(b.rawStatement)
for _, k := range sortStrings(b.stringProperties) {
sb.WriteString(fmt.Sprintf(" %s='%s'", strings.ToUpper(k), EscapeString(b.stringProperties[k])))
}
for _, k := range sortStringList(b.stringListProperties) {
sb.WriteString(fmt.Sprintf(" %s=%s", strings.ToUpper(k), formatStringList(b.stringListProperties[k])))
}
for _, k := range sortStringsBool(b.boolProperties) {
sb.WriteString(fmt.Sprintf(" %s=%t", strings.ToUpper(k), b.boolProperties[k]))
}
for _, k := range sortStringsInt(b.intProperties) {
sb.WriteString(fmt.Sprintf(" %s=%d", strings.ToUpper(k), b.intProperties[k]))
}
for _, k := range sortStringsFloat(b.floatProperties) {
sb.WriteString(fmt.Sprintf(" %s=%.2f", strings.ToUpper(k), b.floatProperties[k]))
}
for i, t := range sortTags(b.tags) {
if i == 0 {
sb.WriteString(` TAG`)
} else if i < len(b.tags)-1 {
sb.WriteString(`, `)
}
sb.WriteString(fmt.Sprintf(`"%v"."%v"."%v" = "%v"`, t.Database, t.Schema, t.Name, t.Value))
}
return sb.String()
}
func formatStringList(list []string) string {
t, err := template.New("StringList").Funcs(template.FuncMap{
"escapeString": EscapeString,
}).Parse(`({{ range $i, $v := .}}{{ if $i }}, {{ end }}'{{ escapeString $v }}'{{ end }})`)
if err != nil {
return ""
}
var buf bytes.Buffer
if err := t.Execute(&buf, list); err != nil {
return ""
}
return buf.String()
}
func Contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}