-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
265 lines (247 loc) · 9.15 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
package godynamo
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/btnguyen2k/consu/g18"
"regexp"
"strconv"
"strings"
"time"
)
var (
reSqlInsert = regexp.MustCompile(`(?is)^INSERT\s+INTO\s+` + field + `\s*\(([^)]*?)\)\s*VALUES\s*\(([^)]*?)\)$`)
ErrNotValidInsertStm = errors.New("input is not an invalid INSERT statement")
ErrFieldsAndValuesNotMatch = errors.New("number of fields and values mismatch")
reValNull = regexp.MustCompile(`(?i)(null)\s*,?`)
reValNumber = regexp.MustCompile(`([\d\.xe+-]+)\s*,?`)
reValBoolean = regexp.MustCompile(`(?i)(true|false)\s*,?`)
reValStringDoubleQuoted = regexp.MustCompile(`(?i)("(\\"|[^"])*?")\s*,?`)
reValStringSingleQuoted = regexp.MustCompile(`(?i)'(?:[^']+|'')*'\s*,?`)
reValPlaceholder = regexp.MustCompile(`(?i)\?\s*,?`)
reValRaw = regexp.MustCompile(`(?i)([^,]+)\s*,?`)
)
type valPlaceholder struct{}
func _isSpace(r rune) bool {
switch r {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0, '=':
return true
default:
return false
}
}
func _parseValue(input string, separator rune) (value interface{}, leftOver string, err error) {
if loc := reValPlaceholder.FindStringIndex(input); loc != nil && loc[0] == 0 {
return valPlaceholder{}, input[loc[1]:], nil
}
if loc := reValNull.FindStringIndex(input); loc != nil && loc[0] == 0 {
return nil, input[loc[1]:], nil
}
if loc := reValNumber.FindStringIndex(input); loc != nil && loc[0] == 0 {
token := strings.TrimFunc(input[loc[0]:loc[1]], func(r rune) bool { return _isSpace(r) || r == separator })
var data interface{}
err := json.Unmarshal([]byte(token), &data)
if err != nil {
err = errors.New("(number) cannot parse query, invalid token at: " + token)
}
return data, input[loc[1]:], err
}
if loc := reValBoolean.FindStringIndex(input); loc != nil && loc[0] == 0 {
token := strings.TrimFunc(input[loc[0]:loc[1]], func(r rune) bool { return _isSpace(r) || r == separator })
var data bool
err := json.Unmarshal([]byte(token), &data)
if err != nil {
err = errors.New("(bool) cannot parse query, invalid token at: " + token)
}
return data, input[loc[1]:], err
}
if loc := reValStringDoubleQuoted.FindStringIndex(input); loc != nil && loc[0] == 0 {
val, err := strconv.Unquote(strings.TrimFunc(input[loc[0]:loc[1]], func(r rune) bool { return _isSpace(r) || r == separator }))
if err != nil {
err = errors.New("(unquote) cannot parse query, invalid token at: " + val)
} else {
val = strings.ReplaceAll(val, "\a", `\a`)
val = strings.ReplaceAll(val, "\b", `\b`)
val = strings.ReplaceAll(val, "\f", `\f`)
val = strings.ReplaceAll(val, "\n", `\n`)
val = strings.ReplaceAll(val, "\r", `\r`)
val = strings.ReplaceAll(val, "\t", `\t`)
val = strings.ReplaceAll(val, "\v", `\v`)
// string value must be single-quoted (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.insert.html)
val = "'" + val + "'"
}
return val, input[loc[1]:], err
}
if loc := reValStringSingleQuoted.FindStringIndex(input); loc != nil && loc[0] == 0 {
val := strings.TrimFunc(input[loc[0]:loc[1]], func(r rune) bool { return _isSpace(r) || r == separator })
val = val[1 : len(val)-1]
// string value must be single-quoted (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.insert.html)
val = "'" + val + "'"
return val, input[loc[1]:], err
}
if loc := reValRaw.FindStringIndex(input); loc != nil && loc[0] == 0 {
val := strings.TrimFunc(input[loc[0]:loc[1]], func(r rune) bool { return _isSpace(r) || r == separator })
return val, input[loc[1]:], nil
}
return nil, input, errors.New("cannot parse query, invalid token at: " + input)
}
func _fetchAllRowsAndClose(dbRows *sql.Rows) ([]map[string]interface{}, error) {
defer func() { _ = dbRows.Close() }()
colTypes, err := dbRows.ColumnTypes()
if err != nil {
return nil, err
}
numCols := len(colTypes)
rows := make([]map[string]interface{}, 0)
for dbRows.Next() {
vals := make([]interface{}, numCols)
scanVals := make([]interface{}, numCols)
for i := 0; i < numCols; i++ {
scanVals[i] = &vals[i]
}
if err := dbRows.Scan(scanVals...); err == nil {
row := make(map[string]interface{})
for i := range colTypes {
row[colTypes[i].Name()] = vals[i]
}
rows = append(rows, row)
} else if !errors.Is(err, sql.ErrNoRows) {
return nil, err
}
}
return rows, nil
}
// TransformInsertStmToPartiQL converts an INSERT statement to a PartiQL statement.
//
// e.g. INSERT INTO table_name (field1, field2, field3) VALUES ('val1', ?, 3) will be converted to
// INSERT INTO table_name VALUE {'field1': 'val1', 'field2': ?, 'field3': 3}
//
// Note:
//
// - table name is automatically double-quoted per PartiQL syntax.
// - field name is automatically single-quoted per PartiQL syntax.
// - value is automatically single-quoted if it is a string per PartiQL syntax.
// - Order of fields after conversion is the same as the order of fields in the original INSERT statement.
// - Other than the above, the value is kept as-is! It is the caller's responsibility to ensure the value is valid.
// - PartiQL syntax for INSERT statement: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.insert.html
//
// @Available since v1.1.0
func TransformInsertStmToPartiQL(insStm string) (string, error) {
insSql := strings.TrimSpace(insStm)
if !reSqlInsert.MatchString(insSql) {
return insStm, ErrNotValidInsertStm
}
groups := reSqlInsert.FindAllStringSubmatch(insSql, -1)
tableName := strings.Trim(strings.TrimSpace(groups[0][1]), `'"`)
fieldsStr := strings.TrimSpace(groups[0][2])
fields := regexp.MustCompile(`[,\s]+`).Split(fieldsStr, -1)
valuesStr := strings.TrimSpace(groups[0][3])
values := make([]interface{}, 0)
for temp := strings.TrimSpace(valuesStr); temp != ""; temp = strings.TrimSpace(temp) {
value, leftOver, err := _parseValue(temp, ',')
if err == nil {
if value == nil {
values = append(values, "NULL")
} else {
switch v := value.(type) {
case valPlaceholder:
values = append(values, "?")
default:
values = append(values, v)
}
}
temp = leftOver
continue
}
return insSql, err
}
if len(fields) != len(values) {
return insSql, ErrFieldsAndValuesNotMatch
}
fieldsAndVals := make([]string, 0)
for i, field := range fields {
// field name must be single-quoted (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.insert.html)
fieldsAndVals = append(fieldsAndVals, fmt.Sprintf(`'%s': %v`, field, values[i]))
}
// table name must be double-quoted (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.insert.html)
finalSql := fmt.Sprintf(`INSERT INTO "%s" VALUE {%s}`, tableName, strings.Join(fieldsAndVals, ", "))
return finalSql, nil
}
// WaitForGSIStatus periodically checks if table's GSI status reaches a desired value, or timeout.
//
// - statusList: list of desired statuses. This function returns nil if one of the desired statuses is reached.
// - delay: sleep for this amount of time after each status check. Supplied value of 0 or negative means 'no sleep'.
// - timeout is controlled via ctx.
// - Note: this function treats GSI status as "" if it does not exist. Thus, supply value "" to statusList to wait for GSI to be deleted.
//
// @Available since v1.1.0
func WaitForGSIStatus(ctx context.Context, db *sql.DB, tableName, gsiName string, statusList []string, sleepTime time.Duration) error {
if ctx == nil {
ctx = context.Background()
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
dbrows, err := db.Query(fmt.Sprintf(`DESCRIBE GSI %s ON %s`, gsiName, tableName))
if err != nil {
return err
}
rows, err := _fetchAllRowsAndClose(dbrows)
if err != nil {
return err
}
status := ""
if len(rows) > 0 {
status, _ = rows[0]["IndexStatus"].(string)
}
if g18.FindInSlice(status, statusList) >= 0 {
return nil
}
if sleepTime > 0 {
time.Sleep(sleepTime)
}
}
}
}
// WaitForTableStatus periodically checks if table status reaches a desired value, or timeout.
//
// - statusList: list of desired statuses. This function returns nil if one of the desired statuses is reached.
// - delay: sleep for this amount of time after each status check. Supplied value of 0 or negative means 'no sleep'.
// - timeout is controlled via ctx.
// - Note: this function treats table status as "" if it does not exist. Thus, supply value "" to statusList to wait for table to be deleted.
//
// @Available since v1.1.0
func WaitForTableStatus(ctx context.Context, db *sql.DB, tableName string, statusList []string, sleepTime time.Duration) error {
if ctx == nil {
ctx = context.Background()
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
dbrows, err := db.Query(fmt.Sprintf(`DESCRIBE TABLE %s`, tableName))
if err != nil {
return err
}
rows, err := _fetchAllRowsAndClose(dbrows)
if err != nil {
return err
}
status := ""
if len(rows) > 0 {
status, _ = rows[0]["TableStatus"].(string)
}
if g18.FindInSlice(status, statusList) >= 0 {
return nil
}
if sleepTime > 0 {
time.Sleep(sleepTime)
}
}
}
}