no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use crate::embedded::{
    placeholders, EmbeddedSqlSourcePosition, EmbeddedSqlVariant, MAX_EMBEDDED_SQL_VARIANTS,
};
mod append_decisions;
mod append_effects;
mod conditional;
pub(in crate::embedded::walk) mod conditions;
pub(super) mod control;
mod effects;
mod escape_effects;
mod expression;
mod guarded;
mod helper;
mod helper_alias;
mod helper_bindings;
mod helper_effects;
mod limit;
mod logical;
use limit::bounded;
pub(crate) mod origins;
mod recovery;
mod state;
pub(super) mod switch;
mod template;
mod text;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum ValueKind {
    Sql,
    Null,
    Boolean(bool),
    Number(bool),
    Absent,
    UnknownFalsy,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Recovered {
    pub value: ValueKind,
    pub sql: String,
    pub line: u32,
    pub origins: Vec<u32>,
    pub invocations: super::super::site_identity::Invocations,
    pub positions: Vec<EmbeddedSqlSourcePosition>,
    pub fragment: bool,
    pub enumerated: bool,
    pub closed_domain: bool,
    /// Filled from a parameter type annotation, not a runtime value.
    pub type_annotation: bool,
    pub choices: Vec<(u64, u32)>,
    pub append_sites: Vec<u32>,
    pub first_append_offset: Option<usize>,
    pub append_decisions: Vec<u64>,
    pub decision_offs: Vec<(u64, u32, u32)>,
    pub variants_truncated: bool,
}

impl Recovered {
    pub(in crate::embedded::walk) fn empty(line: u32) -> Self {
        Self {
            value: ValueKind::Sql,
            sql: String::new(),
            line,
            origins: Vec::new(),
            invocations: Default::default(),
            positions: Vec::new(),
            fragment: false,
            enumerated: false,
            closed_domain: false,
            type_annotation: false,
            choices: Vec::new(),
            append_sites: Vec::new(),
            first_append_offset: None,
            append_decisions: Vec::new(),
            decision_offs: Vec::new(),
            variants_truncated: false,
        }
    }
    pub(super) fn truth(&self) -> bool {
        match self.value {
            ValueKind::Sql => self.fragment || !self.sql.is_empty(),
            ValueKind::Boolean(value) | ValueKind::Number(value) => value,
            ValueKind::Null | ValueKind::Absent | ValueKind::UnknownFalsy => false,
        }
    }
    pub(super) fn nullish(&self) -> Option<bool> {
        (self.value != ValueKind::UnknownFalsy).then_some(self.value == ValueKind::Null)
    }
    pub(super) fn merge_append_sites(&mut self, other: &Self) {
        self.variants_truncated |= other.variants_truncated;
        self.append_decisions.extend(&other.append_decisions);
        self.append_decisions.sort_unstable();
        self.append_decisions.dedup();
        self.decision_offs.extend(&other.decision_offs);
        self.decision_offs.sort_unstable();
        self.decision_offs.dedup();
        for site in &other.append_sites {
            if !self.append_sites.contains(site) {
                self.append_sites.push(*site);
            }
        }
    }
    pub(super) fn merge_alternative(&mut self, other: &Self) {
        self.closed_domain &= other.closed_domain;
        self.type_annotation |= other.type_annotation;
        self.first_append_offset = self
            .first_append_offset
            .zip(other.first_append_offset)
            .map(|(left, right)| left.max(right));
        self.merge_append_sites(other);
    }
    pub(super) fn same_value(&self, other: &Self) -> bool {
        self.same_physical_value(other)
            && self.enumerated == other.enumerated
            && self.choices == other.choices
    }

    fn same_physical_value(&self, other: &Self) -> bool {
        self.value == other.value
            && self.sql == other.sql
            && self.line == other.line
            && self.origins == other.origins
            && self.positions == other.positions
            && self.fragment == other.fragment
    }

    pub(super) fn publish(self) -> EmbeddedSqlVariant {
        debug_assert_eq!(self.origins.len(), self.sql.len());
        let first_append_offset = self
            .first_append_offset
            .map(|at| placeholders::publish_placeholders(self.sql[..at].to_owned()).len());
        let sql_source_offsets = origins::rewrite(&self.sql, &self.origins, 0, true);
        let invocations = self.invocations.publish(&self.sql);
        let (sql_text, recovered_placeholder_positions) =
            placeholders::publish_placeholders_with_positions(self.sql);
        EmbeddedSqlVariant {
            sql_text,
            sql_source_offsets: sql_source_offsets.into(),
            invocations,
            line: self.line,
            sql_source_positions: self.positions,
            recovered_placeholder_positions,
            append_sites: self.append_sites,
            first_append_offset,
            variants_truncated: self.variants_truncated,
            closed_domain: self.closed_domain,
            type_annotation: self.type_annotation,
        }
    }
}

fn alternatives(left: Vec<Recovered>, right: Vec<Recovered>) -> Option<Vec<Recovered>> {
    bounded(left.into_iter().chain(right).collect())
}

fn combine(left: Vec<Recovered>, right: Vec<Recovered>) -> Option<Vec<Recovered>> {
    if left
        .iter()
        .chain(&right)
        .any(|value| value.value != ValueKind::Sql)
    {
        return None;
    }
    let append = left
        .iter()
        .chain(&right)
        .any(|value| !value.append_sites.is_empty());
    let mut out = Vec::new();
    for prefix in left {
        for suffix in &right {
            if prefix.choices.iter().any(|(id, arm)| {
                suffix
                    .choices
                    .iter()
                    .any(|(other, branch)| id == other && arm != branch)
            }) {
                continue;
            }
            let mut value = prefix.clone();
            value.append_sql(suffix);
            out.push(value);
            if !append && out.len() > MAX_EMBEDDED_SQL_VARIANTS {
                return None;
            }
        }
    }
    bounded(out)
}