use relate_core::{Error, Result};
use relate_ir::{Clause, Query};
use crate::binding::{next_stage_scope, Env};
use super::subquery::validate_stage_star_rule;
use super::write::WriteHistory;
use super::Scope;
pub(crate) struct StageSeed {
pub(crate) env: Env,
pub(crate) names: Vec<String>,
pub(crate) excluded_imports: std::collections::HashSet<String>,
pub(crate) scope_clause: bool,
pub(crate) write_history: WriteHistory,
}
impl StageSeed {
pub(crate) fn statement() -> Self {
Self {
env: Env::new(),
names: Vec::new(),
excluded_imports: crate::names::no_excluded_imports(),
scope_clause: false,
write_history: WriteHistory::empty(),
}
}
pub(crate) fn body(
scope: &relate_ir::CallScope,
env: Env,
names: Vec<String>,
excluded_imports: std::collections::HashSet<String>,
) -> Self {
Self {
env,
names,
excluded_imports,
scope_clause: scope.has_scope_clause(),
write_history: WriteHistory::empty(),
}
}
pub(crate) fn successor(self, output_names: &[String], write_behind: WriteHistory) -> Self {
let (env, names) = next_stage_scope(&self.env, &self.names, output_names);
Self {
env,
names,
excluded_imports: self.excluded_imports,
scope_clause: self.scope_clause,
write_history: write_behind,
}
}
}
struct StageValidation {
output_names: Vec<String>,
write_behind: WriteHistory,
}
pub(crate) fn validate_nested_body(
clauses: &[Clause],
scope: Scope,
is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> Result<()> {
let _ = super::validate_clauses_with_env(
clauses,
scope,
WriteHistory::empty(),
is_write_procedure,
)?;
Ok(())
}
fn validate_stage_with_env(
query: &Query,
seed: &StageSeed,
is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> Result<StageValidation> {
if !query.union_all.is_empty() {
super::check_union_joiners(&query.union_all)?;
if let Some(offender) = query
.parts
.iter()
.flat_map(|p| p.clauses.iter())
.find(|c| crate::clause_kind::clause_cannot_join_union(c))
{
return Err(Error::syntax(format!(
"InvalidClauseComposition: {} cannot be used with UNION",
crate::clause_kind::ir_clause_keyword(offender)
)));
}
}
let mut write_behind = WriteHistory::empty();
for part in &query.parts {
write_behind = write_behind.union(super::validate_clauses_with_env(
&part.clauses,
Scope::stage(seed.env.clone()),
seed.write_history,
is_write_procedure,
)?);
if part
.clauses
.last()
.is_some_and(crate::clause_kind::clause_cannot_conclude_part)
{
return Err(Error::syntax(
"InvalidClauseComposition: query must conclude with RETURN, FINISH, an update, or a unit subquery",
));
}
}
let output_names = crate::names::parts_output_names(
&query.parts,
seed.names.to_vec(),
&seed.excluded_imports,
)?;
Ok(StageValidation {
output_names,
write_behind,
})
}
pub(crate) fn validate_query_with_env(
query: &Query,
seed: StageSeed,
is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> Result<Vec<String>> {
let StageValidation {
output_names,
write_behind,
} = validate_stage_with_env(query, &seed, is_write_procedure)?;
if seed.scope_clause {
validate_stage_star_rule(query, &seed.names, &seed.excluded_imports)?;
}
match query.next.as_deref() {
Some(next) => {
super::refuse_unaliased_terminal_returns(
query,
"NoExpressionAlias: expression in RETURN followed by NEXT must use AS",
)?;
validate_query_with_env(
next,
seed.successor(&output_names, write_behind),
is_write_procedure,
)
}
None => Ok(output_names),
}
}