

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AWS AppSync RDS 的解析器映射模板参考
<a name="resolver-mapping-template-reference-rds"></a>

 AWS AppSync RDS 解析器映射模板允许开发人员向适用于 Amazon Aurora Serverless 的数据 API 发送 SQL 查询并获取这些查询的结果。

## 请求映射模板
<a name="request-mapping-template"></a>

RDS 请求映射模板相当简单：

```
{
    "version": "2018-05-29",
    "statements": [],
    "variableMap": {},
    "variableTypeHintMap": {}
}
```

下面是 RDS 请求映射模板的 JSON 架构表示形式（解析后）：

```
{
    "definitions": {},
    "$schema": "https://json-schema.org/draft-07/schema#",
    "$id": "https://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
        "version",
        "statements",
        "variableMap"
    ],
    "properties": {
        "version": {
            "$id": "#/properties/version",
            "type": "string",
            "title": "The Version Schema",
            "default": "",
            "examples": [
                "2018-05-29"
            ],
            "enum": [
                "2018-05-29"
            ],
            "pattern": "^(.*)$"
        },
        "statements": {
            "$id": "#/properties/statements",
            "type": "array",
            "title": "The Statements Schema",
            "items": {
                "$id": "#/properties/statements/items",
                "type": "string",
                "title": "The Items Schema",
                "default": "",
                "examples": [
                    "SELECT * from BOOKS"
                ],
                "pattern": "^(.*)$"
            }
        },
        "variableMap": {
            "$id": "#/properties/variableMap",
            "type": "object",
            "title": "The Variablemap Schema"
        },
        "variableTypeHintMap": {
            "$id": "#/properties/variableTypeHintMap",
            "type": "object",
            "title": "The variableTypeHintMap Schema"
        }
    }
}
```

以下是一个具有静态查询的请求映射模板示例：

```
{
    "version": "2018-05-29",
    "statements": [
        "select title, isbn13 from BOOKS where author = 'Mark Twain'"
    ]
}
```

## 版本
<a name="version"></a>

version 字段是所有请求映射模板通用的，用于定义模板使用的版本。version 字段为必填项。“2018-05-29”值是 Amazon RDS 映射模板支持的唯一版本。

```
"version": "2018-05-29"
```

## 声明和 VariableMap
<a name="statements-variablemap"></a>

statements 数组是开发人员提供的查询的占位符。目前，每个请求映射模板最多支持两个查询。`variableMap` 是一个可选字段，它包含可用于使 SQL 语句更短且更易读的别名。例如，可以编写以下代码：

```
{
"version": "2018-05-29",
    "statements": [
        "insert into BOOKS VALUES (:AUTHOR, :TITLE, :ISBN13)",
        "select * from BOOKS WHERE isbn13 = :ISBN13"
    ],
    "variableMap": {
        ":AUTHOR": $util.toJson($ctx.args.newBook.author),
        ":TITLE": $util.toJson($ctx.args.newBook.title),
        ":ISBN13": $util.toJson($ctx.args.newBook.isbn13)
    }
}
```

AWS AppSync 将使用变量映射值来构造将发送到 Amazon Aurora 无服务器数据 API 的**[SqlParameterized](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_SqlParameter.html)**查询。SQL 语句是使用变量映射中提供的参数执行的，从而消除了 SQL 注入风险。

## VariableTypeHintMap
<a name="variabletypehintmap"></a>

`variableTypeHintMap` 是一个可选字段，它包含可用于发送 [SQL 参数](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_SqlParameter.html)类型提示的别名类型。这些类型提示避免了 SQL 语句中的显式转换，从而使这些语句变得更短。例如，可以编写以下代码：

```
{
    "version": "2018-05-29",
    "statements": [
        "insert into LOGINDATA VALUES (:ID, :TIME)",
        "select * from LOGINDATA WHERE id = :ID"
     ],
     "variableMap": {
        ":ID": $util.toJson($ctx.args.id),
        ":TIME": $util.toJson($ctx.args.time)
     },
     "variableTypeHintMap": {
        ":id": "UUID",
        ":time": "TIME"
     }
}
```

AWS AppSync 将使用变量映射值来构造发送到 Amazon Aurora 无服务器数据 API 的查询。它还使用 `variableTypeHintMap` 数据，并将类型的信息发送到 RDS。可以在[此处](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_SqlParameter.html)找到 RDS 支持的 `typeHints`。