Skip to main content

yang_base/table/
table_config.rs

1//! Schema-first DSL 构建后的内部表配置。
2
3use super::FieldConfig;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7/// 查询与 schema 同步共享的编译后表契约。
8#[derive(Debug, Clone)]
9pub(crate) struct TableConfig {
10    pub(crate) table_name: String,
11    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
12    pub(crate) table_ref: yang_db::TableRef,
13    pub(crate) display_name: String,
14    pub(crate) primary_key: String,
15    pub(crate) fields: HashMap<String, FieldConfig>,
16    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
17    pub(crate) field_refs: HashMap<String, yang_db::FieldRef>,
18    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
19    pub(crate) unique_indexes: Vec<IndexConfig>,
20    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
21    pub(crate) indexes: Vec<IndexConfig>,
22    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
23    pub(crate) checks: Vec<CheckConfig>,
24    #[cfg_attr(not(feature = "mysql"), allow(dead_code))]
25    pub(crate) foreign_keys: Vec<ForeignKeyConfig>,
26    #[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
27    pub(crate) default_order: Vec<(String, SortOrder)>,
28    pub(crate) soft_delete_field: Option<String>,
29    pub(crate) timestamp_fields: Option<TimestampFields>,
30}
31
32/// 编译后的 CHECK 约束。
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub(crate) struct CheckConfig {
35    pub(crate) name: String,
36    pub(crate) expression: String,
37}
38
39/// 编译后的外键约束;删除和更新规则固定为 RESTRICT。
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub(crate) struct ForeignKeyConfig {
42    pub(crate) name: String,
43    pub(crate) columns: Vec<String>,
44    pub(crate) referenced_table: String,
45    pub(crate) referenced_columns: Vec<String>,
46}
47
48impl TableConfig {
49    pub(crate) fn get_field(&self, field_name: &str) -> Option<&FieldConfig> {
50        self.fields.get(field_name)
51    }
52
53    #[cfg(feature = "mysql")]
54    pub(crate) fn get_field_ref(&self, field_name: &str) -> Option<&yang_db::FieldRef> {
55        self.field_refs.get(field_name)
56    }
57}
58
59/// 编译后的索引配置。
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub(crate) struct IndexConfig {
62    pub(crate) name: Option<String>,
63    pub(crate) fields: Vec<String>,
64    /// PostgreSQL 15+ 唯一索引的 `NULLS NOT DISTINCT` 语义;MySQL 明确不支持。
65    pub(crate) nulls_not_distinct: bool,
66}
67
68impl IndexConfig {
69    pub(crate) fn new(name: Option<String>, fields: Vec<String>, nulls_not_distinct: bool) -> Self {
70        Self {
71            name,
72            fields,
73            nulls_not_distinct,
74        }
75    }
76}
77
78/// 排序方向。
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
80pub enum SortOrder {
81    /// 升序排序。
82    Asc,
83    /// 降序排序。
84    Desc,
85}
86
87/// 编译后的自动时间戳字段配置。
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub(crate) struct TimestampFields {
90    pub(crate) created_at: Option<String>,
91    pub(crate) updated_at: Option<String>,
92    pub(crate) deleted_at: Option<String>,
93}
94
95impl TimestampFields {
96    pub(crate) fn new(
97        created_at: Option<String>,
98        updated_at: Option<String>,
99        deleted_at: Option<String>,
100    ) -> Self {
101        Self {
102            created_at,
103            updated_at,
104            deleted_at,
105        }
106    }
107}