internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync)
{
var currentConfig = config ?? new UpdateItemOperationConfig();
// If the keys have been changed, treat entire document as having changed
bool haveKeysChanged = HaveKeysChanged(doc);
bool updateChangedAttributesOnly = !haveKeysChanged;
var attributeUpdates = doc.ToAttributeUpdateMap(Conversion, updateChangedAttributesOnly);
foreach (var keyName in this.KeyNames)
{
attributeUpdates.Remove(keyName);
}
UpdateItemRequest req = new UpdateItemRequest
{
TableName = TableName,
Key = key,
AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update
ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues)
};
((Amazon.Runtime.Internal.IAmazonWebServiceRequest)req).AddBeforeRequestHandler(isAsync ?
new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
new RequestEventHandler(UserAgentRequestEventHandlerSync)
);
ValidateConditional(currentConfig);
if (currentConfig.Expected != null)
{
req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion);
}
else if (currentConfig.ExpectedState != null &&
currentConfig.ExpectedState.ExpectedValues != null &&
currentConfig.ExpectedState.ExpectedValues.Count > 0)
{
req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion);
if (req.Expected.Count > 1)
req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
}
else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
{
currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion);
string statement;
Dictionary<string, AttributeValue> expressionAttributeValues;
Dictionary<string, string> expressionAttributeNames;
Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames);
req.AttributeUpdates = null;
req.UpdateExpression = statement;
if (req.ExpressionAttributeValues == null)
req.ExpressionAttributeValues = expressionAttributeValues;
else
{
foreach (var kvp in expressionAttributeValues)
req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value);
}
if (req.ExpressionAttributeNames == null)
req.ExpressionAttributeNames = expressionAttributeNames;
else
{
foreach (var kvp in expressionAttributeNames)
req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value);
}
}
var resp = DDBClient.UpdateItem(req);
var returnedAttributes = resp.Attributes;
doc.CommitChanges();
Document ret = null;
if (currentConfig.ReturnValues != ReturnValues.None)
{
ret = Document.FromAttributeMap(returnedAttributes);
}
return ret;
}