feat(crud): 表无关的通用 CRUD 引擎

- list/all/detail/create/update/delete 六件套,行为逐条对齐上游 TdcCrudController
- 出参 camelCase、入参反查 f_ 列;null 值跳过;未知键静默忽略
- 过滤统一 CAST(col AS TEXT)=,规避上游 varchar 绑 smallint 的缺陷
- 冻结列只拒「值真的变化」,避免误伤整行回传的编辑弹窗
- 增补资源侧只读聚合体 detail_aggregate(fieldRules + maskRules),供上游前端 /{id}/Detail

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
adminandClaude Code committed 2026-10-24 22:50:07 +08:00
1 parent 70fe0c5cc2
commit 88464aaff9
3 files changed
+1088

No files matched your search

+780
View File
@@ -0,0 +1,780 @@
//! 表无关的通用 CRUD 引擎 —— 上游 `TdcCrudController` 六件套的 Rust 复刻。
//!
//! 上游 18 个控制器全部 `extends TdcCrudController`,只覆写「元数据」(关键字列 / 过滤列 /
//! 租户列 / 默认排序列)与少量动作钩子 ⇒ 本仓把公共部分抽成本引擎,**元数据**放
//! [`crate::registry`]。
//!
//! 红线:
//! - 表名只来自注册表(静态白名单);客户端输入只进**绑定参数**,绝不拼进 SQL。
//! - 所有 DB 访问持 `state.db_lock`(PGlite 单 in-process backend)。
//! - 出参 camelCase(上游 Jackson 属性名),入参反查 `f_*` 列名。
//! - 业务失败一律 HTTP 200 + body `code != 200`(走 [`ActionResult`] 业务信封)。
use std::collections::HashMap;
use axum::response::{IntoResponse, Response};
use pglite::ToSql;
use serde_json::{Map, Value};
use crate::auth::{require_menu_permission, Principal};
use crate::db::{
column_to_json_key, column_types, json_key_to_column, query_json_params, query_scalar_text, ALIVE,
};
use crate::envelope::{ActionResult, Pagination, CODE_SERVER_ERROR};
use crate::ids::next_id;
use crate::registry::{self, TableSpec};
use crate::state::AppState;
/// 系统列:请求体不得覆盖(对齐上游 `update` 里置 null 的那批 + 身份/排序列)。
pub const SYSTEM_COLUMNS: &[&str] = &[
"f_id",
"f_tenant_id",
"f_sort_code",
"f_creator_time",
"f_creator_user_id",
"f_last_modify_time",
"f_last_modify_user_id",
"f_delete_mark",
"f_delete_time",
"f_delete_user_id",
];
/// 分页默认条数(上游 `pageSize<=0 → 20`)。
pub const DEFAULT_PAGE_SIZE: i64 = 20;
/// 分页上限(上游 `Math.min(pageSize, 500)`)。
pub const MAX_PAGE_SIZE: i64 = 500;
/// `/All` 的上限(上游 `wrapper.last("limit 1000")`)。
pub const ALL_LIMIT: i64 = 1000;
/// 解析查询串:同名参数取**第一个**(对齐 `request.getParameter`),并做百分号解码。
pub fn parse_query(raw: Option<&str>) -> HashMap<String, String> {
let mut out: HashMap<String, String> = HashMap::new();
let raw = match raw {
Some(r) => r,
None => return out,
};
for pair in raw.split('&') {
if pair.is_empty() {
continue;
}
let (k, v) = match pair.split_once('=') {
Some((k, v)) => (k, v),
None => (pair, ""),
};
let key = percent_decode(k);
if out.contains_key(&key) {
continue;
}
out.insert(key, percent_decode(v));
}
out
}
fn percent_decode(s: &str) -> String {
let bytes = s.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'%' if i + 2 < bytes.len() => {
match u8::from_str_radix(std::str::from_utf8(&bytes[i + 1..i + 3]).unwrap_or(""), 16) {
Ok(b) => {
out.push(b);
i += 3;
}
Err(_) => {
out.push(bytes[i]);
i += 1;
}
}
}
b'+' => {
out.push(b' ');
i += 1;
}
b => {
out.push(b);
i += 1;
}
}
}
String::from_utf8_lossy(&out).to_string()
}
/// 取参数:trim 后空串 ⇒ `None`(对齐上游 `param()`)。
pub fn param(params: &HashMap<String, String>, name: &str) -> Option<String> {
params
.get(name)
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
}
/// 行数据(`f_*` 列名)⇒ 上游 Jackson 形状(camelCase 键)。
pub fn row_to_json(row: &Value) -> Value {
let mut out = Map::new();
if let Some(obj) = row.as_object() {
for (k, v) in obj {
out.insert(column_to_json_key(k), v.clone());
}
}
Value::Object(out)
}
/// 标量值 ⇒ 文本(用于「值真的变化」比较;非标量返回 `None`)。
fn scalar_text(v: &Value) -> Option<String> {
match v {
Value::String(s) => Some(s.clone()),
Value::Number(n) => Some(n.to_string()),
Value::Bool(b) => Some(b.to_string()),
_ => None,
}
}
/// 请求体(camelCase 键)⇒ `(列名, 文本值)` 列表。
///
/// - `null` 值跳过(等价 MyBatis-Plus 的 `NOT_NULL` 合并策略:null 表示「不覆盖」);
/// - 系统列跳过;
/// - 表里没有的键**静默忽略**(对齐 Spring Boot 默认 `FAIL_ON_UNKNOWN_PROPERTIES=false`)。
pub fn body_to_columns(body: &Value, types: &HashMap<String, String>) -> Vec<(String, String)> {
let obj = match body.as_object() {
Some(o) => o,
None => return Vec::new(),
};
let mut out = Vec::new();
for (key, value) in obj {
if value.is_null() {
continue;
}
let col = json_key_to_column(key);
if SYSTEM_COLUMNS.contains(&col.as_str()) {
continue;
}
if !types.contains_key(&col) {
continue;
}
let text = match value {
Value::String(s) => s.clone(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => n.to_string(),
other => other.to_string(),
};
out.push((col, text));
}
out
}
/// 组装 WHERE 子句与绑定参数(全部按 text 绑定,类型转换交给 SQL 里的 `CAST`)。
///
/// 🔴 `tenant_column` 非空时客户端传 `tenantId` ⇒ 拒绝(上游 `buildWrapper` 同口径)。
fn build_where(
spec: &TableSpec,
params: &HashMap<String, String>,
) -> Result<(String, Vec<String>), String> {
let mut clauses: Vec<String> = vec![ALIVE.to_string()];
if let Some(extra) = spec.extra_where {
clauses.push(format!("({})", extra));
}
if spec.tenant_column.is_some() && param(params, "tenantId").is_some() {
return Err("身份上下文不可由客户端指定(tenantId),请由固定实例/TDS 授权确定".to_string());
}
let mut binds: Vec<String> = Vec::new();
for (pname, col) in spec.filter_columns {
if let Some(v) = param(params, pname) {
binds.push(v);
let idx = binds.len();
clauses.push(format!("CAST({} AS TEXT) = CAST(${} AS TEXT)", col, idx));
}
}
if let Some(kw) = param(params, "keyword") {
if !spec.keyword_columns.is_empty() {
let mut parts = Vec::new();
for col in spec.keyword_columns {
binds.push(format!("%{}%", kw));
let idx = binds.len();
parts.push(format!("CAST({} AS TEXT) LIKE CAST(${} AS TEXT)", col, idx));
}
clauses.push(format!("({})", parts.join(" OR ")));
}
}
Ok((clauses.join(" AND "), binds))
}
fn bind_refs(binds: &[String]) -> Vec<&(dyn ToSql + Sync)> {
binds.iter().map(|s| s as &(dyn ToSql + Sync)).collect()
}
/// 当前操作人:内部令牌通道的 `X-Internal-Operator` > 会话身份 > 本地账号 > 本地引导身份 > `SYSTEM`。
pub fn operator(state: &AppState, principal: Option<&Principal>) -> String {
match principal {
Some(Principal::Internal { operator: Some(op) }) => op.clone(),
Some(Principal::Session { identity_id, .. }) => identity_id.clone(),
Some(Principal::Account { account, .. }) => account.clone(),
_ => {
if state.bootstrap.identity_id.is_empty() {
"SYSTEM".to_string()
} else {
state.bootstrap.identity_id.clone()
}
}
}
}
fn server_error(e: impl std::fmt::Display) -> Response {
ActionResult::fail_code(CODE_SERVER_ERROR, format!("服务异常: {}", e)).into_response()
}
/// 分页列表:`GET /api/ssw/tdc/{Entity}`。
pub async fn list(
state: &AppState,
spec: &'static TableSpec,
params: &HashMap<String, String>,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
let current = params
.get("currentPage")
.and_then(|v| v.trim().parse::<i64>().ok())
.filter(|v| *v > 0)
.unwrap_or(1);
let size = params
.get("pageSize")
.and_then(|v| v.trim().parse::<i64>().ok())
.filter(|v| *v > 0)
.map(|v| v.min(MAX_PAGE_SIZE))
.unwrap_or(DEFAULT_PAGE_SIZE);
let (where_clause, binds) = match build_where(spec, params) {
Ok(v) => v,
Err(msg) => return ActionResult::fail(msg).into_response(),
};
let refs = bind_refs(&binds);
let count_sql = format!(
"SELECT count(*)::text FROM {} WHERE {}",
spec.table, where_clause
);
let total = {
let _g = state.db_lock.lock().await;
query_scalar_text(state.db.as_ref(), &count_sql, &refs).await
};
let total: i64 = match total {
Ok(Some(t)) => t.trim().parse().unwrap_or(0),
Ok(None) => 0,
Err(e) => return server_error(e),
};
let offset = (current - 1) * size;
let sql = format!(
"SELECT * FROM {} WHERE {} ORDER BY {} DESC LIMIT {} OFFSET {}",
spec.table, where_clause, spec.default_order_column, size, offset
);
let rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &refs).await
};
let rows = match rows {
Ok(r) => r,
Err(e) => return server_error(e),
};
let list: Vec<Value> = rows.iter().map(row_to_json).collect();
ActionResult::page(
list,
Pagination {
current_page: current,
page_size: size,
total,
},
)
.into_response()
}
/// 全量列表:`GET /api/ssw/tdc/{Entity}/All`(裸数组,≤1000)。
pub async fn all(
state: &AppState,
spec: &'static TableSpec,
params: &HashMap<String, String>,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
let (where_clause, binds) = match build_where(spec, params) {
Ok(v) => v,
Err(msg) => return ActionResult::fail(msg).into_response(),
};
let refs = bind_refs(&binds);
let sql = format!(
"SELECT * FROM {} WHERE {} ORDER BY {} DESC LIMIT {}",
spec.table, where_clause, spec.default_order_column, ALL_LIMIT
);
let rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &refs).await
};
let rows = match rows {
Ok(r) => r,
Err(e) => return server_error(e),
};
ActionResult::ok(rows.iter().map(row_to_json).collect::<Vec<Value>>()).into_response()
}
/// 详情:`GET /api/ssw/tdc/{Entity}/{id}`。
pub async fn detail(
state: &AppState,
spec: &'static TableSpec,
id: &str,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
let sql = format!(
"SELECT * FROM {} WHERE f_id = CAST($1 AS character varying) AND {}",
spec.table, ALIVE
);
let rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &[&id]).await
};
let rows = match rows {
Ok(r) => r,
Err(e) => return server_error(e),
};
match rows.first() {
Some(row) => ActionResult::ok(row_to_json(row)).into_response(),
None => ActionResult::fail("记录不存在或已删除").into_response(),
}
}
/// 取一行(带 `ALIVE`)用于「改前取当前值」与「删前校验」。
async fn fetch_alive(
state: &AppState,
spec: &TableSpec,
id: &str,
) -> Result<Option<Value>, pglite::Error> {
let sql = format!(
"SELECT * FROM {} WHERE f_id = CAST($1 AS character varying) AND {}",
spec.table, ALIVE
);
let rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &[&id]).await?
};
Ok(rows.into_iter().next())
}
/// 新增:`POST /api/ssw/tdc/{Entity}`。
pub async fn create(
state: &AppState,
spec: &'static TableSpec,
body: &Value,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
if let Some(msg) = spec.no_create {
return ActionResult::fail(msg).into_response();
}
let types = {
let _g = state.db_lock.lock().await;
match column_types(state.db.as_ref(), spec.table).await {
Ok(t) => t,
Err(e) => return server_error(e),
}
};
let op = operator(state, principal);
let id = next_id();
let mut cols: Vec<String> = vec!["f_id".to_string()];
let mut vals: Vec<String> = Vec::new();
let mut binds: Vec<String> = vec![id.clone()];
vals.push(cast_param(&types, "f_id", 1));
for (col, text) in body_to_columns(body, &types) {
binds.push(text);
let idx = binds.len();
cols.push(col.clone());
vals.push(cast_param(&types, &col, idx));
}
// 服务端补默认值(请求体已给该键 ⇒ 不补)。
for (key, raw) in registry::create_defaults(spec.api_name) {
if body.get(key).is_some() {
continue;
}
let col = json_key_to_column(key);
binds.push(raw.replace("{id}", &id));
let idx = binds.len();
cols.push(col.clone());
vals.push(cast_param(&types, &col, idx));
}
for key in registry::create_now_columns(spec.api_name) {
if body.get(key).is_some() {
continue;
}
cols.push(json_key_to_column(key));
vals.push("now()".to_string());
}
cols.push("f_creator_time".to_string());
vals.push("now()".to_string());
cols.push("f_creator_user_id".to_string());
binds.push(op.clone());
let idx = binds.len();
vals.push(cast_param(&types, "f_creator_user_id", idx));
cols.push("f_last_modify_time".to_string());
vals.push("now()".to_string());
cols.push("f_last_modify_user_id".to_string());
binds.push(op);
let idx = binds.len();
vals.push(cast_param(&types, "f_last_modify_user_id", idx));
cols.push("f_delete_mark".to_string());
vals.push("NULL".to_string());
let sql = format!(
"INSERT INTO {} ({}) VALUES ({})",
spec.table,
cols.join(", "),
vals.join(", ")
);
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("新增成功", id).into_response(),
Err(e) => server_error(format!("写入失败: {}", e)),
}
}
/// 修改:`PUT /api/ssw/tdc/{Entity}/{id}`。
pub async fn update(
state: &AppState,
spec: &'static TableSpec,
id: &str,
body: &Value,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
if let Some(msg) = spec.no_update {
return ActionResult::fail(msg).into_response();
}
let current = match fetch_alive(state, spec, id).await {
Ok(v) => v,
Err(e) => return server_error(e),
};
let current = match current {
Some(v) => v,
None => return ActionResult::fail("记录不存在或已删除").into_response(),
};
// 冻结列:只拒「值真的变化」(上游 `changedAuthorityColumn` 口径)。
for fc in spec.frozen_columns {
let key = column_to_json_key(fc.column);
if let Some(incoming) = body.get(&key) {
if !incoming.is_null() {
let cur_txt = current.get(fc.column).and_then(scalar_text);
let in_txt = scalar_text(incoming);
if in_txt != cur_txt {
return ActionResult::fail(format!(
"不允许修改「{}」:该列由平台下发/本端服务写入,不得经编辑入口篡改",
fc.label
))
.into_response();
}
}
}
}
let types = {
let _g = state.db_lock.lock().await;
match column_types(state.db.as_ref(), spec.table).await {
Ok(t) => t,
Err(e) => return server_error(e),
}
};
let op = operator(state, principal);
let mut sets: Vec<String> = Vec::new();
let mut binds: Vec<String> = Vec::new();
for (col, text) in body_to_columns(body, &types) {
binds.push(text);
let idx = binds.len();
sets.push(format!("{} = {}", col, cast_param(&types, &col, idx)));
}
sets.push("f_last_modify_time = now()".to_string());
binds.push(op);
let idx = binds.len();
sets.push(format!(
"f_last_modify_user_id = {}",
cast_param(&types, "f_last_modify_user_id", idx)
));
binds.push(id.to_string());
let id_idx = binds.len();
sets.push(format!("f_id = CAST(${} AS character varying)", id_idx));
let sql = format!(
"UPDATE {} SET {} WHERE f_id = CAST(${} AS character varying) AND {}",
spec.table,
sets.join(", "),
id_idx,
ALIVE
);
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("修改成功", id.to_string()).into_response(),
Err(e) => server_error(format!("写入失败: {}", e)),
}
}
/// 逻辑删除:`DELETE /api/ssw/tdc/{Entity}/{id}`。
pub async fn delete(
state: &AppState,
spec: &'static TableSpec,
id: &str,
principal: Option<&Principal>,
) -> Response {
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
if let Some(msg) = spec.no_delete {
return ActionResult::fail(msg).into_response();
}
let current = match fetch_alive(state, spec, id).await {
Ok(v) => v,
Err(e) => return server_error(e),
};
if current.is_none() {
return ActionResult::fail("记录不存在或已删除").into_response();
}
let types = {
let _g = state.db_lock.lock().await;
match column_types(state.db.as_ref(), spec.table).await {
Ok(t) => t,
Err(e) => return server_error(e),
}
};
let op = operator(state, principal);
let mut binds: Vec<String> = vec![op];
let op_idx = binds.len();
binds.push(id.to_string());
let id_idx = binds.len();
let sql = format!(
"UPDATE {} SET f_delete_mark = 1, f_delete_time = now(), f_delete_user_id = {} \
WHERE f_id = CAST(${} AS character varying) AND {}",
spec.table,
cast_param(&types, "f_delete_user_id", op_idx),
id_idx,
ALIVE
);
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("删除成功", id.to_string()).into_response(),
Err(e) => server_error(format!("写入失败: {}", e)),
}
}
/// 绑定参数 ⇒ `CAST(CAST($n AS text) AS <列类型>)`。
///
/// 🔴 **内层 `CAST($n AS text)` 是必需的**:Rust 侧只有 `String`,而 PG 会按
/// `CAST($n AS <列类型>)` 把 `$n` 推断成那个类型(`smallint` / `int4` / `numeric` …)
/// ⇒ wire 层类型不匹配(`cannot convert between the Rust type String and the
/// Postgres type int2`)。先钉死成 text、再**显式**转成列类型(text→任意类型
/// 的显式转换 PG 允许)⇒ 一律按 text 送、类型转换全部交给 SQL。
fn cast_param(types: &HashMap<String, String>, col: &str, idx: usize) -> String {
let ty = types.get(col).cloned().unwrap_or_else(|| "text".to_string());
format!("CAST(CAST(${} AS text) AS {})", idx, ty)
}
/// 有子表聚合的实体 —— 上游 `ResourceService.withChildren`。
///
/// 🔴 目前**只有资源侧**(`Resource` / `Dataset` 共用 `tdc_resource`)有子表;
/// 其余 16 个实体上游没有 `/Detail` 端点 ⇒ 走 [`detail`] 原样。
///
/// 返回 `(输出 JSON 键, 子表名)`;顺序即输出顺序(上游 `fieldRules` 在前、`maskRules` 在后)。
fn child_tables(spec: &TableSpec) -> &'static [(&'static str, &'static str)] {
match spec.api_name {
"Resource" | "Dataset" => &[
("fieldRules", "tdc_resource_field"),
("maskRules", "tdc_resource_mask_rule"),
],
_ => &[],
}
}
/// `GET /api/ssw/tdc/{Entity}/{id}/Detail` —— 上游 `ResourceController` / `DatasetController`
/// 的**只读聚合体**(`ResourceService.withChildren`):资源行 + 两张子表。
///
/// 与 [`detail`] 的差别**只有**「多挂两个子表」;无子表的实体完全等价于 `detail()`。
pub async fn detail_aggregate(
state: &AppState,
spec: &'static TableSpec,
id: &str,
principal: Option<&Principal>,
) -> Response {
let children = child_tables(spec);
if children.is_empty() {
return detail(state, spec, id, principal).await;
}
if let Err(resp) = require_menu_permission(principal) {
return resp;
}
let sql = format!(
"SELECT * FROM {} WHERE f_id = CAST($1 AS character varying) AND {}",
spec.table, ALIVE
);
let rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &[&id]).await
};
let rows = match rows {
Ok(r) => r,
Err(e) => return server_error(e),
};
let row = match rows.first() {
Some(row) => row,
None => return ActionResult::fail("记录不存在或已删除").into_response(),
};
let mut data = row_to_json(row);
for (key, table) in children {
let sql = format!(
"SELECT * FROM {} WHERE f_resource_id = CAST($1 AS character varying) ORDER BY f_sort_code",
table
);
let child_rows = {
let _g = state.db_lock.lock().await;
query_json_params(state.db.as_ref(), &sql, &[&id]).await
};
let child_rows = match child_rows {
Ok(r) => r,
Err(e) => return server_error(e),
};
let list: Vec<Value> = child_rows.iter().map(row_to_json).collect();
if let Some(obj) = data.as_object_mut() {
obj.insert((*key).to_string(), Value::Array(list));
}
}
ActionResult::ok(data).into_response()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::registry;
use serde_json::json;
#[test]
fn parse_query_takes_first_occurrence_and_decodes() {
let q = parse_query(Some("keyword=abc&keyword=def&name=%E4%B8%AD%E6%96%87&empty="));
assert_eq!(q.get("keyword").unwrap(), "abc");
assert_eq!(q.get("name").unwrap(), "中文");
assert_eq!(q.get("empty").unwrap(), "");
assert_eq!(parse_query(None).len(), 0);
}
#[test]
fn param_trims_and_maps_empty_to_none() {
let mut m = HashMap::new();
m.insert("a".to_string(), " x ".to_string());
m.insert("b".to_string(), " ".to_string());
assert_eq!(param(&m, "a").as_deref(), Some("x"));
assert_eq!(param(&m, "b"), None);
assert_eq!(param(&m, "missing"), None);
}
#[test]
fn row_to_json_converts_f_columns_to_camel_case() {
let row = json!({"f_id": "1", "f_task_no": "T-1", "f_create_time": "x"});
let out = row_to_json(&row);
assert_eq!(out["id"], "1");
assert_eq!(out["taskNo"], "T-1");
assert_eq!(out["createTime"], "x");
assert!(out.get("f_id").is_none());
}
#[test]
fn body_to_columns_skips_null_system_and_unknown_keys() {
let mut types = HashMap::new();
types.insert("f_name".to_string(), "character varying(200)".to_string());
types.insert("f_id".to_string(), "character varying(50)".to_string());
types.insert("f_creator_time".to_string(), "timestamp without time zone".to_string());
let body = json!({
"name": "产品",
"id": "999",
"creatorTime": "2020-01-01",
"nope": "x",
"count": 3
});
let cols = body_to_columns(&body, &types);
assert_eq!(cols, vec![("f_name".to_string(), "产品".to_string())]);
}
#[test]
fn build_where_rejects_client_tenant_id() {
let spec = registry::find("Product").unwrap();
let mut m = HashMap::new();
m.insert("tenantId".to_string(), "x".to_string());
let err = build_where(spec, &m).unwrap_err();
assert_eq!(
err,
"身份上下文不可由客户端指定(tenantId),请由固定实例/TDS 授权确定"
);
}
#[test]
fn build_where_builds_keyword_or_filters() {
let spec = registry::find("Product").unwrap();
let mut m = HashMap::new();
m.insert("keyword".to_string(), "数据".to_string());
m.insert("category".to_string(), "A".to_string());
let (clause, binds) = build_where(spec, &m).unwrap();
assert!(clause.starts_with(ALIVE));
assert!(clause.contains("CAST(f_category AS TEXT) = CAST($1 AS TEXT)"));
assert!(clause.contains("CAST(f_name AS TEXT) LIKE CAST($2 AS TEXT)"));
assert!(clause.contains(" OR "));
// 过滤参数先绑($1),随后**每个关键字列各绑一个**(OR 展开 ⇒ N 个占位符)。
// Product 有 4 个关键字列(f_name/f_product_code/f_resource_name/f_tags)。
assert_eq!(binds[0], "A".to_string());
assert_eq!(binds.len(), 1 + spec.keyword_columns.len());
assert!(binds[1..].iter().all(|b| b == "%数据%"));
}
#[test]
fn identity_table_allows_tenant_id_param() {
let spec = registry::find("Identity").unwrap();
let mut m = HashMap::new();
m.insert("tenantId".to_string(), "x".to_string());
assert!(
build_where(spec, &m).is_ok(),
"Identity 的 tenant_column 为 null,不该拒"
);
}
#[test]
fn only_resource_side_entities_have_child_tables() {
let res = registry::find("Resource").unwrap();
let ds = registry::find("Dataset").unwrap();
let prod = registry::find("Product").unwrap();
assert_eq!(
child_tables(res),
&[
("fieldRules", "tdc_resource_field"),
("maskRules", "tdc_resource_mask_rule")
]
);
assert_eq!(child_tables(ds), child_tables(res));
assert!(child_tables(prod).is_empty());
}
}
+1
View File
@@ -9,6 +9,7 @@
pub mod auth;
pub mod config;
pub mod crud;
pub mod db;
pub mod domain;
pub mod envelope;
+307
View File
@@ -0,0 +1,307 @@
//! CRUD 引擎端到端:新增 → 列表 → 关键字 → 过滤 → 修改 → 删除 → 各类拒因。
//!
//! ⚠️ 本测试二进制内共用一个 PGlite 实例(pglite-rs 每进程只允许一个)。
use std::collections::HashMap;
use std::sync::Arc;
use pglite::PGlite;
use serde_json::{json, Value};
use tdc_connector::auth::Principal;
use tdc_connector::config::Config;
use tdc_connector::crud;
use tdc_connector::registry;
use tdc_connector::session::SessionStore;
use tdc_connector::state::{AppState, BootstrapIdentity};
use tokio::sync::OnceCell;
static DB: OnceCell<Arc<PGlite>> = OnceCell::const_new();
async fn shared_db() -> Arc<PGlite> {
DB.get_or_init(|| async {
let db = Arc::new(PGlite::open_temp().await.expect("打开临时 PGlite 失败"));
tdc_connector::db::ensure_schema(db.as_ref())
.await
.expect("建表失败");
db
})
.await
.clone()
}
async fn test_state() -> AppState {
let db = shared_db().await;
let mut config = Config::from_env(std::env::current_dir().unwrap());
config.identity_code = "TEST-CONNECTOR".to_string();
AppState::new(
config,
db,
Arc::new(SessionStore::new()),
BootstrapIdentity {
identity_id: "TEST-OP".to_string(),
identity_code: "TEST-CONNECTOR".to_string(),
issued_secret: None,
},
)
}
fn principal() -> Principal {
Principal::Session {
identity_id: "TEST-OP".to_string(),
identity_code: "TEST-CONNECTOR".to_string(),
}
}
async fn body_of(resp: axum::response::Response) -> Value {
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.expect("读取响应体失败");
serde_json::from_slice(&bytes).expect("响应体不是 JSON")
}
#[tokio::test]
async fn product_crud_roundtrip() {
let state = test_state().await;
let spec = registry::find("Product").unwrap();
// 新增
let resp = crud::create(
&state,
spec,
&json!({"name": "测试产品", "productCode": "P-001", "status": "ENABLED"}),
Some(&principal()),
)
.await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200);
assert_eq!(body["msg"], "新增成功");
let id = body["data"].as_str().expect("应返回 id").to_string();
assert!(!id.is_empty());
assert!(id.chars().all(|c| c.is_ascii_digit()), "id 应是雪花号十进制串");
// 列表(关键字)
let mut params = HashMap::new();
params.insert("keyword".to_string(), "测试产品".to_string());
let resp = crud::list(&state, spec, &params, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200);
assert_eq!(body["data"]["pagination"]["total"], 1);
let first = &body["data"]["list"][0];
assert_eq!(first["name"], "测试产品");
assert_eq!(first["productCode"], "P-001");
assert!(first.get("f_name").is_none(), "响应键必须是 camelCase");
// 过滤(category 命中不到 ⇒ total 0)
let mut params = HashMap::new();
params.insert("category".to_string(), "不存在的分类".to_string());
let resp = crud::list(&state, spec, &params, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["data"]["pagination"]["total"], 0);
// tenantId 拒因
let mut params = HashMap::new();
params.insert("tenantId".to_string(), "x".to_string());
let resp = crud::list(&state, spec, &params, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(
body["msg"],
"身份上下文不可由客户端指定(tenantId),请由固定实例/TDS 授权确定"
);
// 修改
let resp = crud::update(
&state,
spec,
&id,
&json!({"name": "改名后的产品"}),
Some(&principal()),
)
.await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200);
assert_eq!(body["msg"], "修改成功");
let resp = crud::detail(&state, spec, &id, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["data"]["name"], "改名后的产品");
assert_eq!(body["data"]["productCode"], "P-001", "未传的字段保持原值");
// 删除(逻辑删除)
let resp = crud::delete(&state, spec, &id, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200);
assert_eq!(body["msg"], "删除成功");
let resp = crud::detail(&state, spec, &id, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(body["msg"], "记录不存在或已删除");
}
#[tokio::test]
async fn readonly_and_guarded_entities_reject_writes() {
let state = test_state().await;
// Dataset:派生态只读
let spec = registry::find("Dataset").unwrap();
let resp = crud::create(&state, spec, &json!({"name": "x"}), Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(body["msg"], "数据集为只读视图,不支持新增/编辑/删除");
// DeliveryTask:直建入口已废弃
let spec = registry::find("DeliveryTask").unwrap();
let resp = crud::create(&state, spec, &json!({"taskNo": "T-1"}), Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert!(body["msg"]
.as_str()
.unwrap()
.starts_with("该入口已废弃:交付任务只能由发起方经平台发起"));
// AuditLog:允许写,但不许改/删
let spec = registry::find("AuditLog").unwrap();
let resp = crud::create(&state, spec, &json!({"action": "LOGIN"}), Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200, "审计日志允许 HTTP 写入:{}", body);
let id = body["data"].as_str().unwrap().to_string();
let resp = crud::update(&state, spec, &id, &json!({"action": "X"}), Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(body["msg"], "审计日志不允许修改");
let resp = crud::delete(&state, spec, &id, Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(body["msg"], "审计日志不允许删除");
}
#[tokio::test]
async fn delivery_task_frozen_columns_cannot_be_changed() {
let state = test_state().await;
let spec = registry::find("DeliveryTask").unwrap();
// 写入口被上游废弃 ⇒ 测试里直接插一行
{
let _g = state.db_lock.lock().await;
state
.db
.query(
"INSERT INTO tdc_delivery_task (f_id, f_task_no, f_contract_id, f_status, f_creator_time) \
VALUES (CAST('T-1' AS character varying), CAST('TASK-1' AS character varying), \
CAST('C-1' AS character varying), CAST('READY' AS character varying), now())",
&[],
)
.await
.expect("插入测试行失败");
}
// 改冻结列(status)⇒ 拒
let resp = crud::update(
&state,
spec,
"T-1",
&json!({"status": "DONE"}),
Some(&principal()),
)
.await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(
body["msg"],
"不允许修改「任务状态」:该列由平台下发/本端服务写入,不得经编辑入口篡改"
);
// 原样回传(值没变)⇒ 放行
let resp = crud::update(
&state,
spec,
"T-1",
&json!({"status": "READY"}),
Some(&principal()),
)
.await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200, "值没变不应误伤:{}", body);
// 删 ⇒ 拒(审计证据)
let resp = crud::delete(&state, spec, "T-1", Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400);
assert_eq!(body["msg"], "交付任务记录是审计证据,不允许删除");
}
#[tokio::test]
async fn resource_detail_aggregate_carries_field_rules_and_mask_rules() {
let state = test_state().await;
let spec = registry::find("Resource").unwrap();
{
let _g = state.db_lock.lock().await;
state
.db
.query(
"INSERT INTO tdc_resource (f_id, f_name, f_resource_code, f_mount_status) \
VALUES (CAST('R-1' AS character varying), CAST('测试资源' AS character varying), \
CAST('RC-1' AS character varying), CAST('MOUNTED' AS character varying))",
&[],
)
.await
.expect("插入资源失败");
state
.db
.query(
"INSERT INTO tdc_resource_field \
(f_id, f_resource_id, f_field_name, f_field_type, f_field_constraint, f_remark, f_sort_code) \
VALUES (CAST('RF-1' AS character varying), CAST('R-1' AS character varying), \
CAST('id_card' AS character varying), CAST('varchar' AS character varying), \
CAST('NOT NULL' AS character varying), CAST('身份证号' AS character varying), 1)",
&[],
)
.await
.expect("插入字段失败");
state
.db
.query(
"INSERT INTO tdc_resource_mask_rule \
(f_id, f_resource_id, f_field_name, f_field_type, f_mask_type, f_origin_value, f_replace_value, f_sort_code) \
VALUES (CAST('RM-1' AS character varying), CAST('R-1' AS character varying), \
CAST('id_card' AS character varying), CAST('STRING' AS character varying), \
CAST('MD5' AS character varying), CAST('13800000000' AS character varying), \
CAST('***' AS character varying), 1)",
&[],
)
.await
.expect("插入脱敏规则失败");
}
let resp = crud::detail_aggregate(&state, spec, "R-1", Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 200, "聚合详情应成功:{}", body);
let data = &body["data"];
assert_eq!(data["id"], "R-1");
assert_eq!(data["name"], "测试资源");
let fields = data["fieldRules"].as_array().expect("应有 fieldRules 数组");
assert_eq!(fields.len(), 1, "字段子表条数:{}", body);
assert_eq!(fields[0]["fieldName"], "id_card");
assert_eq!(fields[0]["fieldType"], "varchar");
assert_eq!(fields[0]["fieldConstraint"], "NOT NULL");
assert_eq!(fields[0]["remark"], "身份证号");
let masks = data["maskRules"].as_array().expect("应有 maskRules 数组");
assert_eq!(masks.len(), 1, "脱敏子表条数:{}", body);
assert_eq!(masks[0]["fieldName"], "id_card");
assert_eq!(masks[0]["fieldType"], "STRING");
assert_eq!(masks[0]["maskType"], "MD5");
assert_eq!(masks[0]["originValue"], "13800000000");
assert_eq!(masks[0]["replaceValue"], "***");
// 查无 ⇒ 业务失败(HTTP 200 + code != 200)
let resp = crud::detail_aggregate(&state, spec, "R-NOT-EXIST", Some(&principal())).await;
let body = body_of(resp).await;
assert_eq!(body["code"], 400, "查无应是业务失败:{}", body);
}