forked from btnguyen2k/godynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt_index.go
564 lines (503 loc) · 18.1 KB
/
stmt_index.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package godynamo
import (
"context"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
var (
dynamodbLSISpec = map[string]struct {
scanType reflect.Type
srcType string
}{
"IndexName": {srcType: "S", scanType: typeS},
"KeySchema": {srcType: "L", scanType: typeL},
"Projection": {srcType: "M", scanType: typeM},
"IndexSizeBytes": {srcType: "N", scanType: typeN},
"ItemCount": {srcType: "N", scanType: typeN},
"IndexArn": {srcType: "S", scanType: typeS},
}
dynamodbGSISpec = map[string]struct {
scanType reflect.Type
srcType string
}{
"Backfilling": {srcType: "BOOL", scanType: typeBool},
"IndexArn": {srcType: "S", scanType: typeS},
"IndexName": {srcType: "S", scanType: typeS},
"IndexSizeBytes": {srcType: "N", scanType: typeN},
"IndexStatus": {srcType: "S", scanType: typeS},
"ItemCount": {srcType: "N", scanType: typeN},
"KeySchema": {srcType: "L", scanType: typeL},
"Projection": {srcType: "M", scanType: typeM},
"ProvisionedThroughput": {srcType: "M", scanType: typeM},
}
)
// RowsDescribeIndex captures the result from DESCRIBE LSI or DESCRIBE GSI statement.
type RowsDescribeIndex struct {
count int
columnList []string
columnTypes map[string]reflect.Type
columnSourceTypes map[string]string
indexInfo map[string]interface{}
cursorCount int
}
// Columns implements driver.Rows/Columns.
func (r *RowsDescribeIndex) Columns() []string {
return r.columnList
}
// Close implements driver.Rows/Close.
func (r *RowsDescribeIndex) Close() error {
return nil
}
// Next implements driver.Rows/Next.
func (r *RowsDescribeIndex) Next(dest []driver.Value) error {
if r.cursorCount >= r.count {
return io.EOF
}
for i, colName := range r.columnList {
dest[i] = r.indexInfo[colName]
}
r.cursorCount++
return nil
}
// ColumnTypeScanType implements driver.RowsColumnTypeScanType/ColumnTypeScanType
func (r *RowsDescribeIndex) ColumnTypeScanType(index int) reflect.Type {
return r.columnTypes[r.columnList[index]]
}
// ColumnTypeDatabaseTypeName implements driver.RowsColumnTypeDatabaseTypeName/ColumnTypeDatabaseTypeName
//
// @since v0.3.0 ColumnTypeDatabaseTypeName returns DynamoDB's native data types (e.g. B, N, S, SS, NS, BS, BOOL, L, M, NULL).
func (r *RowsDescribeIndex) ColumnTypeDatabaseTypeName(index int) string {
return r.columnSourceTypes[r.columnList[index]]
}
/*----------------------------------------------------------------------*/
// StmtDescribeLSI implements "DESCRIBE LSI" statement.
//
// Syntax:
//
// DESCRIBE LSI <index-name> ON <table-name>
type StmtDescribeLSI struct {
*Stmt
tableName, indexName string
}
func (s *StmtDescribeLSI) validate() error {
if s.tableName == "" {
return errors.New("table name is missing")
}
if s.indexName == "" {
return errors.New("index name is missing")
}
return nil
}
// Exec implements driver.Stmt/Exec.
// This function is not implemented, use Query instead.
func (s *StmtDescribeLSI) Exec(_ []driver.Value) (driver.Result, error) {
return nil, errors.New("this operation is not supported, please use Query")
}
// ExecContext implements driver.StmtExecContext/ExecContext.
// This function is not implemented, use QueryContext instead.
func (s *StmtDescribeLSI) ExecContext(_ context.Context, _ []driver.NamedValue) (driver.Result, error) {
return nil, errors.New("this operation is not supported, please use QueryContext")
}
// Query implements driver.Stmt/Query.
func (s *StmtDescribeLSI) Query(_ []driver.Value) (driver.Rows, error) {
return s.QueryContext(s.conn.newContext(), nil)
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
//
// @Available since v0.2.0
func (s *StmtDescribeLSI) QueryContext(ctx context.Context, _ []driver.NamedValue) (driver.Rows, error) {
input := &dynamodb.DescribeTableInput{
TableName: &s.tableName,
}
output, err := s.conn.client.DescribeTable(s.conn.ensureContext(ctx), input)
result := &RowsDescribeIndex{count: 0}
if err == nil {
for _, lsi := range output.Table.LocalSecondaryIndexes {
if lsi.IndexName != nil && *lsi.IndexName == s.indexName {
result.count = 1
js, _ := json.Marshal(lsi)
_ = json.Unmarshal(js, &result.indexInfo)
result.columnList = make([]string, 0)
result.columnTypes = make(map[string]reflect.Type)
result.columnSourceTypes = make(map[string]string)
for col, spec := range dynamodbLSISpec {
result.columnList = append(result.columnList, col)
result.columnTypes[col] = spec.scanType
result.columnSourceTypes[col] = spec.srcType
}
sort.Strings(result.columnList)
break
}
}
}
return result, err
}
/*----------------------------------------------------------------------*/
// StmtCreateGSI implements "CREATE GSI" statement.
//
// Syntax:
//
// CREATE GSI [IF NOT EXISTS] <index-name> ON <table-name>
// <WITH PK=pk-attr-name:data-type>
// [[,] WITH SK=sk-attr-name:data-type]
// [[,] WITH wcu=<number>[,] WITH rcu=<number>]
// [[,] WITH projection=*|attr1,attr2,attr3,...]
//
// - PK: GSI's partition key, format name:type (type is one of String, Number, Binary).
// - SK: GSI's sort key, format name:type (type is one of String, Number, Binary).
// - RCU: an integer specifying DynamoDB's read capacity.
// - WCU: an integer specifying DynamoDB's write capacity.
// - PROJECTION:
// - if not supplied, GSI will be created with projection setting KEYS_ONLY.
// - if equal to "*", GSI will be created with projection setting ALL.
// - if supplied with comma-separated attribute list, for example "attr1,attr2,attr3", GSI will be created with projection setting INCLUDE.
// - If "IF NOT EXISTS" is specified, Exec will silently swallow the error "Attempting to create an index which already exists".
// - Note: The provisioned throughput settings of a GSI are separate from those of its base table.
// - Note: GSI inherit the RCU and WCU mode from the base table. That means if the base table is in on-demand mode, then DynamoDB also creates the GSI in on-demand mode.
// - Note: there must be at least one space before the WITH keyword.
type StmtCreateGSI struct {
*Stmt
indexName, tableName string
ifNotExists bool
pkName, pkType string
skName, skType *string
rcu, wcu *int64
projectedAttrs string
withOptsStr string
}
func (s *StmtCreateGSI) parse() error {
if err := s.Stmt.parseWithOpts(s.withOptsStr); err != nil {
return err
}
// partition key
pkTokens := strings.SplitN(s.withOpts["PK"].FirstString(), ":", 2)
s.pkName = strings.TrimSpace(pkTokens[0])
if len(pkTokens) > 1 {
s.pkType = strings.TrimSpace(strings.ToUpper(pkTokens[1]))
}
if s.pkName == "" {
return fmt.Errorf("no PartitionKey, specify one using WITH pk=pkname:pktype")
}
if _, ok := dataTypes[s.pkType]; !ok {
return fmt.Errorf("invalid type <%s> for PartitionKey, accepts values are BINARY, NUMBER and STRING", s.pkType)
}
// sort key
skTokens := strings.SplitN(s.withOpts["SK"].FirstString(), ":", 2)
skName := strings.TrimSpace(skTokens[0])
if skName != "" {
s.skName = &skName
skType := ""
if len(skTokens) > 1 {
skType = strings.TrimSpace(strings.ToUpper(skTokens[1]))
}
if _, ok := dataTypes[skType]; !ok {
return fmt.Errorf("invalid type SortKey <%s>, accepts values are BINARY, NUMBER and STRING", skType)
}
s.skType = &skType
}
// projection
s.projectedAttrs = s.withOpts["PROJECTION"].FirstString()
// RCU
if _, ok := s.withOpts["RCU"]; ok {
rcu, err := strconv.ParseInt(s.withOpts["RCU"].FirstString(), 10, 64)
if err != nil || rcu < 0 {
return fmt.Errorf("invalid RCU value: %s", s.withOpts["RCU"])
}
s.rcu = &rcu
}
// WCU
if _, ok := s.withOpts["WCU"]; ok {
wcu, err := strconv.ParseInt(s.withOpts["WCU"].FirstString(), 10, 64)
if err != nil || wcu < 0 {
return fmt.Errorf("invalid WCU value: %s", s.withOpts["WCU"])
}
s.wcu = &wcu
}
return nil
}
func (s *StmtCreateGSI) validate() error {
if s.tableName == "" {
return errors.New("table name is missing")
}
if s.indexName == "" {
return errors.New("index name is missing")
}
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtCreateGSI) Query(_ []driver.Value) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use Exec")
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
// This function is not implemented, use ExecContext instead.
func (s *StmtCreateGSI) QueryContext(_ context.Context, _ []driver.NamedValue) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use ExecContext")
}
// Exec implements driver.Stmt/Exec.
func (s *StmtCreateGSI) Exec(_ []driver.Value) (driver.Result, error) {
return s.ExecContext(s.conn.newContext(), nil)
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v0.2.0
func (s *StmtCreateGSI) ExecContext(ctx context.Context, _ []driver.NamedValue) (driver.Result, error) {
attrDefs := make([]types.AttributeDefinition, 0, 2)
attrDefs = append(attrDefs, types.AttributeDefinition{AttributeName: &s.pkName, AttributeType: dataTypes[s.pkType]})
keySchema := make([]types.KeySchemaElement, 0, 2)
keySchema = append(keySchema, types.KeySchemaElement{AttributeName: &s.pkName, KeyType: keyTypes["HASH"]})
if s.skName != nil {
attrDefs = append(attrDefs, types.AttributeDefinition{AttributeName: s.skName, AttributeType: dataTypes[*s.skType]})
keySchema = append(keySchema, types.KeySchemaElement{AttributeName: s.skName, KeyType: keyTypes["RANGE"]})
}
gsiInput := &types.CreateGlobalSecondaryIndexAction{
IndexName: &s.indexName,
KeySchema: keySchema,
Projection: &types.Projection{
ProjectionType: types.ProjectionTypeKeysOnly,
},
}
if s.projectedAttrs == "*" {
gsiInput.Projection.ProjectionType = types.ProjectionTypeAll
} else if s.projectedAttrs != "" {
gsiInput.Projection.ProjectionType = types.ProjectionTypeInclude
nonKeyAttrs := strings.Split(s.projectedAttrs, ",")
gsiInput.Projection.NonKeyAttributes = nonKeyAttrs
}
if s.rcu != nil && s.wcu != nil {
gsiInput.ProvisionedThroughput = &types.ProvisionedThroughput{
ReadCapacityUnits: s.rcu,
WriteCapacityUnits: s.wcu,
}
}
input := &dynamodb.UpdateTableInput{
TableName: &s.tableName,
AttributeDefinitions: attrDefs,
GlobalSecondaryIndexUpdates: []types.GlobalSecondaryIndexUpdate{{Create: gsiInput}},
}
_, err := s.conn.client.UpdateTable(s.conn.ensureContext(ctx), input)
affectedRows := int64(0)
if err == nil {
affectedRows = 1
}
if s.ifNotExists && err != nil {
if IsAwsError(err, "ResourceInUseException") || strings.Contains(err.Error(), "already exist") {
err = nil
}
}
return &ResultNoResultSet{err: err, affectedRows: affectedRows}, err
}
/*----------------------------------------------------------------------*/
// StmtDescribeGSI implements "DESCRIBE GSI" statement.
//
// Syntax:
//
// DESCRIBE GSI <index-name> ON <table-name>
type StmtDescribeGSI struct {
*Stmt
tableName, indexName string
}
func (s *StmtDescribeGSI) validate() error {
if s.tableName == "" {
return errors.New("table name is missing")
}
if s.indexName == "" {
return errors.New("index name is missing")
}
return nil
}
// Exec implements driver.Stmt/Exec.
// This function is not implemented, use Query instead.
func (s *StmtDescribeGSI) Exec(_ []driver.Value) (driver.Result, error) {
return nil, errors.New("this operation is not supported, please use Query")
}
// ExecContext implements driver.StmtExecContext/ExecContext.
// This function is not implemented, use QueryContext instead.
func (s *StmtDescribeGSI) ExecContext(_ context.Context, _ []driver.NamedValue) (driver.Result, error) {
return nil, errors.New("this operation is not supported, please use QueryContext")
}
// Query implements driver.Stmt/Query.
func (s *StmtDescribeGSI) Query(_ []driver.Value) (driver.Rows, error) {
return s.QueryContext(s.conn.newContext(), nil)
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
//
// @Available since v0.2.0
func (s *StmtDescribeGSI) QueryContext(ctx context.Context, _ []driver.NamedValue) (driver.Rows, error) {
input := &dynamodb.DescribeTableInput{
TableName: &s.tableName,
}
output, err := s.conn.client.DescribeTable(s.conn.ensureContext(ctx), input)
result := &RowsDescribeIndex{count: 0}
if err == nil {
for _, gsi := range output.Table.GlobalSecondaryIndexes {
if gsi.IndexName != nil && *gsi.IndexName == s.indexName {
result.count = 1
js, _ := json.Marshal(gsi)
_ = json.Unmarshal(js, &result.indexInfo)
result.columnList = make([]string, 0)
result.columnTypes = make(map[string]reflect.Type)
result.columnSourceTypes = make(map[string]string)
for col, spec := range dynamodbGSISpec {
result.columnList = append(result.columnList, col)
result.columnTypes[col] = spec.scanType
result.columnSourceTypes[col] = spec.srcType
}
sort.Strings(result.columnList)
break
}
}
}
return result, err
}
/*----------------------------------------------------------------------*/
// StmtAlterGSI implements "ALTER GSI" statement.
//
// Syntax:
//
// ALTER GSI <index-name> ON <table-name>
// WITH wcu=<number>[,] WITH rcu=<number>
//
// - RCU: an integer specifying DynamoDB's read capacity.
// - WCU: an integer specifying DynamoDB's write capacity.
// - Note: The provisioned throughput settings of a GSI are separate from those of its base table.
// - Note: GSI inherit the RCU and WCU mode from the base table. That means if the base table is in on-demand mode, then DynamoDB also creates the GSI in on-demand mode.
// - Note: there must be at least one space before the WITH keyword.
type StmtAlterGSI struct {
*Stmt
indexName, tableName string
rcu, wcu *int64
withOptsStr string
}
func (s *StmtAlterGSI) parse() error {
if err := s.Stmt.parseWithOpts(s.withOptsStr); err != nil {
return err
}
// RCU
if _, ok := s.withOpts["RCU"]; ok {
rcu, err := strconv.ParseInt(s.withOpts["RCU"].FirstString(), 10, 64)
if err != nil || rcu < 0 {
return fmt.Errorf("invalid RCU value: %s", s.withOpts["RCU"])
}
s.rcu = &rcu
}
// WCU
if _, ok := s.withOpts["WCU"]; ok {
wcu, err := strconv.ParseInt(s.withOpts["WCU"].FirstString(), 10, 64)
if err != nil || wcu < 0 {
return fmt.Errorf("invalid WCU value: %s", s.withOpts["WCU"])
}
s.wcu = &wcu
}
return nil
}
func (s *StmtAlterGSI) validate() error {
if s.tableName == "" {
return errors.New("table name is missing")
}
if s.indexName == "" {
return errors.New("index name is missing")
}
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtAlterGSI) Query(_ []driver.Value) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use Exec")
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
// This function is not implemented, use ExecContext instead.
func (s *StmtAlterGSI) QueryContext(_ context.Context, _ []driver.NamedValue) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use ExecContext")
}
// Exec implements driver.Stmt/Exec.
func (s *StmtAlterGSI) Exec(_ []driver.Value) (driver.Result, error) {
return s.ExecContext(s.conn.newContext(), nil)
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v0.2.0
func (s *StmtAlterGSI) ExecContext(ctx context.Context, _ []driver.NamedValue) (driver.Result, error) {
gsiInput := &types.UpdateGlobalSecondaryIndexAction{
IndexName: &s.indexName,
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: s.rcu,
WriteCapacityUnits: s.wcu,
},
}
input := &dynamodb.UpdateTableInput{
TableName: &s.tableName,
GlobalSecondaryIndexUpdates: []types.GlobalSecondaryIndexUpdate{{Update: gsiInput}},
}
_, err := s.conn.client.UpdateTable(s.conn.ensureContext(ctx), input)
affectedRows := int64(0)
if err == nil {
affectedRows = 1
}
return &ResultNoResultSet{err: err, affectedRows: affectedRows}, err
}
/*----------------------------------------------------------------------*/
// StmtDropGSI implements "DROP GSI" statement.
//
// Syntax:
//
// DROP GSI [IF EXISTS] <index-name> ON <table-name>
//
// If "IF EXISTS" is specified, Exec will silently swallow the error "ResourceNotFoundException".
type StmtDropGSI struct {
*Stmt
tableName string
indexName string
ifExists bool
}
func (s *StmtDropGSI) validate() error {
if s.tableName == "" {
return errors.New("table name is missing")
}
if s.indexName == "" {
return errors.New("index name is missing")
}
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtDropGSI) Query(_ []driver.Value) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use Exec")
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
// This function is not implemented, use ExecContext instead.
func (s *StmtDropGSI) QueryContext(_ context.Context, _ []driver.NamedValue) (driver.Rows, error) {
return nil, errors.New("this operation is not supported, please use ExecContext")
}
// Exec implements driver.Stmt/Exec.
func (s *StmtDropGSI) Exec(_ []driver.Value) (driver.Result, error) {
return s.ExecContext(s.conn.newContext(), nil)
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v0.2.0
func (s *StmtDropGSI) ExecContext(ctx context.Context, _ []driver.NamedValue) (driver.Result, error) {
gsiInput := &types.DeleteGlobalSecondaryIndexAction{IndexName: &s.indexName}
input := &dynamodb.UpdateTableInput{
TableName: &s.tableName,
GlobalSecondaryIndexUpdates: []types.GlobalSecondaryIndexUpdate{{Delete: gsiInput}},
}
_, err := s.conn.client.UpdateTable(s.conn.ensureContext(ctx), input)
affectedRows := int64(0)
if err == nil {
affectedRows = 1
}
if s.ifExists && IsAwsError(err, "ResourceNotFoundException") {
err = nil
}
return &ResultNoResultSet{err: err, affectedRows: affectedRows}, err
}