no-mistakes-postgres-sql 0.109.130

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::super::sources::StatementProjection;
use super::super::SqlStatementFileFacts;
use crate::conflict::ConflictChunks;
use crate::parse::{
    orphan_chunk_tokens, parse_postgres_sql_lenient_with_sources, partition_function_sources,
    PreparedSql,
};
use crate::statement_facts::SqlLocationDemand;
use crate::statements::value::PlaceholderPositions;
use sqlparser::tokenizer::TokenWithSpan;
use std::sync::Arc;

mod opaque_prefix;

pub(super) fn extract(
    sql: &str,
    collect_bounds: bool,
    placeholder_positions: PlaceholderPositions<'_>,
    locations: SqlLocationDemand,
    projection: StatementProjection,
    mut policy: super::super::StatementPolicySources<'_>,
) -> SqlStatementFileFacts {
    policy.collect_body_bounds = matches!(
        projection,
        StatementProjection::BodyBounds | StatementProjection::BodyBoundsLockScope
    );
    super::super::locations::work::statement_extraction();
    let prepared = PreparedSql::new(sql);
    let mut statement_locations = Vec::new();
    let parsed = prepared.parse_policy_with_locations(Some(&mut statement_locations));
    let parse_failed = parsed.is_err();
    match parsed {
        Ok(statements) => super::super::projection::extract(
            sql,
            &prepared,
            &statements,
            super::super::StatementSources {
                projection,
                locations,
                policy: super::super::StatementPolicySources {
                    statement_locations: Some(&statement_locations),
                    ..policy
                },
                ..Default::default()
            },
            false,
            collect_bounds,
            placeholder_positions,
        ),
        Err(_) => {
            let parsed = parse_postgres_sql_lenient_with_sources(sql, prepared.normalized());
            let partition_complete = parsed.iter().any(|statement| statement.partition_complete)
                && parsed
                    .iter()
                    .all(|statement| statement.complete || statement.partition_complete);
            let terminator = policy
                .opaque_sql_prefix
                .then(|| opaque_prefix::last_terminator(sql, &prepared, placeholder_positions))
                .flatten();
            let parse_failures: Vec<_> = crate::migration::recovery_failures(&parsed)
                .into_iter()
                .filter(|failure| failure.statement_unanalyzable)
                .filter(|failure| {
                    !policy.opaque_sql_prefix
                        || terminator.is_some_and(|end| {
                            (failure.line as u64, failure.column as u64) < (end.line, end.column)
                        })
                })
                .collect();
            let opaque_sql_prefix = policy.opaque_sql_prefix;
            let orphans = orphan_chunk_tokens(&parsed);
            let (mut located, functions) = partition_function_sources(parsed);
            prepared.adopt_recovered_arbiters(&mut located);
            let complete: Vec<bool> = located.iter().map(|statement| statement.complete).collect();
            let chunks: Vec<Option<Arc<[TokenWithSpan]>>> = located
                .iter()
                .map(|statement| statement.chunk_tokens.clone())
                .collect();
            let recovery = ConflictChunks {
                complete: &complete,
                chunks: &chunks,
                orphans: &orphans,
            };
            let policy_sources = super::super::sources::policy_tokens(&located);
            let recovered_locations: Vec<_> = located
                .iter()
                .map(crate::parse::LocatedStatement::policy_start)
                .collect();
            let (statements, sources): (Vec<_>, Vec<_>) = located
                .into_iter()
                .map(|located| (located.statement, located.source))
                .unzip();
            let mut facts = super::super::projection::extract(
                sql,
                &prepared,
                &statements,
                super::super::StatementSources {
                    projection,
                    locations,
                    parse_failures: &parse_failures,
                    tokens: Some(&sources),
                    recovery: Some(&recovery),
                    policy: super::super::StatementPolicySources {
                        tokens: Some(&policy_sources),
                        statement_locations: Some(&recovered_locations),
                        schema: None,
                        functions: &functions,
                        ..policy
                    },
                },
                parse_failed,
                collect_bounds,
                placeholder_positions,
            );
            facts.lock_writes.ddl_recovery_complete = partition_complete;
            if opaque_sql_prefix {
                facts.parse_failed = !facts.parse_failures.is_empty();
            }
            facts
        }
    }
}

pub(super) fn extract_with_bind_values(
    sql: &str,
    collect_bounds: bool,
    positions: PlaceholderPositions<'_>,
    locations: SqlLocationDemand,
    projection: StatementProjection,
    policy: super::super::StatementPolicySources<'_>,
) -> SqlStatementFileFacts {
    extract(
        sql,
        collect_bounds,
        positions,
        locations,
        projection,
        policy,
    )
}