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(any(feature = "mysql", test)), allow(dead_code))]
23    pub(crate) default_order: Vec<(String, SortOrder)>,
24    pub(crate) soft_delete_field: Option<String>,
25    pub(crate) timestamp_fields: Option<TimestampFields>,
26}
27
28impl TableConfig {
29    pub(crate) fn get_field(&self, field_name: &str) -> Option<&FieldConfig> {
30        self.fields.get(field_name)
31    }
32
33    #[cfg(feature = "mysql")]
34    pub(crate) fn get_field_ref(&self, field_name: &str) -> Option<&yang_db::FieldRef> {
35        self.field_refs.get(field_name)
36    }
37}
38
39/// 编译后的索引配置。
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub(crate) struct IndexConfig {
42    pub(crate) name: Option<String>,
43    pub(crate) fields: Vec<String>,
44}
45
46impl IndexConfig {
47    pub(crate) fn new(name: Option<String>, fields: Vec<String>) -> Self {
48        Self { name, fields }
49    }
50}
51
52/// 排序方向。
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
54pub enum SortOrder {
55    /// 升序排序。
56    Asc,
57    /// 降序排序。
58    Desc,
59}
60
61/// 编译后的自动时间戳字段配置。
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub(crate) struct TimestampFields {
64    pub(crate) created_at: Option<String>,
65    pub(crate) updated_at: Option<String>,
66    pub(crate) deleted_at: Option<String>,
67}
68
69impl TimestampFields {
70    pub(crate) fn new(
71        created_at: Option<String>,
72        updated_at: Option<String>,
73        deleted_at: Option<String>,
74    ) -> Self {
75        Self {
76            created_at,
77            updated_at,
78            deleted_at,
79        }
80    }
81}