no-mistakes-postgres-sql 0.109.129

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::super::super::query::outputs::wildcard_owners::{local_relation_key, WildcardOwners};
use crate::erased_visit::VisitErased;
use crate::idents::{ident_key, relation_part_key};
use sqlparser::ast::{Expr, Query, Select, TableFactor, Visit, Visitor};
use std::ops::ControlFlow;

pub(super) fn has_reference(input: &impl Visit) -> bool {
    has_reference_matching(input, None)
}

pub(super) fn has_current_reference(input: &impl Visit, current: &WildcardOwners) -> bool {
    has_reference_matching(input, Some(current))
}

fn has_reference_matching(input: &impl Visit, current: Option<&WildcardOwners>) -> bool {
    input
        .visit_erased(&mut OuterReference {
            owners: WildcardOwners::default(),
            current,
        })
        .is_break()
}

struct OuterReference<'a> {
    owners: WildcardOwners,
    current: Option<&'a WildcardOwners>,
}

impl Visitor for OuterReference<'_> {
    type Break = ();

    fn pre_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
        self.owners.enter_query(query);
        ControlFlow::Continue(())
    }

    fn post_visit_query(&mut self, _: &Query) -> ControlFlow<()> {
        self.owners.leave_query();
        ControlFlow::Continue(())
    }

    fn pre_visit_select(&mut self, select: &Select) -> ControlFlow<()> {
        self.owners.enter_select(select);
        ControlFlow::Continue(())
    }

    fn post_visit_select(&mut self, select: &Select) -> ControlFlow<()> {
        self.owners.leave_select(select);
        ControlFlow::Continue(())
    }

    fn pre_visit_table_factor(&mut self, factor: &TableFactor) -> ControlFlow<()> {
        self.owners.enter_factor(factor);
        ControlFlow::Continue(())
    }

    fn post_visit_table_factor(&mut self, factor: &TableFactor) -> ControlFlow<()> {
        self.owners.leave_factor(factor);
        ControlFlow::Continue(())
    }

    fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<()> {
        let qualifier = match expr {
            Expr::Identifier(_)
                if !self.owners.in_query()
                    && self.current.is_none_or(WildcardOwners::has_inputs) =>
            {
                return ControlFlow::Break(());
            }
            Expr::CompoundIdentifier(parts) => {
                let Some((_, prefix)) = parts.split_last() else {
                    return ControlFlow::Continue(());
                };
                let prefix = if prefix.len() == 3 {
                    &prefix[1..]
                } else {
                    prefix
                };
                prefix
                    .iter()
                    .map(|part| relation_part_key(&ident_key(part)))
                    .collect::<Vec<_>>()
                    .join(".")
            }
            Expr::QualifiedWildcard(name, _) => local_relation_key(name),
            _ => return ControlFlow::Continue(()),
        };
        if self.owners.contains(&qualifier)
            || self
                .current
                .is_some_and(|current| !current.contains(&qualifier))
        {
            ControlFlow::Continue(())
        } else {
            ControlFlow::Break(())
        }
    }
}