use relate_core::{Error, Result};
use relate_ir::{Clause, Query, SingleQuery};
use relate_names::SmallName;
use crate::binding::{Binding, Env, EnvBridge};
use crate::expr_check::check_expr;
pub(crate) mod chain;
mod clause;
mod in_transactions;
mod subquery;
pub(crate) mod write;
use clause::*;
use subquery::*;
use write::{ClausePosition, WriteHistory};
#[derive(Clone)]
pub(crate) struct Scope {
pub(crate) env: Env,
pub(crate) imports: crate::agg::Imports,
pub(crate) shadowable: std::collections::HashSet<String>,
}
impl Scope {
pub(crate) fn stage(env: Env) -> Self {
let imports = env.keys().cloned().collect();
Self {
env,
imports,
shadowable: std::collections::HashSet::new(),
}
}
pub(crate) fn expression_body(env: Env) -> Self {
let imports = env.keys().cloned().collect();
let shadowable = crate::binding::shadowable_imports(&env);
Self {
env,
imports,
shadowable,
}
}
pub(crate) fn comprehension_body(mut env: Env, path_shadow: Option<&SmallName>) -> Self {
if let Some(name) = path_shadow {
env.remove_name(name);
}
Self::expression_body(env)
}
pub(crate) fn branch_body(env: Env) -> Self {
Self {
env,
imports: crate::agg::Imports::new(),
shadowable: std::collections::HashSet::new(),
}
}
pub(crate) fn foreach_body(outer: &Env, loop_variable: &SmallName) -> Self {
let mut shadowable: std::collections::HashSet<String> = outer.keys().cloned().collect();
loop_variable.with_str(|text| {
shadowable.remove(text);
});
let mut env = outer.clone();
env.insert_name(loop_variable.clone(), Binding::Value);
Self {
env,
imports: crate::agg::Imports::new(),
shadowable,
}
}
}
pub(crate) fn validate_clauses_with_env(
clauses: &[Clause],
scope: Scope,
mut write_history: WriteHistory,
is_write_procedure: &mut impl FnMut(&str) -> bool,
) -> Result<WriteHistory> {
let Scope {
mut env,
imports,
shadowable,
} = scope;
if clauses.is_empty() {
return Err(Error::syntax("empty query part"));
}
let substantive = clauses
.iter()
.filter(|c| !crate::clause_kind::clause_is_routing(c))
.count();
if clauses
.iter()
.any(crate::clause_kind::clause_is_ddl_command)
&& substantive != 1
{
return Err(Error::syntax(
"InvalidClauseComposition: schema and database-alteration commands must be standalone clauses",
));
}
for (clause_index, c) in clauses.iter().enumerate() {
let position = ClausePosition {
clauses,
index: clause_index,
write_history,
};
if matches!(c, Clause::Return { .. } | Clause::Finish) && !position.is_terminal() {
return Err(Error::syntax(format!(
"InvalidClauseComposition: {} must terminate its query",
crate::clause_kind::ir_clause_keyword(c)
)));
}
match c {
Clause::Catalog(command) => check_catalog_clause(&mut env, command)?,
Clause::Match { .. } => {
check_match_clause(&mut env, &shadowable, is_write_procedure, c)?
}
Clause::Create { patterns } => {
check_create_clause(&mut env, &shadowable, is_write_procedure, patterns)?
}
Clause::Merge { .. } => {
check_merge_clause(&mut env, &shadowable, is_write_procedure, c)?
}
Clause::Call {
args,
yield_items,
where_clause,
..
} => check_call_clause(
&mut env,
is_write_procedure,
clauses,
args,
yield_items,
where_clause.as_ref(),
)?,
Clause::Delete { expressions, .. } => {
check_delete_clause(&mut env, is_write_procedure, expressions)?
}
Clause::Set { items } => check_set_clause(&mut env, is_write_procedure, items)?,
Clause::Remove { items } => check_remove_clause(&mut env, is_write_procedure, items)?,
Clause::With { .. } => check_with_clause(&mut env, is_write_procedure, &imports, c)?,
Clause::Return { .. } => {
check_return_clause(&mut env, is_write_procedure, &imports, c)?
}
Clause::Unwind { expression, alias } => {
check_unwind_clause(&mut env, is_write_procedure, expression, alias)?
}
Clause::CreateIndex { .. } | Clause::DropIndex { .. } | Clause::ShowIndexes => {}
Clause::Finish => {}
Clause::LoadCsv { url, variable, .. } => {
check_load_csv_clause(&mut env, is_write_procedure, url, variable)?
}
Clause::CallSubquery {
query,
scope,
in_transactions,
..
} => check_call_subquery_clause(
&mut env,
is_write_procedure,
position,
query,
scope,
in_transactions.as_ref(),
)?,
Clause::Conditional { .. } => {
check_conditional_clause(&mut env, is_write_procedure, c)?
}
Clause::Foreach {
variable,
list,
clauses: body,
} => check_foreach_clause(&mut env, is_write_procedure, variable, list, body)?,
Clause::Use { graph } => {
check_use_clause(&mut env, is_write_procedure, position, graph)?
}
}
write_history = write_history.after(c, is_write_procedure);
}
Ok(write_history)
}
pub fn check_union_joiners(union_all: &[bool]) -> Result<()> {
match union_all.first() {
Some(first) if union_all.iter().any(|a| a != first) => Err(Error::syntax(
"InvalidClauseComposition: mixing UNION and UNION ALL",
)),
_ => Ok(()),
}
}
fn is_unaliased_expression(item: &relate_ir::Projection) -> bool {
item.alias.is_none() && relate_ir::canonical_name(&item.expr).is_none()
}
fn last_return_has_unaliased_expression(part: &SingleQuery) -> bool {
matches!(
part.clauses.last(),
Some(Clause::Return { items, .. }) if items.iter().any(is_unaliased_expression)
)
}
fn refuse_unaliased_terminal_returns(query: &Query, text: &str) -> Result<()> {
if query.parts.iter().any(last_return_has_unaliased_expression) {
return Err(Error::syntax(text));
}
Ok(())
}
fn check_set_item(
env: &Env,
is_write_procedure: &mut impl FnMut(&str) -> bool,
item: &relate_ir::SetItem,
) -> Result<()> {
match item {
relate_ir::SetItem::Property { target, value }
| relate_ir::SetItem::ReplaceAll { target, value }
| relate_ir::SetItem::MergeMap { target, value } => {
check_expr(env, is_write_procedure, target)?;
check_expr(env, is_write_procedure, value)?;
}
relate_ir::SetItem::Labels { target, .. } => check_expr(env, is_write_procedure, target)?,
relate_ir::SetItem::LabelsFunction { target, value, .. } => {
check_expr(env, is_write_procedure, target)?;
check_expr(env, is_write_procedure, value)?;
}
}
Ok(())
}
fn check_remove_item(
env: &Env,
is_write_procedure: &mut impl FnMut(&str) -> bool,
item: &relate_ir::RemoveItem,
) -> Result<()> {
match item {
relate_ir::RemoveItem::Property { target }
| relate_ir::RemoveItem::Labels { target, .. } => {
check_expr(env, is_write_procedure, target)?;
}
}
Ok(())
}
fn query_contains_in_transactions(query: &Query) -> bool {
crate::query_any_clause(query, &mut |clause| {
crate::clause_kind::clause_is_in_transactions(clause)
|| matches!(
clause,
Clause::CallSubquery { query, .. } if query_contains_in_transactions(query)
)
})
}
#[cfg(test)]
mod composition_tests;
#[cfg(test)]
mod subquery_scope_tests;