1
|
|
|
package dynamodb |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
|
6
|
|
|
SDK "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
7
|
|
|
|
8
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors" |
9
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// Query executes `Query` operation. |
13
|
|
|
func (svc *DynamoDB) Query(ctx context.Context, r QueryRequest) (*QueryResult, error) { |
14
|
|
|
in, err := r.ToInput() |
15
|
|
|
if err != nil { |
16
|
|
|
return nil, err |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
out, err := svc.RawQuery(ctx, in) |
20
|
|
|
if err == nil { |
21
|
|
|
return NewQueryResult(out), nil |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
err = svc.errWrap(errors.ErrorData{ |
25
|
|
|
Err: err, |
26
|
|
|
AWSOperation: "Query", |
27
|
|
|
}) |
28
|
|
|
svc.Errorf(err.Error()) |
|
|
|
|
29
|
|
|
return nil, err |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// QueryRequest has parameters for `Query` operation. |
33
|
|
|
type QueryRequest struct { |
34
|
|
|
TableName string |
35
|
|
|
|
36
|
|
|
// optional |
37
|
|
|
ConsistentRead bool |
38
|
|
|
ExclusiveStartKey map[string]AttributeValue |
39
|
|
|
ExpressionAttributeNames map[string]string |
40
|
|
|
ExpressionAttributeValues map[string]AttributeValue |
41
|
|
|
FilterExpression string |
42
|
|
|
IndexName string |
43
|
|
|
KeyConditionExpression string |
44
|
|
|
KeyConditions map[string]Condition |
45
|
|
|
Limit int64 |
46
|
|
|
ProjectionExpression string |
47
|
|
|
ReturnConsumedCapacity ReturnConsumedCapacity |
48
|
|
|
ScanIndexForward bool |
49
|
|
|
Select Select |
50
|
|
|
|
51
|
|
|
XConditions XConditions |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func (r QueryRequest) ToInput() (*SDK.QueryInput, error) { |
|
|
|
|
55
|
|
|
in := &SDK.QueryInput{} |
56
|
|
|
|
57
|
|
|
if r.TableName != "" { |
58
|
|
|
in.TableName = pointers.String(r.TableName) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if r.ConsistentRead { |
62
|
|
|
in.ConsistentRead = pointers.Bool(r.ConsistentRead) |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if len(r.ExclusiveStartKey) != 0 { |
66
|
|
|
m := make(map[string]SDK.AttributeValue, len(r.ExclusiveStartKey)) |
67
|
|
|
for key, val := range r.ExclusiveStartKey { |
68
|
|
|
m[key] = val.ToSDK() |
69
|
|
|
} |
70
|
|
|
in.ExclusiveStartKey = m |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
in.ExpressionAttributeNames = r.ExpressionAttributeNames |
74
|
|
|
|
75
|
|
|
if len(r.ExpressionAttributeValues) != 0 { |
76
|
|
|
m := make(map[string]SDK.AttributeValue, len(r.ExpressionAttributeValues)) |
77
|
|
|
for key, val := range r.ExpressionAttributeValues { |
78
|
|
|
m[key] = val.ToSDK() |
79
|
|
|
} |
80
|
|
|
in.ExpressionAttributeValues = m |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if r.FilterExpression != "" { |
84
|
|
|
in.FilterExpression = pointers.String(r.FilterExpression) |
85
|
|
|
} |
86
|
|
|
if r.IndexName != "" { |
87
|
|
|
in.IndexName = pointers.String(r.IndexName) |
88
|
|
|
} |
89
|
|
|
if r.KeyConditionExpression != "" { |
90
|
|
|
in.KeyConditionExpression = pointers.String(r.KeyConditionExpression) |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if len(r.KeyConditions) != 0 { |
94
|
|
|
m := make(map[string]SDK.Condition, len(r.KeyConditions)) |
95
|
|
|
for key, val := range r.KeyConditions { |
96
|
|
|
m[key] = val.ToSDK() |
97
|
|
|
} |
98
|
|
|
in.KeyConditions = m |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if r.Limit != 0 { |
102
|
|
|
in.Limit = pointers.Long64(r.Limit) |
103
|
|
|
} |
104
|
|
|
if r.ProjectionExpression != "" { |
105
|
|
|
in.ProjectionExpression = pointers.String(r.ProjectionExpression) |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
in.ReturnConsumedCapacity = SDK.ReturnConsumedCapacity(r.ReturnConsumedCapacity) |
109
|
|
|
|
110
|
|
|
if r.ScanIndexForward { |
111
|
|
|
in.ScanIndexForward = pointers.Bool(r.ScanIndexForward) |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
in.Select = SDK.Select(r.Select) |
115
|
|
|
|
116
|
|
|
if r.XConditions.hasValue() { |
117
|
|
|
expr, err := r.XConditions.Build() |
118
|
|
|
if err != nil { |
119
|
|
|
return nil, err |
120
|
|
|
} |
121
|
|
|
if v := expr.Names(); v != nil { |
122
|
|
|
in.ExpressionAttributeNames = v |
123
|
|
|
} |
124
|
|
|
if v := expr.Values(); v != nil { |
125
|
|
|
in.ExpressionAttributeValues = v |
126
|
|
|
} |
127
|
|
|
if v := expr.Filter(); v != nil { |
128
|
|
|
in.FilterExpression = v |
129
|
|
|
} |
130
|
|
|
if v := expr.KeyCondition(); v != nil { |
131
|
|
|
in.KeyConditionExpression = v |
132
|
|
|
} |
133
|
|
|
if v := expr.Projection(); v != nil { |
134
|
|
|
in.ProjectionExpression = v |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
return in, nil |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// QueryResult contains results from `Query` operation. |
141
|
|
|
type QueryResult struct { |
142
|
|
|
ConsumedCapacity ConsumedCapacity |
143
|
|
|
Count int64 |
144
|
|
|
Items []map[string]SDK.AttributeValue // keep original type to reduce unmarshal cost |
145
|
|
|
LastEvaluatedKey map[string]AttributeValue |
146
|
|
|
ScannedCount int64 |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
func NewQueryResult(output *SDK.QueryResponse) *QueryResult { |
|
|
|
|
150
|
|
|
r := &QueryResult{} |
151
|
|
|
if output == nil { |
152
|
|
|
return r |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if output.ConsumedCapacity != nil { |
156
|
|
|
r.ConsumedCapacity = newConsumedCapacity(*output.ConsumedCapacity) |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if output.Count != nil { |
160
|
|
|
r.Count = *output.Count |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
r.Items = output.Items |
164
|
|
|
r.LastEvaluatedKey = newAttributeValueMap(output.LastEvaluatedKey) |
165
|
|
|
|
166
|
|
|
if output.ScannedCount != nil { |
167
|
|
|
r.ScannedCount = *output.ScannedCount |
168
|
|
|
} |
169
|
|
|
return r |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
func (r QueryResult) ToSliceMap() ([]map[string]interface{}, error) { |
|
|
|
|
173
|
|
|
return ToSliceMapValues(r.Items), nil |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
func (r QueryResult) Unmarshal(out interface{}) error { |
|
|
|
|
177
|
|
|
return RawUnmarshalAttributeValues(r.Items, out) |
178
|
|
|
} |
179
|
|
|
|