-
Notifications
You must be signed in to change notification settings - Fork 862
/
Expression.cs
169 lines (146 loc) · 7.5 KB
/
Expression.cs
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Amazon.DynamoDBv2.Model;
using Amazon.Util;
namespace Amazon.DynamoDBv2.DocumentModel
{
/// <summary>
/// Expressions are used for conditional deletes and filtering for query and scan operations.
/// </summary>
public class Expression
{
private Dictionary<string, string> _expressionAttributeNames = new Dictionary<string, string>(StringComparer.Ordinal);
private Dictionary<string, DynamoDBEntry> _expressionAttributeValues = new Dictionary<string, DynamoDBEntry>(StringComparer.Ordinal);
internal bool IsSet
{
get { return this.ExpressionStatement != null; }
}
/// <summary>
/// Gets and sets the property ExpressionStatement. "Price > :price" is an example expression statement.
/// :price is a variable which gets its value from the ExpressionAttributeValues collection. If this is used
/// for deletes then it prevents the delete from happening if the Price attribute on the item is less then the passed
/// in price. For query and scan it will only return back items where the Price attribute is greater then passed
/// in price.
/// </summary>
public string ExpressionStatement
{
get;
set;
}
/// <summary>
/// Gets and sets the property ExpressionAttributeNames. This collection contains attribute names from the item
/// that should be substituted in the expression when it is evaluated. For example the expression "#C < #U" will
/// expect the attribute names to be added to this collection.
/// <code>
/// expression.ExpressionAttributeNames["#C"] = "CriticRating"
/// expression.ExpressionAttributeNames["#U"] = "UserRating"
/// </code>
/// </summary>
public Dictionary<string, string> ExpressionAttributeNames
{
get { return this._expressionAttributeNames; }
set { this._expressionAttributeNames = value; }
}
/// <summary>
/// Gets and sets the property ExpressionAttributeValues. This collection contains the values to be substituted in the expression.
/// For example the expression "Price > :price" will contain one entry in this collection a key of ":price".
/// <para>
/// DynamoDBEntry contains many common implicit cast operations so assignment can be done with the basic .NET types.
/// In the price example shown above the value to be used for the expression can be provided using the following code snippet:
/// <code>
/// expression.ExpressionAttributeValues[":price"] = 3.99;
/// </code>
/// </para>
/// </summary>
public Dictionary<string, DynamoDBEntry> ExpressionAttributeValues
{
get { return this._expressionAttributeValues; }
set { this._expressionAttributeValues = value; }
}
internal void ApplyExpression(ScanRequest request, Table table)
{
request.FilterExpression = this.ExpressionStatement;
request.ExpressionAttributeNames = new Dictionary<string, string>(this.ExpressionAttributeNames);
request.ExpressionAttributeValues = ConvertToAttributeValues(this.ExpressionAttributeValues, table);
}
internal void ApplyExpression(DeleteItemRequest request, Table table)
{
request.ConditionExpression = this.ExpressionStatement;
request.ExpressionAttributeNames = new Dictionary<string, string>(this.ExpressionAttributeNames);
request.ExpressionAttributeValues = ConvertToAttributeValues(this.ExpressionAttributeValues, table);
}
internal void ApplyExpression(PutItemRequest request, Table table)
{
request.ConditionExpression = this.ExpressionStatement;
request.ExpressionAttributeNames = new Dictionary<string, string>(this.ExpressionAttributeNames);
request.ExpressionAttributeValues = ConvertToAttributeValues(this.ExpressionAttributeValues, table);
}
internal void ApplyExpression(UpdateItemRequest request, Table table)
{
request.ConditionExpression = this.ExpressionStatement;
request.ExpressionAttributeNames = new Dictionary<string,string>(this.ExpressionAttributeNames);
request.ExpressionAttributeValues = ConvertToAttributeValues(this.ExpressionAttributeValues, table);
}
internal static void ApplyExpression(QueryRequest request, Table table,
Expression keyExpression, Expression filterExpression)
{
if (keyExpression == null)
keyExpression = new Expression();
if (filterExpression == null)
filterExpression = new Expression();
if (!keyExpression.IsSet && !filterExpression.IsSet)
return;
if (keyExpression.IsSet)
request.KeyConditionExpression = keyExpression.ExpressionStatement;
if (filterExpression.IsSet)
request.FilterExpression = filterExpression.ExpressionStatement;
var kean = keyExpression.ExpressionAttributeNames;
var fean = filterExpression.ExpressionAttributeNames;
var combinedEan = Common.Combine(kean, fean, StringComparer.Ordinal);
request.ExpressionAttributeNames = combinedEan;
var keav = new Document(keyExpression.ExpressionAttributeValues).ForceConversion(table.Conversion);
var feav = new Document(filterExpression.ExpressionAttributeValues).ForceConversion(table.Conversion);
var combinedEav = Common.Combine(keav, feav, null);
request.ExpressionAttributeValues = ConvertToAttributeValues(combinedEav, table);
}
internal static Dictionary<string, AttributeValue> ConvertToAttributeValues(
Dictionary<string, DynamoDBEntry> valueMap, Table table)
{
var convertedValues = new Dictionary<string, AttributeValue>();
if (valueMap != null)
{
foreach (var kvp in valueMap)
{
var attributeName = kvp.Key;
var entry = kvp.Value;
if (entry == null)
convertedValues[attributeName] = new AttributeValue { NULL = true };
else
{
if (table.StoreAsEpoch.Contains(attributeName))
entry = Document.DateTimeToEpochSeconds(entry, attributeName);
var attributeConversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
convertedValues[attributeName] = entry.ConvertToAttributeValue(attributeConversionConfig);
}
}
}
return convertedValues;
}
}
}