forked from acronis/perfkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
353 lines (302 loc) · 8.61 KB
/
helpers.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package db
import (
"fmt"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
// TernaryStr returns trueVal if cond is true, falseVal otherwise
func TernaryStr(cond bool, trueVal, falseVal string) string {
if cond {
return trueVal
}
return falseVal
}
// tryCastToString tries to cast given interface to string
func tryCastToString(i interface{}) (string, bool) {
result := ""
chars, ok := i.([]uint8)
if !ok {
return "", false
}
for _, c := range chars {
if c < 32 || c > 126 {
return "", false
}
result += string(rune(c))
}
return "'" + result + "'", true
}
// DumpRecursive returns string representation of given interface
func DumpRecursive(i interface{}, indent string) string {
val := reflect.ValueOf(i)
if !val.IsValid() {
return "nil"
}
if !val.CanInterface() {
return "?"
}
typ := val.Type()
switch val.Kind() {
case reflect.String:
return fmt.Sprintf("%q", val.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(val.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(val.Uint(), 10)
case reflect.Bool:
return strconv.FormatBool(val.Bool())
case reflect.Slice, reflect.Array:
var result []string
for i := 0; i < val.Len(); i++ {
s, ok := tryCastToString(val.Index(i).Interface())
if ok {
result = append(result, s)
} else {
result = append(result, DumpRecursive(val.Index(i).Interface(), indent+" "))
}
}
return "[" + strings.Join(result, ", ") + "]"
case reflect.Struct:
var result []string
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if field.CanInterface() {
result = append(result, indent+typ.Field(i).Name+" => "+DumpRecursive(val.Field(i).Interface(), indent+" "))
} else {
result = append(result, indent+"??? => ???")
}
}
return strings.Join(result, "\n")
case reflect.Map:
keys := val.MapKeys()
var result []string
for _, key := range keys {
result = append(result, indent+fmt.Sprintf("%v", key.Interface())+" => "+DumpRecursive(val.MapIndex(key).Interface(), indent+" "))
}
return strings.Join(result, "\n")
case reflect.Ptr:
switch typ.Elem().Kind() {
case reflect.String:
return fmt.Sprintf("%s", i)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
return strconv.Itoa(int(*i.(*int32)))
case reflect.Int64:
return strconv.FormatInt(*i.(*int64), 10)
default:
return fmt.Sprintf("%v", val.Interface())
}
default:
return fmt.Sprintf("%v", val.Interface())
}
}
// DefaultCreateQueryPatchFunc returns function that replaces placeholders in query with values from given table, sql_driver and sql_engine
func DefaultCreateQueryPatchFunc(table string, query string, dialect Dialect) (string, error) {
query = strings.ReplaceAll(query, "{table}", table)
for _, logicalType := range []DataType{
DataTypeBigInt,
DataTypeBigIntAutoIncPK,
DataTypeBigIntAutoInc,
DataTypeVarChar,
DataTypeVarChar256,
DataTypeText,
DataTypeAscii,
DataTypeUUID,
DataTypeVarCharUUID,
DataTypeTenantUUIDBoundID,
DataTypeLongBlob,
DataTypeHugeBlob,
DataTypeDateTime,
DataTypeDateTime6,
DataTypeTimestamp6,
DataTypeCurrentTimeStamp6,
DataTypeBinary20,
DataTypeBinaryBlobType,
DataTypeBoolean,
DataTypeBooleanFalse,
DataTypeBooleanTrue,
DataTypeTinyInt,
DataTypeLongText,
DataTypeUnique,
DataTypeNotNull,
DataTypeNull,
DataTypeVector3Float32,
DataTypeVector768Float32,
DataTypeEngine,
} {
var specificType = dialect.GetType(logicalType)
query = strings.ReplaceAll(query, string(logicalType), specificType)
}
return query, nil
}
var kb = int64(1024)
// StringToBytes converts string to bytes
func StringToBytes(str string) (int64, error) {
multipliers := map[string]int64{
"K": kb,
"KB": kb,
"M": kb * kb,
"MB": kb * kb,
"G": kb * kb * kb,
"GB": kb * kb * kb,
"T": kb * kb * kb * kb,
"TB": kb * kb * kb * kb,
"P": kb * kb * kb * kb * kb,
"PB": kb * kb * kb * kb * kb,
}
if str == "" {
return 0, fmt.Errorf("empty string") //nolint:perfsprint
}
for suffix, multiplier := range multipliers {
if strings.HasSuffix(str, suffix) {
s := str[:len(str)-len(suffix)]
number, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, fmt.Errorf("error parsing '%v': %w", s, err)
}
return number * multiplier, nil
}
}
number, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("error parsing '%v': %w", str, err)
}
return number, nil
}
// PrintStack prints stack trace
func PrintStack() {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
fmt.Printf("=== STACK TRACE ===\n%s\n", buf[:n])
}
// WithAutoInc returns true if DBDriver should support 'autoinc' field as current time nanoseconds
func WithAutoInc(name DialectName) bool {
switch name {
case CASSANDRA:
return true
default:
return false
}
}
func DedupStrings(ss []string) []string {
var idx = map[string]struct{}{}
var dd []string
for _, s := range ss {
if _, ok := idx[s]; !ok {
idx[s] = struct{}{}
dd = append(dd, s)
}
}
return dd
}
func ParseFunc(s string) (fName string, arg string, err error) {
argOpen, argClose := strings.Index(s, "("), strings.Index(s, ")")
if argOpen == -1 && argClose == -1 {
return "", s, nil
}
if argOpen == -1 {
return "", "", fmt.Errorf("bad function '%v', no opening bracket", s)
}
if argClose == -1 {
return "", "", fmt.Errorf("bad function '%v', no closing bracket", s)
}
if argClose <= argOpen {
return "", "", fmt.Errorf("bad function '%v', closing bracket placed before opening bracket", s)
}
return s[:argOpen], s[argOpen+1 : argClose], nil
}
func ParseFuncMultipleArgs(s string, sep string) (fName string, args []string, err error) {
argOpen, argClose := strings.Index(s, "("), strings.Index(s, ")")
if argOpen == -1 && argClose == -1 {
return "", strings.Split(s, sep), nil
}
if argOpen == -1 {
return "", nil, fmt.Errorf("bad function '%v', no opening bracket", s)
}
if argClose == -1 {
return "", nil, fmt.Errorf("bad function '%v', no closing bracket", s)
}
if argClose <= argOpen {
return "", nil, fmt.Errorf("bad function '%v', closing bracket placed before opening bracket", s)
}
return s[:argOpen], strings.Split(s[argOpen+1:argClose], sep), nil
}
func ParseVector(s string, sep string) (args []string, err error) {
argOpen, argClose := strings.Index(s, "["), strings.Index(s, "]")
if argOpen == -1 && argClose == -1 {
return strings.Split(s, sep), nil
}
if argOpen == -1 {
return nil, fmt.Errorf("bad vector '%v', no opening bracket", s)
}
if argClose == -1 {
return nil, fmt.Errorf("bad vector '%v', no closing bracket", s)
}
if argClose <= argOpen {
return nil, fmt.Errorf("bad function '%v', closing bracket placed before opening bracket", s)
}
return strings.Split(s[argOpen+1:argClose], sep), nil
}
func ParseTimeUTC(s string) (time.Time, error) {
if s == "" {
return time.Time{}, fmt.Errorf("empty time value")
}
if strings.HasSuffix(s, "ns") {
ns, err := strconv.ParseInt(strings.TrimSuffix(s, "ns"), 10, 64)
if err != nil {
return time.Time{}, fmt.Errorf("incorrect UNIX-TIMESTAMP-NANO format")
}
return time.Unix(0, ns).UTC(), nil
}
sec, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return time.Unix(sec, 0).UTC(), nil
}
var t time.Time
if t, err = time.Parse(time.RFC3339, s); err != nil {
if t, err = time.Parse(time.RFC1123, s); err != nil {
if t, err = time.Parse(time.RFC850, s); err != nil {
if t, err = time.Parse(time.ANSIC, s); err != nil {
return time.Time{}, fmt.Errorf("incorrect time format, must be one of (UNIX-TIMESTAMP-NANO, UNIX-TIMESTAMP, RFC3339, RFC1123, RFC850, ANSI-C)")
}
}
}
}
return t.UTC(), nil
}
func ParseScheme(s string) (scheme string, uri string, err error) {
const schemeSeparator = "://"
parts := strings.Split(s, schemeSeparator)
if len(parts) != 2 {
return "", "", fmt.Errorf("'%s' is invalid scheme separator", schemeSeparator)
}
return parts[0], parts[1], nil
}
// Cond represents a condition
type Cond struct {
Col string
Vals []string
}
// SortFields sorts fields by column name
func SortFields(fields map[string][]string) []Cond {
var cs []Cond
for k, v := range fields {
cs = append(cs, Cond{k, v})
}
sort.Slice(cs, func(i, j int) bool {
return cs[i].Col < cs[j].Col
})
return cs
}
// GenDBParameterPlaceholders generates placeholders for given start and count
func GenDBParameterPlaceholders(start int, count int) string {
var ret = make([]string, count)
end := start + count
for i := start; i < end; i++ {
ret[i-start] = fmt.Sprintf("$%d", i+1)
}
return strings.Join(ret, ",")
}