

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 式を使用する
<a name="ddb-mapper-expressions"></a>

****  
**DynamoDB Mapper はデベロッパープレビューリリースです。機能は完了しておらず、変更される可能性があります。**

特定の DynamoDB オペレーションは、制約または条件を指定するために使用できる[式](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.html)を受け入れます。DynamoDB Mapper は、式を作成するためのイディオマティック Kotlin DSL を提供します。DSL を使用すると、コードの構造と読みやすさが向上し、式の作成も容易になります。

このセクションでは、DSL 構文について説明し、さまざまな例を示します。

## オペレーションで式を使用する
<a name="ddb-mapper-expressions-basic-usage"></a>

のようなオペレーションでは式を使用して`scan`、定義した基準に基づいて返された項目をフィルタリングします。DynamoDB Mapper で式を使用するには、オペレーションリクエストに式コンポーネントを追加します。

次のスニペットは、 `scan`オペレーションで使用されるフィルター式の例を示しています。Lambda 引数を使用して、返される項目を`year`属性値が 2001 の項目に制限するフィルター条件を記述します。

```
val table = // A table instance.

table.scanPaginated {
    filter {
        attr("year") eq 2001
    }
}
```

次の例は、ソートキーフィルタリングと非キーフィルタリングの 2 つの場所で式をサポートする`query`オペレーションを示しています。

```
table.queryPaginated {
    keyCondition = KeyFilter(partitionKey = 1000) { sortKey startsWith "M" }
    filter {
        attr("year") eq 2001
    }
}
```

前のコードは、次の 3 つの条件をすべて満たすコードに結果をフィルタリングします。
+ パーティションキー属性値は 1000 *-AND- *です
+ ソートキー属性値は *M* *-AND-* で始まる
+ 年属性値は 2001 です

## DSL コンポーネント
<a name="ddb-mapper-expressions-dsl"></a>

DSL 構文は、式の構築に使用する、以下で説明するいくつかのタイプのコンポーネントを公開します。

### 属性
<a name="ddb-mapper-expressions-dsl-attrs"></a>

ほとんどの条件は、キーまたはドキュメントパスによって識別される属性を参照します。DSK では、 `attr`関数を使用してすべての属性参照を作成し、オプションで追加の変更を加えます。

次のコードは、インデックスによるリストの参照解除やキーによるマップの参照解除など、単純な属性参照から複雑な属性参照までの範囲の例を示しています。

```
attr("foo")           // Refers to the value of top-level attribute `foo`.

attr("foo")[3]        // Refers to the value at index 3 in the list value of
                      // attribute `foo`.

attr("foo")[3]["bar"] // Refers to the value of key `bar` in the map value at
                      // index 3 of the list value of attribute `foo`.
```

### 同等性と不等式
<a name="ddb-mapper-expressions-dsl-eq-and-ineq"></a>

式の属性値は、等号と不等号で比較できます。属性値をリテラル値または他の属性値と比較できます。条件を指定するために使用する関数は次のとおりです。
+ `eq`: は に等しい ( に相当`==`)
+ `neq`: は と等しくない ( に相当`!=`)
+ `gt`: が より大きい ( に相当`>`)
+ `gte`: が 以上 ( に相当`>=`)
+ `lt`: が より小さい ( に相当`<`)
+ `lte`: 以下 ( に相当`<=`)

比較関数を引数と組み合わせるには、次の例に示すように、インフィックス表記を使用します。

```
attr("foo") eq 42           // Uses a literal. Specifies that the attribute value `foo` must be
                            // equal to 42.

attr("bar") gte attr("baz") // Uses another attribute value. Specifies that the attribute 
                            // value `bar` must be greater than or equal to the
                            // attribute value of `baz`.
```

### 範囲とセット
<a name="ddb-mapper-expressions-dsl-ranges-sets"></a>

単一の値に加えて、属性値を範囲またはセットの複数の値と比較できます。次の例に示すように、infix `[isIn](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-in.html)`関数を使用して比較を行います。

```
attr("foo") isIn 0..99  // Specifies that the attribute value `foo` must be
                        // in the range of `0` to `99` (inclusive).

attr("foo") isIn setOf( // Specifies that the attribute value `foo` must be
    "apple",            // one of `apple`, `banana`, or `cherry`.
    "banana",
    "cherry",
)
```

この`isIn`関数は、コレクション ( など`Set<String>`) と Kotlin として表現できる境界 `[ClosedRange<T>](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/-closed-range/)` ( など) のオーバーロードを提供します`[IntRange](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/-int-range/)`。として表現できない境界 `ClosedRange<T>` (バイト配列やその他の属性参照など) には、 `[isBetween](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-between.html)`関数を使用できます。

```
val lowerBytes = byteArrayOf(0x48, 0x65, 0x6c)  // Specifies that the attribute value
val upperBytes = byteArrayOf(0x6c, 0x6f, 0x21)  // `foo` is between the values
attr("foo").isBetween(lowerBytes, upperBytes)   // `0x48656c` and `0x6c6f21`

attr("foo").isBetween(attr("bar"), attr("baz")) // Specifies that the attribute value
                                                // `foo` is between the values of
                                                // attributes `bar` and `baz`.
```

