use crate::erased_visit::VisitErased;
use sqlparser::ast::{Expr, OrderBy, Query, Select, Statement, TableFactor, Visitor};
use std::ops::ControlFlow;
mod clause;
mod clauses;
mod cte;
mod fact;
mod record;
pub use clause::SqlFunctionClause;
pub use fact::SqlFunctionCallFact;
use record::record;
pub(super) fn collect(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
collect_projected(statement, out, false);
}
pub(super) fn collect_executed(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
let mut executed = Vec::new();
super::statements::walk_executed(statement, &mut executed);
for statement in executed {
collect(statement, out);
}
}
pub(super) fn collect_policy(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
if matches!(
statement,
Statement::CreateFunction(_) | Statement::CreateProcedure { .. }
) {
let start = out.len();
collect(statement, out);
for call in &mut out[start..] {
call.in_stored_routine = true;
}
} else {
collect_executed(statement, out);
}
}
pub(super) fn collect_projected(
statement: &Statement,
out: &mut Vec<SqlFunctionCallFact>,
synthetic_select: bool,
) {
collect_with_synthetic_where(statement, out, synthetic_select, false);
}
pub(super) fn collect_with_synthetic_where(
statement: &Statement,
out: &mut Vec<SqlFunctionCallFact>,
synthetic_select: bool,
synthetic_where: bool,
) {
let _ = statement.visit_erased(&mut Calls {
out,
roots: clauses::Roots::default(),
current: None,
contexts: Vec::new(),
synthetic_select,
ctes: cte::Scopes::default(),
synthetic_where,
});
}
pub(super) fn collect_expression(
expression: &Expr,
out: &mut Vec<SqlFunctionCallFact>,
clause: Option<SqlFunctionClause>,
in_cte: bool,
) {
let _ = expression.visit_erased(&mut Calls {
out,
roots: clauses::Roots::default(),
current: clause,
contexts: Vec::new(),
synthetic_select: false,
ctes: cte::Scopes::for_expression(in_cte),
synthetic_where: false,
});
}
struct Calls<'a> {
out: &'a mut Vec<SqlFunctionCallFact>,
roots: clauses::Roots,
current: Option<SqlFunctionClause>,
contexts: Vec<Option<SqlFunctionClause>>,
synthetic_select: bool,
ctes: cte::Scopes,
synthetic_where: bool,
}
impl Calls<'_> {
fn enter(&mut self, clause: Option<SqlFunctionClause>) {
self.contexts.push(self.current);
self.current = clause;
}
fn leave(&mut self) {
self.current = self.contexts.pop().expect("balanced SQL visitor context");
}
}
impl Visitor for Calls<'_> {
type Break = ();
fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<()> {
self.enter(None);
self.roots.statement(statement);
ControlFlow::Continue(())
}
fn post_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<()> {
self.leave();
ControlFlow::Continue(())
}
fn pre_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
self.enter(None);
self.roots.query(query);
self.ctes.enter(query);
ControlFlow::Continue(())
}
fn post_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
self.leave();
self.ctes.leave(query);
ControlFlow::Continue(())
}
fn pre_visit_select(&mut self, select: &Select) -> ControlFlow<()> {
self.roots
.select(select, !self.synthetic_select, !self.synthetic_where);
self.synthetic_where = false;
self.synthetic_select = false;
ControlFlow::Continue(())
}
fn pre_visit_order_by(&mut self, ordering: &OrderBy) -> ControlFlow<()> {
self.roots.order_by(ordering);
ControlFlow::Continue(())
}
fn pre_visit_expr(&mut self, expression: &Expr) -> ControlFlow<()> {
self.enter(self.roots.clause(expression).unwrap_or(self.current));
if let Expr::Function(function) = expression {
self.roots.function(function);
record(&function.name, self.current, self.ctes.active(), self.out);
}
ControlFlow::Continue(())
}
fn post_visit_expr(&mut self, _expression: &Expr) -> ControlFlow<()> {
self.leave();
ControlFlow::Continue(())
}
fn pre_visit_table_factor(&mut self, table: &TableFactor) -> ControlFlow<()> {
match table {
TableFactor::Table {
name,
args: Some(_),
..
}
| TableFactor::Function { name, .. } => {
record(name, self.current, self.ctes.active(), self.out)
}
_ => {}
}
ControlFlow::Continue(())
}
}
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod tests;
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod clause_tests;
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod join_tests;
#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
mod variant_tests;
#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
mod mutation_tests;
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod execution_tests;