no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::{relation_suffix_key, Candidate, Scope, SqlSchemaQualifiedViewRead};

pub(in crate::statements::keyed_lookups::view_uses::refs) fn mark_embedding<'a>(
    candidates: &mut [Candidate],
    scopes: impl Iterator<Item = &'a Scope>,
    qualifier: &str,
    unmatched: &super::super::super::unmatched::Unmatched,
) -> bool {
    let short = relation_suffix_key(qualifier);
    let mut default_binding = false;
    let mut embedded = false;
    for scope in scopes {
        if let Some(binding) = scope.bindings.get(qualifier) {
            if let Some(index) = binding.filter(|index| !unmatched.blocks(*index)) {
                if default_binding {
                    record_embedding(&mut candidates[index], qualifier, false);
                } else {
                    candidates[index].embedded = true;
                }
                embedded = true;
            }
            break;
        }
        if short != qualifier && !default_binding {
            if let Some(input) = scope
                .physical_inputs
                .iter()
                .find(|input| input.unqualified && input.relation == short)
            {
                default_binding = true;
                if let Some(index) = input.candidate.filter(|index| !unmatched.blocks(*index)) {
                    record_embedding(&mut candidates[index], qualifier, true);
                    embedded = true;
                }
            }
        }
    }
    embedded
}

fn record_embedding(candidate: &mut Candidate, relation: &str, matches_default_schema: bool) {
    let read = SqlSchemaQualifiedViewRead {
        relation: relation.to_owned(),
        matches_default_schema,
    };
    if !candidate.schema_qualified_embeddings.contains(&read) {
        candidate.schema_qualified_embeddings.push(read);
    }
}