### ブールロジック
<a name="ddb-mapper-expressions-dsl-boolean"></a>

以下の関数を使用して、個々の条件を組み合わせるか、ブールロジックを使用して変更できます。
+ `and`: すべての条件が true ( に相当` &&`) である必要があります
+ `or`: 少なくとも 1 つの条件が true ( に相当`||`) である必要があります
+ `not`: 指定された条件は false ( に相当`!`) である必要があります

次の例は、各関数を示しています。

```
and(                           // Both conditions must be met:
    attr("foo") eq "banana",   // * attribute value `foo` must equal `banana`
    attr("bar") isIn 0..99,    // * attribute value `bar` must be between
)                              //   0 and 99 (inclusive)

or(                            // At least one condition must be met:
    attr("foo") eq "cherry",   // * attribute value `foo` must equal `cherry`
    attr("bar") isIn 100..199, // * attribute value `bar` must be between
)                              //   100 and 199 (inclusive)

not(                           // The attribute value `foo` must *not* be
    attr("baz") isIn setOf(    // one of `apple`, `banana`, or `cherry`.
        "apple",               // Stated another way, the attribute value
        "banana",              // must be *anything except* `apple`, `banana`,
        "cherry",              // or `cherry`--including potentially a
    ),                         // non-string value or no value at all.
)
```

次の式に示すように、ブール関数でブール条件をさらに組み合わせて、ネストされたロジックを作成できます。

```
or(
    and(
        attr("foo") eq 123,
        attr("bar") eq "abc",
    ),
    and(
        attr("foo") eq 234,
        attr("bar") eq "bcd",
    ),
)
```

前の式は、次のいずれかの条件を満たす式をフィルタリングします。
+  これらの条件はどちらも当てはまります。
  + `foo` 属性値は 123 *-AND-*
  + `bar` 属性値は「abc」です 
+ これらの条件はどちらも当てはまります。
  + `foo` 属性値は 234 *-AND- *です。
  + `bar` 属性値は「bcd」です 

これは、次の Kotlin ブール式に相当します。

```
(foo == 123 && bar == "abc") || (foo == 234 && bar == "bcd")
```

### 関数とプロパティ
<a name="ddb-mapper-expressions-dsl-functions"></a>

次の関数とプロパティは、追加の式機能を提供します。
+ `[contains](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/contains.html)`: 文字列/リスト属性値に指定された値が含まれているかどうかを確認します
+ `[exists](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/exists.html)`: 属性が定義されているかどうかをチェックし、値 ( を含む`null`) を保持します。
+ `[notExists](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/not-exists.html)`: 属性が未定義かどうかを確認します
+ `[isOfType](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-of-type.html)`: 属性値が文字列、数値、ブール値などの特定のタイプであるかどうかをチェックします。
+ `[size](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/size.html)`: コレクション内の要素数や文字列の長さなどの属性のサイズを取得します。
+ `[startsWith](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/starts-with.html)`: 文字列属性値が特定の部分文字列で始まるかどうかを確認します

次の例は、式で使用できる追加の関数とプロパティの使用を示しています。

```
attr("foo") contains "apple" // Specifies that the attribute value `foo` must be
                             // a list that contains an `apple` element or a string
                             // which contains the substring `apple`.

attr("bar").exists()         // Specifies that the `bar` must exist and have a
                             // value (including potentially `null`).

attr("baz").size lt 100      // Specifies that the attribute value `baz` must have
                             // a size of less than 100.

attr("qux") isOfType AttributeType.String // Specifies that the attribute `qux`
                                          // must have a string value.
```

### ソートキーフィルター
<a name="ddb-mapper-expressions-dsl-sort-key"></a>

ソートキー ( `query` オペレーションの `keyCondition`パラメータなど) のフィルタ式は、名前付き属性値を使用しません。フィルターでソートキーを使用するには、すべての比較`sortKey`で キーワードを使用する必要があります。次の例`attr("<sort key name>")`に示すように、 `sortKey`キーワードは を置き換えます。

```
sortKey startsWith "abc" // The sort key attribute value must begin with the
                         // substring `abc`.

sortKey isIn 0..99       // The sort key attribute value must be between 0
                         // and 99 (inclusive).
```

ソートキーフィルターをブールロジックと組み合わせることはできず、上記の比較のサブセットのみをサポートします。
+ [同等性と不等式](#ddb-mapper-expressions-dsl-eq-and-ineq): サポートされているすべての比較
+ [範囲とセット](#ddb-mapper-expressions-dsl-ranges-sets): サポートされているすべての比較
+ [ブールロジック](#ddb-mapper-expressions-dsl-boolean): サポートされていません
+ [関数とプロパティ](#ddb-mapper-expressions-dsl-functions): `startsWith` のみがサポートされています