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.iter().any(|c| c.name().to_lowercase() == lower)
117    }
118
119    pub fn columns_as_string(&self) -> String {
120        self.columns
121            .iter()
122            .map(|c| c.name())
123            .collect::<Vec<_>>()
124            .join(",")
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    use crate::model::types::KeyType;
132
133    #[test]
134    fn key_column_and_key_basics() {
135        let cols = vec![KeyColumn::new("id"), KeyColumn::new("code")];
136        let k = Key::new(KeyType::Primary, cols.clone());
137        assert_eq!(k.key_type(), KeyType::Primary);
138        assert_eq!(k.columns().len(), 2);
139        assert!(!k.is_cluster());
140        assert!(!k.is_compress());
141        assert!(!k.is_unique());
142        assert_eq!(k.include(), None);
143        assert!(k.contains_column("id"));
144        assert_eq!(k.columns_as_string(), "id,code");
145
146        let k2 = Key::new_full(KeyType::Index, cols, true, true, true, Some("inc"));
147        assert!(k2.is_cluster());
148        assert!(k2.is_compress());
149        assert!(k2.is_unique());
150        assert_eq!(k2.include(), Some("inc"));
151        assert_eq!(k2.filter(), None);
152
153        let k3 = Key::new_full_with_filter(
154            KeyType::Index,
155            vec![KeyColumn::new("id")],
156            false,
157            false,
158            true,
159            None::<String>,
160            Some("id is not null"),
161        );
162        assert_eq!(k3.filter(), Some("id is not null"));
163        assert!(k3.is_unique());
164    }
165
166    #[test]
167    fn contains_column_is_case_insensitive() {
168        // Matches the case-insensitive lookup used everywhere else in the model
169        // (Table::column/has_column), so a key declared with `Id` is found by callers
170        // that look up `id` (or any other casing) rather than incorrectly reporting the
171        // column as absent.
172        let k = Key::new(KeyType::Primary, vec![KeyColumn::new("Id")]);
173        assert!(k.contains_column("Id"));
174        assert!(k.contains_column("id"));
175        assert!(k.contains_column("ID"));
176        assert!(!k.contains_column("other"));
177
178        // Unicode casing (eq_ignore_ascii_case only folds ASCII letters, so this must go
179        // through to_lowercase() on both sides, per H21).
180        let k2 = Key::new(KeyType::Primary, vec![KeyColumn::new("ÉTAT")]);
181        assert!(k2.contains_column("ÉTAT"));
182        assert!(k2.contains_column("état"));
183    }
184}