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}
65
66impl IndexConfig {
67    pub(crate) fn new(name: Option<String>, fields: Vec<String>) -> Self {
68        Self { name, fields }
69    }
70}
71
72/// 排序方向。
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
74pub enum SortOrder {
75    /// 升序排序。
76    Asc,
77    /// 降序排序。
78    Desc,
79}
80
81/// 编译后的自动时间戳字段配置。
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub(crate) struct TimestampFields {
84    pub(crate) created_at: Option<String>,
85    pub(crate) updated_at: Option<String>,
86    pub(crate) deleted_at: Option<String>,
87}
88
89impl TimestampFields {
90    pub(crate) fn new(
91        created_at: Option<String>,
92        updated_at: Option<String>,
93        deleted_at: Option<String>,
94    ) -> Self {
95        Self {
96            created_at,
97            updated_at,
98            deleted_at,
99        }
100    }
101}