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;
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 parse_failures: Vec<_> = crate::migration::recovery_failures(&parsed)
.into_iter()
.filter(|failure| failure.statement_unanalyzable)
.collect();
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;
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,
)
}