schema_model/model/
key.rs1use crate::model::types::KeyType;
2
3#[derive(Debug, Clone)]
4pub struct KeyColumn {
5 name: String,
6}
7
8impl KeyColumn {
9 pub fn new<S: Into<String>>(name: S) -> Self {
10 Self { name: name.into() }
11 }
12
13 pub fn name(&self) -> &str {
14 &self.name
15 }
16}
17
18#[derive(Debug, Clone)]
19pub struct Key {
20 key_type: KeyType,
21 columns: Vec<KeyColumn>,
22 cluster: bool,
23 compress: bool,
24 unique: bool,
25 include: Option<String>,
26}
27
28impl Key {
29 pub fn new(key_type: KeyType, columns: Vec<KeyColumn>) -> Self {
30 Self {
31 key_type,
32 columns,
33 cluster: false,
34 compress: false,
35 unique: false,
36 include: None,
37 }
38 }
39
40 pub fn new_full<S: Into<String>>(
41 key_type: KeyType,
42 columns: Vec<KeyColumn>,
43 cluster: bool,
44 compress: bool,
45 unique: bool,
46 include: Option<S>,
47 ) -> Self {
48 Self {
49 key_type,
50 columns,
51 cluster,
52 compress,
53 unique,
54 include: include.map(|s| s.into()),
55 }
56 }
57
58 pub fn key_type(&self) -> KeyType {
59 self.key_type
60 }
61
62 pub fn columns(&self) -> &Vec<KeyColumn> {
63 &self.columns
64 }
65
66 pub fn is_cluster(&self) -> bool {
67 self.cluster
68 }
69
70 pub fn is_compress(&self) -> bool {
71 self.compress
72 }
73
74 pub fn is_unique(&self) -> bool {
75 self.unique
76 }
77
78 pub fn include(&self) -> Option<&str> {
79 self.include.as_deref()
80 }
81
82 pub fn is_index(&self) -> bool {
83 self.key_type == KeyType::Index
84 }
85
86 pub fn contains_column(&self, column_name: &str) -> bool {
87 let lower = column_name.to_lowercase();
88 self.columns.iter().any(|c| c.name().to_lowercase() == lower)
89 }
90
91 pub fn columns_as_string(&self) -> String {
92 self.columns
93 .iter()
94 .map(|c| c.name())
95 .collect::<Vec<_>>()
96 .join(",")
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use crate::model::types::KeyType;
104
105 #[test]
106 fn key_column_and_key_basics() {
107 let cols = vec![KeyColumn::new("id"), KeyColumn::new("code")];
108 let k = Key::new(KeyType::Primary, cols.clone());
109 assert_eq!(k.key_type(), KeyType::Primary);
110 assert_eq!(k.columns().len(), 2);
111 assert!(!k.is_cluster());
112 assert!(!k.is_compress());
113 assert!(!k.is_unique());
114 assert_eq!(k.include(), None);
115 assert!(k.contains_column("id"));
116 assert_eq!(k.columns_as_string(), "id,code");
117
118 let k2 = Key::new_full(KeyType::Index, cols, true, true, true, Some("inc"));
119 assert!(k2.is_cluster());
120 assert!(k2.is_compress());
121 assert!(k2.is_unique());
122 assert_eq!(k2.include(), Some("inc"));
123 }
124
125 #[test]
126 fn contains_column_is_case_insensitive() {
127 let k = Key::new(KeyType::Primary, vec![KeyColumn::new("Id")]);
132 assert!(k.contains_column("Id"));
133 assert!(k.contains_column("id"));
134 assert!(k.contains_column("ID"));
135 assert!(!k.contains_column("other"));
136
137 let k2 = Key::new(KeyType::Primary, vec![KeyColumn::new("ÉTAT")]);
140 assert!(k2.contains_column("ÉTAT"));
141 assert!(k2.contains_column("état"));
142 }
143}