no-mistakes-postgres-sql 0.109.129

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use crate::erased_visit::VisitErased;
use crate::idents::{object_name_identity, unwrap_expr};
use crate::lock_inventory::tokens::Segments;
mod advisory;
mod collector;
use collector::Collector;
mod cte_delete;
mod ddl;
mod helpers;
pub(crate) mod lookup;
mod prelock;
mod row_locks;
mod scope;
mod sequences;
mod settings;
mod statement_checks;
pub(crate) use statement_checks::collect_discarded_exists_queries;
mod trigger_writes;
use super::value::PlaceholderPositions;
use sqlparser::ast::{BinaryOperator, Expr, Query, Statement, TableFactor, Visitor};
use std::cell::OnceCell;
use std::collections::{BTreeMap, BTreeSet};
use std::ops::{ControlFlow, Range};
pub use {
    advisory::AdvisoryKey,
    cte_delete::CteWriteLink,
    ddl::DdlEvent,
    helpers::RowAccess,
    sequences::{SequenceMode, ShareStatement},
    statement_checks::StatementLockCheck,
};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LockWriteFacts {
    pub ddl_events: Vec<DdlEvent>,
    pub ddl_recovery_complete: bool,
    pub locks: Vec<RowAccess>,
    pub statement_checks: Vec<StatementLockCheck>,
    pub writes: Vec<RowAccess>,
    pub upsert_write_keys: Vec<RowAccess>,
    pub trigger_writes: Vec<crate::CatalogTriggerWrite>,
    pub table_locks: Vec<Option<String>>,
    pub transaction_boundary: bool,
    pub relation_identity_changed: bool,
    pub index_dropped: bool,
    pub dynamic_setting_name: bool,
    pub advisory_keys: Vec<Vec<AdvisoryKey>>,
    pub linked_cte_locks: BTreeMap<usize, CteWriteLink>,
    pub self_contained_locks: BTreeMap<usize, Range<usize>>,
    pub share_statements: Vec<sequences::ShareStatement>,
    pub prelock: Option<(RowAccess, bool)>,
    pub prelock_attempt: bool,
    pub conditional_write_keys: Vec<RowAccess>,
    pub conditional_write_unique: bool,
    pub multi_row_upsert_keys: Option<Box<[RowAccess]>>,
}
pub(super) fn collect<const ADVANCED: bool>(
    statements: &[Statement],
    positions: PlaceholderPositions<'_>,
    redundant_prelocks: bool,
    modes: Option<&Segments<'_>>,
    tokens: &[sqlparser::tokenizer::TokenWithSpan],
) -> LockWriteFacts {
    let mut out = LockWriteFacts::default();
    let supplemental_modes = OnceCell::new();
    let row_modes = |query: &Query| {
        modes
            .unwrap_or_else(|| supplemental_modes.get_or_init(|| Segments::new(tokens)))
            .row_lock_modes(query)
    };
    let redundant_prelocks = redundant_prelocks && statements.len() == 1;
    for statement in statements {
        prelock::collect::<ADVANCED>(statement, &mut out, positions, redundant_prelocks, tokens);
        if matches!(statement, Statement::Prepare { .. }) {
            continue;
        }
        let mut executed = Vec::new();
        super::wrappers::walk_executed(statement, &mut executed);
        for statement in executed {
            if ADVANCED {
                ddl::collect(statement, positions, &mut out);
            }
            let (first_lock, first_write) = (out.locks.len(), out.writes.len());
            let mut sequence = modes.map(sequences::Pending::new);
            let _ = statement.visit_erased(&mut Collector::<ADVANCED> {
                out: &mut out,
                scopes: Vec::new(),
                cte_depth: 0,
                with_stack: Vec::new(),
                positions,
                sequence: sequence.as_mut(),
                share_top_level: matches!(statement, Statement::Query(_)),
            });
            if ADVANCED {
                if out.locks.len() > first_lock {
                    statement_checks::collect(
                        statement,
                        positions,
                        &row_modes,
                        &mut out.statement_checks,
                    );
                }
                scope::mark_self_contained(&mut out, first_lock, first_write);
                if let Some(sequence) = sequence {
                    sequence.finish(&mut out, first_write);
                }
                if out.locks.len() == first_lock + 1
                    && out.self_contained_locks.contains_key(&first_lock)
                {
                    if let Some(link) = cte_delete::linked(statement, positions) {
                        out.linked_cte_locks.insert(first_lock, link);
                    }
                }
            }
        }
    }
    out
}
#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
mod tests;