schema-model 0.6.29

A set of tools to manage relational database schemas
Documentation
use crate::model::types::KeyType;

#[derive(Debug, Clone)]
pub struct KeyColumn {
    name: String,
}

impl KeyColumn {
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self { name: name.into() }
    }

    pub fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Debug, Clone)]
pub struct Key {
    key_type: KeyType,
    columns: Vec<KeyColumn>,
    cluster: bool,
    compress: bool,
    unique: bool,
    include: Option<String>,
    filter: Option<String>,
}

impl Key {
    pub fn new(key_type: KeyType, columns: Vec<KeyColumn>) -> Self {
        Self {
            key_type,
            columns,
            cluster: false,
            compress: false,
            unique: false,
            include: None,
            filter: None,
        }
    }

    pub fn new_full<S: Into<String>>(
        key_type: KeyType,
        columns: Vec<KeyColumn>,
        cluster: bool,
        compress: bool,
        unique: bool,
        include: Option<S>,
    ) -> Self {
        Self {
            key_type,
            columns,
            cluster,
            compress,
            unique,
            include: include.map(|s| s.into()),
            filter: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new_full_with_filter<S: Into<String>, F: Into<String>>(
        key_type: KeyType,
        columns: Vec<KeyColumn>,
        cluster: bool,
        compress: bool,
        unique: bool,
        include: Option<S>,
        filter: Option<F>,
    ) -> Self {
        Self {
            key_type,
            columns,
            cluster,
            compress,
            unique,
            include: include.map(|s| s.into()),
            filter: filter.map(|s| s.into()),
        }
    }

    pub fn key_type(&self) -> KeyType {
        self.key_type
    }

    pub fn columns(&self) -> &Vec<KeyColumn> {
        &self.columns
    }

    pub fn is_cluster(&self) -> bool {
        self.cluster
    }

    pub fn is_compress(&self) -> bool {
        self.compress
    }

    pub fn is_unique(&self) -> bool {
        self.unique
    }

    pub fn include(&self) -> Option<&str> {
        self.include.as_deref()
    }

    pub fn filter(&self) -> Option<&str> {
        self.filter.as_deref()
    }

    pub fn is_index(&self) -> bool {
        self.key_type == KeyType::Index
    }

    pub fn contains_column(&self, column_name: &str) -> bool {
        let lower = column_name.to_lowercase();
        self.columns
            .iter()
            .any(|c| c.name().to_lowercase() == lower)
    }

    pub fn columns_as_string(&self) -> String {
        self.columns
            .iter()
            .map(|c| c.name())
            .collect::<Vec<_>>()
            .join(",")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::types::KeyType;

    #[test]
    fn key_column_and_key_basics() {
        let cols = vec![KeyColumn::new("id"), KeyColumn::new("code")];
        let k = Key::new(KeyType::Primary, cols.clone());
        assert_eq!(k.key_type(), KeyType::Primary);
        assert_eq!(k.columns().len(), 2);
        assert!(!k.is_cluster());
        assert!(!k.is_compress());
        assert!(!k.is_unique());
        assert_eq!(k.include(), None);
        assert!(k.contains_column("id"));
        assert_eq!(k.columns_as_string(), "id,code");

        let k2 = Key::new_full(KeyType::Index, cols, true, true, true, Some("inc"));
        assert!(k2.is_cluster());
        assert!(k2.is_compress());
        assert!(k2.is_unique());
        assert_eq!(k2.include(), Some("inc"));
        assert_eq!(k2.filter(), None);

        let k3 = Key::new_full_with_filter(
            KeyType::Index,
            vec![KeyColumn::new("id")],
            false,
            false,
            true,
            None::<String>,
            Some("id is not null"),
        );
        assert_eq!(k3.filter(), Some("id is not null"));
        assert!(k3.is_unique());
    }

    #[test]
    fn contains_column_is_case_insensitive() {
        // Matches the case-insensitive lookup used everywhere else in the model
        // (Table::column/has_column), so a key declared with `Id` is found by callers
        // that look up `id` (or any other casing) rather than incorrectly reporting the
        // column as absent.
        let k = Key::new(KeyType::Primary, vec![KeyColumn::new("Id")]);
        assert!(k.contains_column("Id"));
        assert!(k.contains_column("id"));
        assert!(k.contains_column("ID"));
        assert!(!k.contains_column("other"));

        // Unicode casing (eq_ignore_ascii_case only folds ASCII letters, so this must go
        // through to_lowercase() on both sides, per H21).
        let k2 = Key::new(KeyType::Primary, vec![KeyColumn::new("ÉTAT")]);
        assert!(k2.contains_column("ÉTAT"));
        assert!(k2.contains_column("état"));
    }
}