yang_base/table/
table_config.rs1use super::FieldConfig;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[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#[derive(Debug, Clone, PartialEq, Eq)]
34pub(crate) struct CheckConfig {
35 pub(crate) name: String,
36 pub(crate) expression: String,
37}
38
39#[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#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
74pub enum SortOrder {
75 Asc,
77 Desc,
79}
80
81#[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}