Skip to main content

yang_base/table/
table_config.rs

1//! Schema-first DSL 构建后的内部表配置。
2
3use super::FieldConfig;
4use crate::datasource::DatasourceId;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// 查询与 schema 同步共享的编译后表契约。
9#[derive(Debug, Clone)]
10pub(crate) struct TableConfig {
11    pub(crate) table_name: String,
12    /// 表所属逻辑数据源;默认 `default`。
13    pub(crate) datasource: DatasourceId,
14    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
15    pub(crate) table_ref: yang_db::TableRef,
16    pub(crate) display_name: String,
17    /// 兼容字段:单主键时等于唯一主键;复合主键时等于首个主键。
18    pub(crate) primary_key: String,
19    /// 完整主键列(按声明顺序);空表示无主键。
20    pub(crate) primary_keys: Vec<String>,
21    pub(crate) fields: HashMap<String, FieldConfig>,
22    /// 框架不建模、不读写、也不改写的存量列(仅 PostgreSQL)。
23    ///
24    /// 只用于校验列确实存在且类型符合声明;运行期 CRUD/UI 完全忽略。
25    #[cfg_attr(not(any(feature = "mysql", feature = "postgres")), allow(dead_code))]
26    pub(crate) opaque_columns: Vec<OpaqueColumnConfig>,
27    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
28    pub(crate) field_refs: HashMap<String, yang_db::FieldRef>,
29    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
30    pub(crate) unique_indexes: Vec<IndexConfig>,
31    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
32    pub(crate) indexes: Vec<IndexConfig>,
33    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
34    pub(crate) checks: Vec<CheckConfig>,
35    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
36    pub(crate) foreign_keys: Vec<ForeignKeyConfig>,
37    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
38    pub(crate) default_order: Vec<(String, SortOrder)>,
39    pub(crate) soft_delete_field: Option<String>,
40    pub(crate) timestamp_fields: Option<TimestampFields>,
41}
42
43/// 编译后的 CHECK 约束。
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub(crate) struct CheckConfig {
46    pub(crate) name: String,
47    pub(crate) expression: String,
48}
49
50/// 编译后的外键约束;删除和更新规则固定为 RESTRICT。
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub(crate) struct ForeignKeyConfig {
53    pub(crate) name: String,
54    pub(crate) columns: Vec<String>,
55    pub(crate) referenced_table: String,
56    pub(crate) referenced_columns: Vec<String>,
57}
58
59impl TableConfig {
60    pub(crate) fn get_field(&self, field_name: &str) -> Option<&FieldConfig> {
61        self.fields.get(field_name)
62    }
63
64    #[cfg(feature = "mysql")]
65    pub(crate) fn get_field_ref(&self, field_name: &str) -> Option<&yang_db::FieldRef> {
66        self.field_refs.get(field_name)
67    }
68}
69
70/// 框架不建模的存量列声明。
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub(crate) struct OpaqueColumnConfig {
73    pub(crate) name: String,
74    /// 与 PostgreSQL `format_type` 输出比较的受信类型文本(大小写/空白不敏感)。
75    pub(crate) sql_type: String,
76}
77
78/// 编译后的索引配置。
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub(crate) struct IndexConfig {
81    pub(crate) name: Option<String>,
82    pub(crate) fields: Vec<String>,
83    /// 表达式键(已校验的受信 SQL);与 `fields` 至少有一个非空。
84    pub(crate) expressions: Vec<String>,
85    /// PostgreSQL 部分索引谓词(`WHERE <predicate>`)。
86    pub(crate) predicate: Option<String>,
87    /// PostgreSQL 15+ 唯一索引的 `NULLS NOT DISTINCT` 语义;MySQL 明确不支持。
88    pub(crate) nulls_not_distinct: bool,
89}
90
91impl IndexConfig {
92    pub(crate) fn new(name: Option<String>, fields: Vec<String>, nulls_not_distinct: bool) -> Self {
93        Self {
94            name,
95            fields,
96            expressions: Vec::new(),
97            predicate: None,
98            nulls_not_distinct,
99        }
100    }
101
102    pub(crate) fn with_expressions(mut self, expressions: Vec<String>) -> Self {
103        self.expressions = expressions;
104        self
105    }
106
107    pub(crate) fn with_predicate(mut self, predicate: Option<String>) -> Self {
108        self.predicate = predicate;
109        self
110    }
111}
112
113/// 排序方向。
114#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
115pub enum SortOrder {
116    /// 升序排序。
117    Asc,
118    /// 降序排序。
119    Desc,
120}
121
122/// 编译后的自动时间戳字段配置。
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub(crate) struct TimestampFields {
125    pub(crate) created_at: Option<String>,
126    pub(crate) updated_at: Option<String>,
127    pub(crate) deleted_at: Option<String>,
128}
129
130impl TimestampFields {
131    pub(crate) fn new(
132        created_at: Option<String>,
133        updated_at: Option<String>,
134        deleted_at: Option<String>,
135    ) -> Self {
136        Self {
137            created_at,
138            updated_at,
139            deleted_at,
140        }
141    }
142}