Skip to main content

yang_base/table/table_query/
build.rs

1//! 构造与运行期注入:两个 `new` 构造器(按 `mysql` feature 区分连接池签名)、
2//! 慢查询阈值 / request_id 链式注入,以及测试专用的无连接池构造器。
3
4use super::TableQuery;
5use crate::table::{QueryParams, RegexCache, TableConfig};
6use std::collections::HashSet;
7use std::sync::Arc;
8
9impl TableQuery {
10    /// 创建新的查询构建器
11    ///
12    /// # 参数
13    ///
14    /// - `table_config`:表配置引用
15    /// - `user_roles`:用户角色列表(`Arc<[String]>` 共享所有权)
16    /// - `pool`:数据库连接池引用(可选)
17    ///
18    /// # 返回值
19    ///
20    /// 返回新的 TableQuery 实例
21    #[cfg(feature = "mysql")]
22    pub(crate) fn new(
23        table_config: Arc<TableConfig>,
24        user_roles: Arc<[String]>,
25        pool: Option<Arc<sqlx::MySqlPool>>,
26    ) -> Self {
27        let user_roles_set: HashSet<String> = user_roles.iter().cloned().collect();
28        Self {
29            table_config,
30            user_roles,
31            user_roles_set,
32            query_params: QueryParams::new(),
33            include_trashed: false,
34            tenant_scope: None,
35            pool,
36            slow_threshold: None,
37            request_id: None,
38            regex_cache: RegexCache::default(),
39        }
40    }
41
42    /// 创建新的查询构建器(无数据库连接池)
43    ///
44    /// 当未启用 `mysql` feature 时使用此方法。
45    #[cfg(not(feature = "mysql"))]
46    pub(crate) fn new(
47        table_config: Arc<TableConfig>,
48        user_roles: Arc<[String]>,
49        _pool: Option<()>,
50    ) -> Self {
51        let user_roles_set: HashSet<String> = user_roles.iter().cloned().collect();
52        Self {
53            table_config,
54            user_roles,
55            user_roles_set,
56            query_params: QueryParams::new(),
57            include_trashed: false,
58            tenant_scope: None,
59            slow_threshold: None,
60            request_id: None,
61            regex_cache: RegexCache::default(),
62        }
63    }
64
65    /// 设置慢查询阈值(链式)。超过该耗时的单次执行会 `tracing::warn!`。
66    ///
67    /// 通常由 `ActionContext::table_query()` 从全局 `ObservabilityConfig` 注入;
68    /// 也可手动覆盖。`None`/不调用时不启用慢查询日志。
69    pub fn with_slow_threshold(mut self, threshold: Option<std::time::Duration>) -> Self {
70        self.slow_threshold = threshold;
71        self
72    }
73
74    /// 设置本次查询关联的 request_id(链式),用于慢查询日志串联。
75    pub fn with_request_id(mut self, request_id: crate::action::RequestId) -> Self {
76        self.request_id = Some(request_id);
77        self
78    }
79
80    /// 注入共享有界正则缓存(链式)。通常由 `ActionContext::table_query()` 从
81    /// [`crate::tools::Tools`] 注入;未注入时使用默认容量缓存。
82    pub fn with_regex_cache(mut self, cache: RegexCache) -> Self {
83        self.regex_cache = cache;
84        self
85    }
86
87    /// 创建仅有表配置、无数据库连接池的查询构建器(测试用)
88    ///
89    /// 使用空的用户角色列表,适合访问无权限限制字段的单元测试。
90    #[cfg(all(test, feature = "mysql"))]
91    pub(crate) fn new_without_pool(table_config: Arc<TableConfig>) -> Self {
92        Self::new(table_config, Arc::from(vec![]), None)
93    }
94}