/* * Copyright 2015-2023 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. */ /** * A request-centric Expression Specification Builder package that can be used to construct valid * expressions, and the respective name maps and value maps, for various DynamoDB requests in a typeful manner. *
* {@link com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder} * is the API entry point to this library. *
* import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*;
* ...
* Table table = dynamo.getTable(TABLE_NAME);
*
* UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder()
* // SET num1 = num1 + 20
* .addUpdate(
* N("num1").set(N("num1").plus(20)))
* // SET string-attr = "string-value"
* .addUpdate(
* S("string-attr").set("string-value")
* )
* // num2 BETWEEN 0 AND 100
* .withCondition(
* N("num2").between(0, 100)
* ).buildForUpdate();
*
* table.updateItem(HASH_KEY_NAME, "hashKeyValue", RANGE_KEY_NAME, 0, xspec);
*
*
* * Let's say you want to include a complex condition expression such as: * *
* (attribute_not_exists(item_version) AND attribute_not_exists(config_id) AND attribute_not_exists(config_version)) OR * (item_version < 123) OR * (item_version = 123 AND config_id < 456) OR * (item_version = 123 AND config_id = 456 AND config_version < 999) ** * Here is how: *
* *
* import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*;
* ...
* Table table = dynamo.getTable(TABLE_NAME);
*
* UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder()
* // SET num1 = num1 + 20
* .addUpdate(
* N("num1").set(N("num1").plus(20)))
* // SET string-attr = "string-value"
* .addUpdate(
* S("string-attr").set("string-value")
* )
* // a complex condition expression (as shown above)
* .withCondition(
* // add explicit parenthesis
* parenthesize( attribute_not_exists("item_version")
* .and( attribute_not_exists("config_id") )
* .and( attribute_not_exists("config_version") )
* ).or( N("item_version").lt(123) )
* .or( N("item_version").eq(123)
* .and( N("config_id").lt(456) ) )
* .or( N("item_version").eq(123)
* .and( N("config_id").eq(456) )
* .and( N("config_version").lt(999) ))
* ).buildForUpdate();
*
* table.updateItem(HASH_KEY_NAME, "hashKeyValue", RANGE_KEY_NAME, 0, xspec);
*
*
* * Without ExpressionSpecBuilder, the code (using the DynamoDB Document API) could * be something like: * *
* ItemCollection<?> col = table.scan(
* "(#hk = :hashkeyAttrValue) AND (#rk BETWEEN :lo AND :hi)",
* new NameMap().with("#hk", HASH_KEY_NAME).with("#rk", RANGE_KEY_NAME),
* new ValueMap().withString(":hashkeyAttrValue", "allDataTypes")
* .withInt(":lo", 1).withInt(":hi", 10));
*
* In contrast, using ExpressionSpecBuilder:
**
* import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*;
* ...
* ScanExpressionSpec xspec = new ExpressionSpecBuilder()
* .withCondition(
* S(HASH_KEY_NAME).eq("allDataTypes")
* .and(N(RANGE_KEY_NAME).between(1, 10))
* ).buildForScan();
*
* ItemCollection> col = table.scan(xspec);
*
*
*
* import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*;
* ...
* Table table = dynamo.getTable(TABLE_NAME);
*
* UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder()
* .addUpdate(S("mapAttr.colors[0]").set("red"))
* .addUpdate(S("mapAttr.colors[1]").set("blue"))
* .addUpdate(L("mapAttr.members").set(
* L("mapAttr.members").listAppend("marry", "liza")))
* .addUpdate(SS("mapAttr.countries").append("cn", "uk"))
* .addUpdate(SS("mapAttr.brands").delete("Facebook", "LinkedIn"))
* .addUpdate(attribute("mapAttr.foo").remove())
* .buildForUpdate();
*
* assertEquals("SET #0.#1[0] = :0, #0.#1[1] = :1, #0.#2 = list_append(#0.#2, :2) ADD #0.#3 :3 DELETE #0.#4 :4 REMOVE #0.#5",
* xspec.getUpdateExpression());
*
* final String hashkey = "addRemoveDeleteColors";
* table.updateItem(HASH_KEY_NAME, hashkey, RANGE_KEY_NAME, 0, xspec);
*
*
*