-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
282 lines (248 loc) · 7.73 KB
/
utils.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package fireboltgosdk
import (
"database/sql/driver"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"runtime"
"strings"
"time"
"github.com/matishsiao/goInfo"
"github.com/xwb1989/sqlparser"
)
var goInfoFunc = goInfo.GetInfo
func getUseParametersList() []string {
return []string{"database", "engine"}
}
func getDisallowedParametersList() []string {
return []string{"output_format"}
}
func ConstructNestedError(message string, err error) error {
infolog.Printf("%s: %v", message, err)
return fmt.Errorf("%s: %v", message, err)
}
func validateSetStatement(key string) error {
for _, denyKey := range getUseParametersList() {
if key == denyKey {
return fmt.Errorf("could not set parameter. "+
"Set parameter '%s' is not allowed. "+
"Try again with 'USE %s' instead of SET", key, strings.ToUpper(key))
}
}
for _, denyKey := range getDisallowedParametersList() {
if key == denyKey {
return fmt.Errorf("could not set parameter. "+
"Set parameter '%s' is not allowed. "+
"Try again with a different parameter name", key)
}
}
return nil
}
// parseSetStatement parses a single set statement and returns a key-value pair,
// or returns an error, if it isn't a set statement
func parseSetStatement(query string) (string, string, error) {
query = strings.TrimSpace(query)
if strings.HasPrefix(strings.ToUpper(query), "SET") {
query = strings.TrimSpace(query[len("SET"):])
values := strings.Split(query, "=")
if len(values) < 2 {
return "", "", fmt.Errorf("not a valid set statement, didn't find '=' sign")
}
key := strings.TrimSpace(values[0])
value := strings.TrimSpace(values[1])
if key != "" && value != "" {
return key, value, nil
}
return "", "", fmt.Errorf("Either key or value is empty")
}
return "", "", fmt.Errorf("Not a set statement")
}
var infolog = log.New(os.Stderr, "[firebolt-go-sdk]", log.Ldate|log.Ltime|log.Lshortfile)
func init() {
infolog.SetOutput(ioutil.Discard)
}
// prepareStatement parses a query and substitude question marks with params
func prepareStatement(query string, params []driver.NamedValue) (string, error) {
r := sqlparser.NewStringTokenizer(query)
var positions []int
for {
tokenId, _ := r.Scan()
if tokenId == 0 {
break
}
if tokenId == sqlparser.VALUE_ARG {
positions = append(positions, r.Position-1)
}
}
if len(positions) != len(params) {
return "", fmt.Errorf("found '%d' value args in query, but '%d' arguments are provided", len(positions), len(params))
}
for i := len(positions) - 1; i >= 0; i -= 1 {
res, err := formatValue(params[i].Value)
if err != nil {
return "", err
}
query = query[:positions[i]-1] + res + query[positions[i]:]
}
return query, nil
}
// SplitStatements split multiple statements into a list of statements
func SplitStatements(sql string) ([]string, error) {
var queries []string
for sql != "" {
var err error
var query string
query, sql, err = sqlparser.SplitStatement(sql)
if err != nil {
return nil, ConstructNestedError("error during splitting query", err)
}
if strings.Trim(query, " \t\n") == "" {
continue
}
queries = append(queries, query)
}
return queries, nil
}
func formatValue(value driver.Value) (string, error) {
switch v := value.(type) {
case string:
res := value.(string)
res = strings.Replace(res, "\\", "\\\\", -1)
res = strings.Replace(res, "'", "\\'", -1)
return fmt.Sprintf("'%s'", res), nil
case int64, uint64, int32, uint32, int16, uint16, int8, uint8, int, uint:
return fmt.Sprintf("%d", value), nil
case float64, float32:
return fmt.Sprintf("%g", value), nil
case bool:
if value.(bool) {
return "true", nil
} else {
return "false", nil
}
case time.Time:
timeValue := value.(time.Time)
layout := "2006-01-02 15:04:05.000000"
// Subtract date part from value and check if remaining time part is zero
// If it is, use date only format
if timeValue.Sub(timeValue.Truncate(time.Hour*24)) == 0 {
layout = "2006-01-02"
} else if _, offset := timeValue.Zone(); offset != 0 {
// If we have a timezone info, add it to format
layout = "2006-01-02 15:04:05.000000-07:00"
}
return fmt.Sprintf("'%s'", timeValue.Format(layout)), nil
case []byte:
byteValue := value.([]byte)
parts := make([]string, len(byteValue))
for i, b := range byteValue {
parts[i] = fmt.Sprintf("\\x%02x", b)
}
return fmt.Sprintf("E'%s'", strings.Join(parts, "")), nil
case nil:
return "NULL", nil
default:
return "", fmt.Errorf("not supported type: %v", v)
}
}
// GetHostNameURL returns a hostname url, either default or overwritten with the environment variable
func GetHostNameURL() string {
if val := os.Getenv("FIREBOLT_ENDPOINT"); val != "" {
return makeCanonicalUrl(val)
}
return "https://api.app.firebolt.io"
}
// ConstructUserAgentString returns a string with go, GoSDK and os type and versions
// additionally user can set "FIREBOLT_GO_DRIVERS" and "FIREBOLT_GO_CLIENTS" env variable,
// and they will be concatenated with the final user-agent string
func ConstructUserAgentString() (ua_string string) {
defer func() {
// ConstructUserAgentString is a non-essential function, used for statistic gathering
// so carry on working if a failure occurs
if err := recover(); err != nil {
infolog.Printf("Unable to generate User Agent string")
ua_string = "GoSDK"
}
}()
osNameVersion := runtime.GOOS
if gi, err := goInfoFunc(); err == nil {
osNameVersion += " " + gi.Core
}
var isStringAllowed = regexp.MustCompile(`^[\w\d._\-/ ]+$`).MatchString
goDrivers := os.Getenv("FIREBOLT_GO_DRIVERS")
if !isStringAllowed(goDrivers) {
goDrivers = ""
}
goClients := os.Getenv("FIREBOLT_GO_CLIENTS")
if !isStringAllowed(goClients) {
goClients = ""
}
ua_string = strings.TrimSpace(fmt.Sprintf("%s GoSDK/%s (Go %s; %s) %s", goClients, sdkVersion, runtime.Version(), osNameVersion, goDrivers))
return ua_string
}
func valueToNamedValue(args []driver.Value) []driver.NamedValue {
namedValues := make([]driver.NamedValue, 0, len(args))
for i, arg := range args {
namedValues = append(namedValues, driver.NamedValue{Ordinal: i, Value: arg})
}
return namedValues
}
type StructuredError struct {
Message string
}
func (e StructuredError) Error() string {
return e.Message
}
func NewStructuredError(errorDetails []ErrorDetails) *StructuredError {
// "{severity}: {name} ({code}) - {source}, {description}, resolution: {resolution} at {location} see {helpLink}"
message := strings.Builder{}
for _, error := range errorDetails {
if message.Len() > 0 {
message.WriteString("\n")
}
formatErrorDetails(&message, error)
}
return &StructuredError{
Message: message.String(),
}
}
func formatErrorDetails(message *strings.Builder, error ErrorDetails) string {
if error.Severity != "" {
message.WriteString(fmt.Sprintf("%s: ", error.Severity))
}
if error.Name != "" {
message.WriteString(fmt.Sprintf("%s ", error.Name))
}
if error.Code != "" {
message.WriteString(fmt.Sprintf("(%s) ", error.Code))
}
if error.Description != "" {
addDelimiterIfNotEmpty(message, "-")
message.WriteString(error.Description)
}
if error.Source != "" {
addDelimiterIfNotEmpty(message, ",")
message.WriteString(error.Source)
}
if error.Resolution != "" {
addDelimiterIfNotEmpty(message, ",")
message.WriteString(fmt.Sprintf("resolution: %s", error.Resolution))
}
if error.Location.FailingLine != 0 || error.Location.StartOffset != 0 || error.Location.EndOffset != 0 {
addDelimiterIfNotEmpty(message, " at")
message.WriteString(fmt.Sprintf("%+v", error.Location))
}
if error.HelpLink != "" {
addDelimiterIfNotEmpty(message, ",")
message.WriteString(fmt.Sprintf("see %s", error.HelpLink))
}
return message.String()
}
func addDelimiterIfNotEmpty(message *strings.Builder, delimiter string) {
if message.Len() > 0 {
message.WriteString(delimiter)
message.WriteString(" ")
}
}