no-mistakes-postgres-engine 0.109.129

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::placeholders;
use std::sync::Arc;

type InvocationRange = (usize, usize, u32);

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Invocations(Option<Arc<Vec<InvocationRange>>>);

impl Invocations {
    fn iter(&self) -> impl Iterator<Item = &InvocationRange> {
        self.0.iter().flat_map(|ranges| ranges.iter())
    }

    fn ranges(&mut self) -> &mut Vec<InvocationRange> {
        Arc::make_mut(self.0.get_or_insert_with(|| Arc::new(Vec::new())))
    }
    pub fn record(&mut self, length: usize, site: u32) {
        if length > 0 {
            self.ranges().push((0, length, site));
        }
    }

    pub fn append(
        &mut self,
        suffix: &Self,
        sql: &str,
        binds: impl Into<placeholders::Counts>,
        prefix: usize,
    ) {
        let binds = binds.into();
        if suffix.is_empty() {
            return;
        }
        self.ranges()
            .extend(suffix.iter().map(|&(start, end, site)| {
                let offset = |at| {
                    prefix
                        + if binds == placeholders::Counts::default() {
                            at
                        } else {
                            placeholders::renumber_placeholders(&sql[..at], binds).len()
                        }
                };
                (offset(start), offset(end), site)
            }));
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_none()
    }

    pub fn inherit(&mut self, caller: &Self, at: usize, length: usize) {
        for &(start, end, site) in caller.iter() {
            if start <= at && at < end {
                self.record(length, site);
            }
        }
    }

    pub fn slice(&self, range: std::ops::Range<usize>) -> Self {
        let ranges: Vec<_> = self
            .iter()
            .filter_map(|&(start, end, site)| {
                let (start, end) = (start.max(range.start), end.min(range.end));
                (start < end).then_some((
                    start.saturating_sub(range.start),
                    end.saturating_sub(range.start),
                    site,
                ))
            })
            .collect();
        if ranges.is_empty() {
            Self::default()
        } else {
            Self(Some(Arc::new(ranges)))
        }
    }

    pub fn remap(mut self, mut offset: impl FnMut(usize) -> usize) -> Self {
        if self.is_empty() {
            return self;
        }
        for (start, end, _) in self.ranges() {
            *start = offset(*start);
            *end = offset(*end);
        }
        self
    }

    pub fn publish(mut self, sql: &str) -> Self {
        if self.is_empty() || placeholders::markers(sql).next().is_none() {
            return self;
        }
        for (start, end, _) in self.ranges() {
            *start = placeholders::publish_placeholders(sql[..*start].to_owned()).len();
            *end = placeholders::publish_placeholders(sql[..*end].to_owned()).len();
        }
        self
    }

    pub fn at(&self, sql: &str, position: (usize, usize)) -> Vec<u32> {
        if self.is_empty() {
            return Vec::new();
        }
        let mut cursor = (1, 1);
        let offset = sql.char_indices().find_map(|(offset, character)| {
            let found = (cursor == position).then_some(offset);
            cursor = if character == '\n' {
                (cursor.0 + 1, 1)
            } else {
                (cursor.0, cursor.1 + 1)
            };
            found
        });
        self.iter()
            .filter(|&&(start, end, _)| offset.is_some_and(|at| start <= at && at < end))
            .map(|&(_, _, site)| site)
            .collect()
    }
}

#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
#[path = "site_identity_tests.rs"]
mod tests;