Skip to main content

schema_model/model/
key.rs

1use 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    filter: Option<String>,
27}
28
29impl Key {
30    pub fn new(key_type: KeyType, columns: Vec<KeyColumn>) -> Self {
31        Self {
32            key_type,
33            columns,
34            cluster: false,
35            compress: false,
36            unique: false,
37            include: None,
38            filter: None,
39        }
40    }
41
42    pub fn new_full<S: Into<String>>(
43        key_type: KeyType,
44        columns: Vec<KeyColumn>,
45        cluster: bool,
46        compress: bool,
47        unique: bool,
48        include: Option<S>,
49    ) -> Self {
50        Self {
51            key_type,
52            columns,
53            cluster,
54            compress,
55            unique,
56            include: include.map(|s| s.into()),
57            filter: None,
58        }
59    }
60
61    #[allow(clippy::too_many_arguments)]
62    pub fn new_full_with_filter<S: Into<String>, F: Into<String>>(
63        key_type: KeyType,
64        columns: Vec<KeyColumn>,
65        cluster: bool,
66        compress: bool,
67        unique: bool,
68        include: Option<S>,
69        filter: Option<F>,
70    ) -> Self {
71        Self {
72            key_type,
73            columns,
74            cluster,
75            compress,
76            unique,
77            include: include.map(|s| s.into()),
78            filter: filter.map(|s| s.into()),
79        }
80    }
81
82    pub fn key_type(&self) -> KeyType {
83        self.key_type
84    }
85
86    pub fn columns(&self) -> &Vec<KeyColumn> {
87        &self.columns
88    }
89
90    pub fn is_cluster(&self) -> bool {
91        self.cluster
92    }
93
94    pub fn is_compress(&self) -> bool {
95        self.compress
96    }
97
98    pub fn is_unique(&self) -> bool {
99        self.unique
100    }
101
102    pub fn include(&self) -> Option<&str> {
103        self.include.as_deref()
104    }
105
106    pub fn filter(&self) -> Option<&str> {
107        self.filter.as_deref()
108    }
109
110    pub fn is_index(&self) -> bool {
111        self.key_type == KeyType::Index
112    }
113
114    pub fn contains_column(&self, column_name: &str) -> bool {
115        let lower = column_name.to_lowercase();
116        self.columns
117            .iter()
118            .any(|c| c.name().to_lowercase() == lower)
119    }
120
121    pub fn columns_as_string(&self) -> String {
122        self.columns
123            .iter()
124            .map(|c| c.name())
125            .collect::<Vec<_>>()
126            .join(",")
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133    use crate::model::types::KeyType;
134
135    #[test]
136    fn key_column_and_key_basics() {
137        let cols = vec![KeyColumn::new("id"), KeyColumn::new("code")];
138        let k = Key::new(KeyType::Primary, cols.clone());
139        assert_eq!(k.key_type(), KeyType::Primary);
140        assert_eq!(k.columns().len(), 2);
141        assert!(!k.is_cluster());
142        assert!(!k.is_compress());
143        assert!(!k.is_unique());
144        assert_eq!(k.include(), None);
145        assert!(k.contains_column("id"));
146        assert_eq!(k.columns_as_string(), "id,code");
147
148        let k2 = Key::new_full(KeyType::Index, cols, true, true, true, Some("inc"));
149        assert!(k2.is_cluster());
150        assert!(k2.is_compress());
151        assert!(k2.is_unique());
152        assert_eq!(k2.include(), Some("inc"));
153        assert_eq!(k2.filter(), None);
154
155        let k3 = Key::new_full_with_filter(
156            KeyType::Index,
157            vec![KeyColumn::new("id")],
158            false,
159            false,
160            true,
161            None::<String>,
162            Some("id is not null"),
163        );
164        assert_eq!(k3.filter(), Some("id is not null"));
165        assert!(k3.is_unique());
166    }
167
168    #[test]
169    fn contains_column_is_case_insensitive() {
170        // Matches the case-insensitive lookup used everywhere else in the model
171        // (Table::column/has_column), so a key declared with `Id` is found by callers
172        // that look up `id` (or any other casing) rather than incorrectly reporting the
173        // column as absent.
174        let k = Key::new(KeyType::Primary, vec![KeyColumn::new("Id")]);
175        assert!(k.contains_column("Id"));
176        assert!(k.contains_column("id"));
177        assert!(k.contains_column("ID"));
178        assert!(!k.contains_column("other"));
179
180        // Unicode casing (eq_ignore_ascii_case only folds ASCII letters, so this must go
181        // through to_lowercase() on both sides, per H21).
182        let k2 = Key::new(KeyType::Primary, vec![KeyColumn::new("ÉTAT")]);
183        assert!(k2.contains_column("ÉTAT"));
184        assert!(k2.contains_column("état"));
185    }
186}