no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{names, SchemaCatalog};

impl SchemaCatalog {
    /// The line of a view's key in the catalog source.
    pub fn view_line(&self, view: &str) -> usize {
        self.locations.views.get(view).copied().unwrap_or(1)
    }

    pub fn materialized_view(&self, name: &str) -> Option<&super::model::CatalogView> {
        self.views
            .get(&names::normalize_table_name(name))
            .or_else(|| {
                let (schema, bare) = names::split_name(name);
                let schema = schema.as_deref()?;
                if self.declared_schema() != Some(schema) {
                    return None;
                }
                let key = no_mistakes_postgres_sql::idents::relation_part_key(&bare);
                self.views.get(&names::normalize_table_name(&key))
            })
            .or_else(|| self.view_relation(name))
            .filter(|view| view.materialized)
    }

    pub fn has_descendants(&self, relation: &str) -> Option<bool> {
        let table = self
            .relation(relation)
            .filter(|table| self.same_relation(relation, &table.name))?;
        self.descendant_presence
            .get(&names::normalize_table_name(&table.name))
            .copied()
    }

    /// Whether two SQL names have the same catalog identity. Equating a bare name with a
    /// schema-qualified one requires explicit evidence for the selected schema.
    pub fn same_relation(&self, left: &str, right: &str) -> bool {
        let left_normalized = names::normalize_table_name(left);
        let right_normalized = names::normalize_table_name(right);
        if left_normalized == right_normalized {
            return true;
        }
        let (left_schema, _) = names::split_name(left);
        let (right_schema, _) = names::split_name(right);
        if [left_schema.as_deref(), right_schema.as_deref()]
            .into_iter()
            .flatten()
            .any(|schema| self.schema.as_deref() != Some(schema))
        {
            return false;
        }
        self.relation(left)
            .zip(self.relation(right))
            .is_some_and(|(left, right)| left.name == right.name)
    }
}