forked from smugmug/godynamo
/
query.go
113 lines (101 loc) · 3.97 KB
/
query.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
// Support for the DynamoDB Query endpoint.
//
// example use:
//
// tests/query-livestest.go
//
package query
import (
"encoding/json"
"github.com/smugmug/godynamo/authreq"
"github.com/smugmug/godynamo/aws_const"
"github.com/smugmug/godynamo/types/attributestoget"
"github.com/smugmug/godynamo/types/attributevalue"
"github.com/smugmug/godynamo/types/aws_strings"
"github.com/smugmug/godynamo/types/capacity"
"github.com/smugmug/godynamo/types/condition"
"github.com/smugmug/godynamo/types/expressionattributenames"
"github.com/smugmug/godynamo/types/item"
)
const (
ENDPOINT_NAME = "Query"
QUERY_ENDPOINT = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME
OP_EQ = aws_strings.OP_EQ
OP_LE = aws_strings.OP_LE
OP_LT = aws_strings.OP_LT
OP_GE = aws_strings.OP_GE
OP_GT = aws_strings.OP_GT
OP_BEGINS_WITH = aws_strings.OP_BEGINS_WITH
OP_BETWEEN = aws_strings.OP_BETWEEN
LIMIT = 10000 // limit of query unless set
)
type ComparisonOperator string
// These are here for backward compatibility
type KeyConditions condition.Conditions
type KeyCondition condition.Condition
type Query struct {
AttributesToGet attributestoget.AttributesToGet `json:",omitempty"`
ConditionalOperator string `json:",omitempty"`
ConsistentRead bool // false is sane default
ExclusiveStartKey attributevalue.AttributeValueMap `json:",omitempty"`
ExpressionAttributeNames expressionattributenames.ExpressionAttributeNames `json:",omitempty"`
ExpressionAttributeValues attributevalue.AttributeValueMap `json:",omitempty"`
FilterExpression string `json:",omitempty"`
Indexname string `json:",omitempty"`
KeyConditions condition.Conditions
Limit uint64 `json:",omitempty"`
ProjectionExpression string `json:",omitempty"`
QueryFilter condition.Conditions `json:",omitempty"`
ReturnConsumedCapacity string `json:",omitempty"`
ScanIndexForward *bool `json:",omitempty"`
Select string `json:",omitempty"`
TableName string
}
// NewQuery returns a pointer to an instantiation of the Query struct.
func NewQuery() *Query {
q := new(Query)
q.AttributesToGet = attributestoget.NewAttributesToGet()
q.ExclusiveStartKey = attributevalue.NewAttributeValueMap()
q.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames()
q.ExpressionAttributeValues = attributevalue.NewAttributeValueMap()
q.KeyConditions = condition.NewConditions()
q.QueryFilter = condition.NewConditions()
return q
}
type Request Query
type Response struct {
ConsumedCapacity *capacity.ConsumedCapacity `json:",omitempty"`
Count uint64
Items []item.Item `json:",omitempty"`
LastEvaluatedKey attributevalue.AttributeValueMap `json:",omitempty"`
ScannedCount uint64 `json:",omitempty"`
}
func NewResponse() *Response {
r := new(Response)
r.ConsumedCapacity = capacity.NewConsumedCapacity()
r.Items = make([]item.Item, 0)
r.LastEvaluatedKey = attributevalue.NewAttributeValueMap()
return r
}
func (query *Query) EndpointReq() ([]byte, int, error) {
// returns resp_body,code,err
reqJSON, json_err := json.Marshal(query)
if json_err != nil {
return nil, 0, json_err
}
return authreq.RetryReqJSON_V4(reqJSON, QUERY_ENDPOINT)
}
func (req *Request) EndpointReq() ([]byte, int, error) {
query := Query(*req)
return query.EndpointReq()
}
// ValidOp determines if an operation is in the approved list.
func ValidOp(op string) bool {
return (op == OP_EQ ||
op == OP_LE ||
op == OP_LT ||
op == OP_GE ||
op == OP_GT ||
op == OP_BEGINS_WITH ||
op == OP_BETWEEN)
}