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(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) }; req.BeforeRequestEvent += isAsync ? new RequestEventHandler(UserAgentRequestEventHandlerAsync) : new RequestEventHandler(UserAgentRequestEventHandlerSync); if (currentConfig.Expected != null && currentConfig.ExpectedState != null) { throw new InvalidOperationException("Expected and ExpectedState cannot be set at the same time"); } if (currentConfig.Expected != null) { req.Expected = currentConfig.Expected.ToExpectedAttributeMap(); } if (currentConfig.ExpectedState != null && currentConfig.ExpectedState.ExpectedValues != null && currentConfig.ExpectedState.ExpectedValues.Count > 0) { req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(); if (req.Expected.Count > 1) { req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator); } } 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); }
/// <summary> /// Creates a map of attribute names mapped to AttributeValueUpdate objects. /// </summary> /// <param name="doc"></param> /// <param name="changedAttributesOnly">If true, only attributes that have been changed will be in the map.</param> /// <returns></returns> public Dictionary <string, AttributeValueUpdate> ToAttributeUpdateMap(Document doc, bool changedAttributesOnly) { return(doc.ToAttributeUpdateMap(this.Conversion, changedAttributesOnly, this.StoreAsEpoch)); }
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); }