存储过程调用
存储过程适合封装复杂的数据库逻辑——多步操作、条件分支、数据验证和聚合计算。数据墙DBW 支持将存储过程作为实体公开,通过 REST 和 GraphQL 调用。
数据库支持
| 数据库 | 存储过程支持 |
|---|---|
| SQL Server | 支持 |
| PostgreSQL | 不支持 |
| MySQL | 不支持 |
存储过程当前仅 SQL Server 可用。
配置存储过程
存储过程的配置有四个关键点:source.type 设为 "stored-procedure"、参数定义、HTTP 方法指定和 execute 权限:
json
{
"GetCowrittenBooksByAuthor": {
"source": {
"type": "stored-procedure",
"object": "dbo.stp_get_all_cowritten_books_by_author",
"parameters": [
{
"name": "searchType",
"required": false,
"default": "all",
"description": "搜索类型:all / fiction / nonfiction"
}
]
},
"rest": { "methods": ["GET"] },
"graphql": { "operation": "query" },
"permissions": [
{ "role": "anonymous", "actions": ["execute"] }
]
}
}参数格式
使用数组格式定义参数:
| 字段 | 必需 | 说明 |
|---|---|---|
name | 是 | 参数名称(不带 @ 前缀) |
required | 否 | 是否为必需参数 |
default | 否 | 未传参时的默认值 |
description | 否 | 参数描述(MCP 和文档使用) |
CLI 添加存储过程
bash
dab add GetCowrittenBooksByAuthor \
--source "dbo.stp_get_all_cowritten_books_by_author" \
--source.type "stored-procedure" \
--parameters.name "searchType" \
--parameters.required "false" \
--parameters.default "all" \
--parameters.description "搜索类型" \
--permissions "anonymous:execute" \
--rest.methods "get"REST 访问
GET 方式
参数通过 URL 查询字符串传递:
bash
curl "http://localhost:5000/api/GetCowrittenBooksByAuthor?searchType=fiction"GET 适用于无副作用的查询类存储过程。
POST 方式
参数通过 JSON 请求体传递:
bash
curl -X POST http://localhost:5000/api/GetCowrittenBooksByAuthor \
-H "Content-Type: application/json" \
-d '{"searchType": "fiction"}'POST 是默认方式。如果 rest.methods 未指定,默认只支持 POST。
同时支持 GET 和 POST
json
{
"rest": { "methods": ["GET", "POST"] }
}禁用 REST
如果存储过程只打算通过 GraphQL 或 MCP 调用,可以关闭 REST:
json
{
"rest": { "enabled": false }
}GraphQL 访问
存储过程在 GraphQL 中自动以 execute 前缀命名,参数作为 GraphQL 参数传递:
graphql
{
executeGetCowrittenBooksByAuthor(searchType: "fiction") {
id
title
authorName
}
}选择挂载位置
通过 graphql.operation 控制存储过程出现在 Query 还是 Mutation 下:
json
{
"graphql": { "operation": "query" }
}| 值 | 效果 |
|---|---|
"query" | 出现在 Query 类型下,适合无副作用的查询 |
"mutation" | 出现在 Mutation 类型下,适合有数据修改的过程(默认) |
差异仅影响架构组织,不影响功能——无论挂载在哪个位置,调用方式和返回结果都一样。
变量形式
graphql
query GetBooks($type: String) {
executeGetCowrittenBooksByAuthor(searchType: $type) {
id
title
}
}MCP 自定义工具
存储过程可以注册为 MCP 命名工具,供 AI 代理直接按名称调用:
json
{
"GetBookById": {
"source": {
"type": "stored-procedure",
"object": "dbo.get_book_by_id"
},
"mcp": { "custom-tool": true },
"permissions": [
{ "role": "anonymous", "actions": ["execute"] }
]
}
}bash
dab add GetBookById \
--source "dbo.get_book_by_id" \
--source.type "stored-procedure" \
--permissions "anonymous:execute" \
--mcp.custom-tool true代理会看到名为 GetBookById 的独立工具,带有参数说明,可以直接调用——不需要通过通用 execute_entity 工具。
权限
存储过程使用专用的 execute 操作:
json
{
"permissions": [
{ "role": "anonymous", "actions": ["execute"] }
]
}通配符 * 对存储过程仅扩展为 execute,不会意外授予 CRUD 权限。如果需要同时限制只有特定角色才能调用,在 permissions 中配置即可。
重要限制
| 限制 | 说明 |
|---|---|
| 仅返回第一个结果集 | 多结果集的存储过程只返回第一个 |
| 不支持分页/筛选/排序 | $filter、$orderby、$first/$after 参数无效 |
| 不支持关系 | 存储过程实体不能配置 relationships |
| 不支持按主键查询 | 没有 by_pk 查询 |
| 不支持参数级授权 | 所有角色看到相同参数,不能按角色限制参数访问 |
| 需要元数据 | SQL Server 需要 sys.dm_exec_describe_first_result_set 的元数据来推断返回结构 |
如果存储过程的返回结果也需要分页筛选,考虑将其逻辑改写为视图,以视图实体的方式公开。
请求参数规则
- 有默认值的参数可以省略——省略时使用默认值。
required: true的参数必须在每次请求中提供,省略会导致错误。- 不支持默认值的参数也必须提供。
