no_mistakes_postgres_sql/
function_calls.rs1use crate::erased_visit::VisitErased;
3use sqlparser::ast::{Expr, OrderBy, Query, Select, Statement, TableFactor, Visitor};
4use std::ops::ControlFlow;
5
6mod clause;
7mod clauses;
8mod cte;
9mod fact;
10mod record;
11pub use clause::SqlFunctionClause;
12pub use fact::SqlFunctionCallFact;
13use record::record;
14
15pub(super) fn collect(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
16 collect_projected(statement, out, false);
17}
18
19pub(super) fn collect_executed(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
22 let mut executed = Vec::new();
23 super::statements::walk_executed(statement, &mut executed);
24 for statement in executed {
25 collect(statement, out);
26 }
27}
28
29pub(super) fn collect_policy(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
30 if matches!(
31 statement,
32 Statement::CreateFunction(_) | Statement::CreateProcedure { .. }
33 ) {
34 let start = out.len();
35 collect(statement, out);
36 for call in &mut out[start..] {
37 call.in_stored_routine = true;
38 }
39 } else {
40 collect_executed(statement, out);
41 }
42}
43
44pub(super) fn collect_projected(
45 statement: &Statement,
46 out: &mut Vec<SqlFunctionCallFact>,
47 synthetic_select: bool,
48) {
49 collect_with_synthetic_where(statement, out, synthetic_select, false);
50}
51
52pub(super) fn collect_with_synthetic_where(
53 statement: &Statement,
54 out: &mut Vec<SqlFunctionCallFact>,
55 synthetic_select: bool,
56 synthetic_where: bool,
57) {
58 let _ = statement.visit_erased(&mut Calls {
59 out,
60 roots: clauses::Roots::default(),
61 current: None,
62 contexts: Vec::new(),
63 synthetic_select,
64 ctes: cte::Scopes::default(),
65 synthetic_where,
66 });
67}
68
69pub(super) fn collect_expression(
71 expression: &Expr,
72 out: &mut Vec<SqlFunctionCallFact>,
73 clause: Option<SqlFunctionClause>,
74 in_cte: bool,
75) {
76 let _ = expression.visit_erased(&mut Calls {
77 out,
78 roots: clauses::Roots::default(),
79 current: clause,
80 contexts: Vec::new(),
81 synthetic_select: false,
82 ctes: cte::Scopes::for_expression(in_cte),
83 synthetic_where: false,
84 });
85}
86
87struct Calls<'a> {
88 out: &'a mut Vec<SqlFunctionCallFact>,
89 roots: clauses::Roots,
90 current: Option<SqlFunctionClause>,
91 contexts: Vec<Option<SqlFunctionClause>>,
92 synthetic_select: bool,
93 ctes: cte::Scopes,
94 synthetic_where: bool,
95}
96
97impl Calls<'_> {
98 fn enter(&mut self, clause: Option<SqlFunctionClause>) {
99 self.contexts.push(self.current);
100 self.current = clause;
101 }
102
103 fn leave(&mut self) {
104 self.current = self.contexts.pop().expect("balanced SQL visitor context");
105 }
106}
107
108impl Visitor for Calls<'_> {
109 type Break = ();
110
111 fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<()> {
112 self.enter(None);
113 self.roots.statement(statement);
114 ControlFlow::Continue(())
115 }
116
117 fn post_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<()> {
118 self.leave();
119 ControlFlow::Continue(())
120 }
121
122 fn pre_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
123 self.enter(None);
125 self.roots.query(query);
126 self.ctes.enter(query);
127 ControlFlow::Continue(())
128 }
129
130 fn post_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
131 self.leave();
132 self.ctes.leave(query);
133 ControlFlow::Continue(())
134 }
135
136 fn pre_visit_select(&mut self, select: &Select) -> ControlFlow<()> {
137 self.roots
138 .select(select, !self.synthetic_select, !self.synthetic_where);
139 self.synthetic_where = false;
140 self.synthetic_select = false;
141 ControlFlow::Continue(())
142 }
143
144 fn pre_visit_order_by(&mut self, ordering: &OrderBy) -> ControlFlow<()> {
145 self.roots.order_by(ordering);
146 ControlFlow::Continue(())
147 }
148
149 fn pre_visit_expr(&mut self, expression: &Expr) -> ControlFlow<()> {
150 self.enter(self.roots.clause(expression).unwrap_or(self.current));
151 if let Expr::Function(function) = expression {
152 self.roots.function(function);
153 record(&function.name, self.current, self.ctes.active(), self.out);
154 }
155 ControlFlow::Continue(())
156 }
157
158 fn post_visit_expr(&mut self, _expression: &Expr) -> ControlFlow<()> {
159 self.leave();
160 ControlFlow::Continue(())
161 }
162
163 fn pre_visit_table_factor(&mut self, table: &TableFactor) -> ControlFlow<()> {
164 match table {
165 TableFactor::Table {
166 name,
167 args: Some(_),
168 ..
169 }
170 | TableFactor::Function { name, .. } => {
171 record(name, self.current, self.ctes.active(), self.out)
172 }
173 _ => {}
174 }
175 ControlFlow::Continue(())
176 }
177}
178
179#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
180mod tests;
181
182#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
183mod clause_tests;
184
185#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
186mod join_tests;
187
188#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
189mod variant_tests;
190
191#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
192mod mutation_tests;
193
194#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
195mod execution_tests;