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