no-mistakes-postgres-engine 0.109.128

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{FileInventory, Gap, LockHelperNames, Occurrence, PARSE_GAP, ROUTINE_GAP};
use crate::statements::SqlStatementFileFacts;
use no_mistakes_postgres_sql::SqlFunctionCallFact;

pub fn record_sql_statement(
    row: &mut FileInventory,
    statement: &SqlStatementFileFacts,
    helpers: &LockHelperNames,
) {
    record_parse_failures(statement, &mut row.gaps);
    if !statement.parse_failed {
        row.sites.extend(sql_sites(helpers, statement));
    }
}

fn record_parse_failures(statement: &SqlStatementFileFacts, gaps: &mut Vec<Gap>) {
    gaps.extend(
        statement
            .parse_failure_sites()
            .map(|(_, line)| (line, PARSE_GAP)),
    );
    gaps.extend(
        statement
            .lock_inventory_unanalyzed
            .iter()
            .map(|&line| (line, ROUTINE_GAP)),
    );
}

fn sql_sites<'a>(
    helpers: &'a LockHelperNames,
    statement: &'a SqlStatementFileFacts,
) -> impl Iterator<Item = Occurrence> + 'a {
    statement
        .lock_inventory
        .iter()
        .map(|lock| Occurrence {
            line: lock.line,
            column: lock.column,
            category: lock.category.clone(),
            fingerprint: lock.fingerprint.clone(),
        })
        .chain(
            statement
                .function_calls
                .iter()
                .filter_map(move |call| sql_helper(call, helpers)),
        )
}

pub fn sql_helper(call: &SqlFunctionCallFact, helpers: &LockHelperNames) -> Option<Occurrence> {
    let terminal = call.name_parts.last()?;
    let name = call.name_parts.join(".");
    helpers.matches_sql(&name, terminal).then(|| Occurrence {
        line: call.line,
        column: call.column,
        category: "helper".into(),
        fingerprint: format!("sql:{:?}", call.name_parts),
    })
}