no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::super::{Frame, Qualified, SqlBareRead};
use super::Scope;
use std::collections::BTreeMap;

pub(in super::super) struct ScopedReads {
    qualifiers: usize,
    reads: usize,
    bare: BTreeMap<String, usize>,
}

impl ScopedReads {
    pub(in super::super) fn begin(frame: &mut Frame) -> Self {
        Self {
            qualifiers: frame.qualifiers.len(),
            reads: frame.reads.len(),
            bare: std::mem::take(&mut frame.bare),
        }
    }

    pub(in super::super) fn finish(
        self,
        frame: &mut Frame,
        resolve: fn(&Scope, &mut Vec<SqlBareRead>),
    ) {
        let mut candidates = None;
        frame.join_qualifiers.extend(
            frame
                .qualifiers
                .split_off(self.qualifiers)
                .into_iter()
                .filter(|read| !frame.scope.relations.contains(&read.key))
                .map(|mut read: Qualified| {
                    read.scopes.push(
                        candidates
                            .get_or_insert_with(|| frame.scope.qualified_candidates())
                            .clone(),
                    );
                    read
                }),
        );
        let mut reads = frame.reads.split_off(self.reads);
        for (column, _) in std::mem::replace(&mut frame.bare, self.bare) {
            if !frame.scope.whole_rows.contains(&column) {
                reads.push(SqlBareRead {
                    column,
                    tables: Vec::new(),
                });
            }
        }
        resolve(&frame.scope, &mut reads);
        frame.join_reads.extend(reads);
    }
}