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 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
80pub enum SortOrder {
81 Asc,
83 Desc,
85}
86
87#[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}