no-mistakes-postgres-sql 0.109.128

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use crate::fx::FxHashSet;
use sqlparser::ast::Query;

#[derive(Default)]
pub(super) struct Scopes {
    bodies: FxHashSet<usize>,
    depth: usize,
}

impl Scopes {
    pub(super) fn for_expression(in_cte: bool) -> Self {
        Self {
            depth: usize::from(in_cte),
            ..Self::default()
        }
    }

    pub(super) fn active(&self) -> bool {
        self.depth > 0
    }

    pub(super) fn enter(&mut self, query: &Query) {
        if self.bodies.contains(&identity(query)) {
            self.depth += 1;
        }
        if let Some(with) = &query.with {
            self.bodies
                .extend(with.cte_tables.iter().map(|cte| identity(&cte.query)));
        }
    }

    pub(super) fn leave(&mut self, query: &Query) {
        if self.bodies.remove(&identity(query)) {
            self.depth -= 1;
        }
    }
}

fn identity(query: &Query) -> usize {
    std::ptr::from_ref(query).addr()
}