Skip to main content

teaql_core/
meta.rs

1use crate::{DataType, default_table_name};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct PropertyDescriptor {
5    pub name: String,
6    pub data_type: DataType,
7    pub nullable: bool,
8    pub column_name: String,
9    pub is_id: bool,
10    pub is_version: bool,
11}
12
13impl PropertyDescriptor {
14    pub fn new(name: impl Into<String>, data_type: DataType) -> Self {
15        let name = name.into();
16        Self {
17            column_name: name.clone(),
18            name,
19            data_type,
20            nullable: true,
21            is_id: false,
22            is_version: false,
23        }
24    }
25
26    pub fn column_name(mut self, column_name: impl Into<String>) -> Self {
27        self.column_name = column_name.into();
28        self
29    }
30
31    pub fn not_null(mut self) -> Self {
32        self.nullable = false;
33        self
34    }
35
36    pub fn id(mut self) -> Self {
37        self.is_id = true;
38        self
39    }
40
41    pub fn version(mut self) -> Self {
42        self.is_version = true;
43        self
44    }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct RelationDescriptor {
49    pub name: String,
50    pub target_entity: String,
51    pub local_key: String,
52    pub foreign_key: String,
53    pub many: bool,
54    pub attach: bool,
55    pub delete_missing: bool,
56}
57
58impl RelationDescriptor {
59    pub fn new(name: impl Into<String>, target_entity: impl Into<String>) -> Self {
60        Self {
61            name: name.into(),
62            target_entity: target_entity.into(),
63            local_key: "id".to_owned(),
64            foreign_key: "id".to_owned(),
65            many: false,
66            attach: true,
67            delete_missing: true,
68        }
69    }
70
71    pub fn local_key(mut self, key: impl Into<String>) -> Self {
72        self.local_key = key.into();
73        self
74    }
75
76    pub fn foreign_key(mut self, key: impl Into<String>) -> Self {
77        self.foreign_key = key.into();
78        self
79    }
80
81    pub fn many(mut self) -> Self {
82        self.many = true;
83        self
84    }
85
86    pub fn attach(mut self) -> Self {
87        self.attach = true;
88        self
89    }
90
91    pub fn detached(mut self) -> Self {
92        self.attach = false;
93        self
94    }
95
96    pub fn delete_missing(mut self) -> Self {
97        self.delete_missing = true;
98        self
99    }
100
101    pub fn keep_missing(mut self) -> Self {
102        self.delete_missing = false;
103        self
104    }
105}
106
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub struct EntityDescriptor {
109    pub name: String,
110    pub table_name: String,
111    pub properties: Vec<PropertyDescriptor>,
112    pub relations: Vec<RelationDescriptor>,
113}
114
115impl EntityDescriptor {
116    pub fn new(name: impl Into<String>) -> Self {
117        let name = name.into();
118        Self {
119            table_name: default_table_name(&name),
120            name,
121            properties: Vec::new(),
122            relations: Vec::new(),
123        }
124    }
125
126    pub fn table_name(mut self, table_name: impl Into<String>) -> Self {
127        self.table_name = table_name.into();
128        self
129    }
130
131    pub fn property(mut self, property: PropertyDescriptor) -> Self {
132        self.properties.push(property);
133        self
134    }
135
136    pub fn relation(mut self, relation: RelationDescriptor) -> Self {
137        self.relations.push(relation);
138        self
139    }
140
141    pub fn property_by_name(&self, name: &str) -> Option<&PropertyDescriptor> {
142        self.properties
143            .iter()
144            .find(|property| property.name == name)
145    }
146
147    pub fn relation_by_name(&self, name: &str) -> Option<&RelationDescriptor> {
148        self.relations.iter().find(|relation| relation.name == name)
149    }
150
151    pub fn id_property(&self) -> Option<&PropertyDescriptor> {
152        self.properties.iter().find(|property| property.is_id)
153    }
154
155    pub fn version_property(&self) -> Option<&PropertyDescriptor> {
156        self.properties.iter().find(|property| property.is_version)
157    }
158
159    pub fn writable_properties(&self) -> impl Iterator<Item = &PropertyDescriptor> {
160        self.properties.iter().filter(|property| !property.is_id)
161    }
162}