use of software.amazon.awssdk.services.dynamodb.model.Update in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method addExpressionsIfExist.
private UpdateItemRequest.Builder addExpressionsIfExist(WriteModification transformation, Map<String, AttributeValue> filteredAttributeValues, UpdateItemRequest.Builder requestBuilder, TableMetadata tableMetadata) {
Map<String, String> expressionNames = null;
Map<String, AttributeValue> expressionValues = null;
String conditionExpressionString = null;
/* Add update expression for transformed non-key attributes if applicable */
if (!filteredAttributeValues.isEmpty()) {
Expression fullUpdateExpression = generateUpdateExpression(filteredAttributeValues, tableMetadata);
expressionNames = fullUpdateExpression.expressionNames();
expressionValues = fullUpdateExpression.expressionValues();
requestBuilder = requestBuilder.updateExpression(fullUpdateExpression.expression());
}
/* Merge in conditional expression from extension WriteModification if applicable */
if (transformation != null && transformation.additionalConditionalExpression() != null) {
expressionNames = Expression.joinNames(expressionNames, transformation.additionalConditionalExpression().expressionNames());
expressionValues = Expression.joinValues(expressionValues, transformation.additionalConditionalExpression().expressionValues());
conditionExpressionString = transformation.additionalConditionalExpression().expression();
}
/* Merge in conditional expression from specified 'conditionExpression' if applicable */
Expression conditionExpression = request.map(r -> Optional.ofNullable(r.conditionExpression()), r -> Optional.ofNullable(r.conditionExpression())).orElse(null);
if (conditionExpression != null) {
expressionNames = Expression.joinNames(expressionNames, conditionExpression.expressionNames());
expressionValues = Expression.joinValues(expressionValues, conditionExpression.expressionValues());
conditionExpressionString = Expression.joinExpressions(conditionExpressionString, conditionExpression.expression(), " AND ");
}
// Avoiding adding empty collections that the low level SDK will propagate to DynamoDb where it causes error.
if (expressionNames != null && !expressionNames.isEmpty()) {
requestBuilder = requestBuilder.expressionAttributeNames(expressionNames);
}
if (expressionValues != null && !expressionValues.isEmpty()) {
requestBuilder = requestBuilder.expressionAttributeValues(expressionValues);
}
return requestBuilder.conditionExpression(conditionExpressionString);
}
use of software.amazon.awssdk.services.dynamodb.model.Update in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method generateTransactWriteItem.
@Override
public TransactWriteItem generateTransactWriteItem(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension dynamoDbEnhancedClientExtension) {
UpdateItemRequest updateItemRequest = generateRequest(tableSchema, operationContext, dynamoDbEnhancedClientExtension);
Update.Builder builder = Update.builder().key(updateItemRequest.key()).tableName(updateItemRequest.tableName()).updateExpression(updateItemRequest.updateExpression()).conditionExpression(updateItemRequest.conditionExpression()).expressionAttributeValues(updateItemRequest.expressionAttributeValues()).expressionAttributeNames(updateItemRequest.expressionAttributeNames());
request.right().map(TransactUpdateItemEnhancedRequest::returnValuesOnConditionCheckFailureAsString).ifPresent(builder::returnValuesOnConditionCheckFailure);
return TransactWriteItem.builder().update(builder.build()).build();
}
use of software.amazon.awssdk.services.dynamodb.model.Update in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTransactTest method generateTransactWriteItem_returnValuesOnConditionCheckFailure_generatesCorrectRequest.
@Test
public void generateTransactWriteItem_returnValuesOnConditionCheckFailure_generatesCorrectRequest() {
FakeItem fakeItem = createUniqueFakeItem();
Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
String returnValues = "return-values";
UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(TransactUpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).returnValuesOnConditionCheckFailure(returnValues).build()));
OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
doReturn(ddbRequest(fakeItemMap, b -> {
})).when(updateItemOperation).generateRequest(any(), any(), any());
TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).returnValuesOnConditionCheckFailure(returnValues).build()).build();
assertThat(actualResult, is(expectedResult));
verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Aggregations