sqlparser/ast/
visitor.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! Recursive visitors for ast Nodes. See [`Visitor`] for more details.
14
15use crate::ast::{Expr, ObjectName, Statement, TableFactor};
16use core::ops::ControlFlow;
17
18/// A type that can be visited by a [`Visitor`]. See [`Visitor`] for
19/// recursively visiting parsed SQL statements.
20///
21/// # Note
22///
23/// This trait should be automatically derived for sqlparser AST nodes
24/// using the [Visit](sqlparser_derive::Visit) proc macro.
25///
26/// ```text
27/// #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
28/// ```
29pub trait Visit {
30    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break>;
31}
32
33/// A type that can be visited by a [`VisitorMut`]. See [`VisitorMut`] for
34/// recursively visiting parsed SQL statements.
35///
36/// # Note
37///
38/// This trait should be automatically derived for sqlparser AST nodes
39/// using the [VisitMut](sqlparser_derive::VisitMut) proc macro.
40///
41/// ```text
42/// #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
43/// ```
44pub trait VisitMut {
45    fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break>;
46}
47
48impl<T: Visit> Visit for Option<T> {
49    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
50        if let Some(s) = self {
51            s.visit(visitor)?;
52        }
53        ControlFlow::Continue(())
54    }
55}
56
57impl<T: Visit> Visit for Vec<T> {
58    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
59        for v in self {
60            v.visit(visitor)?;
61        }
62        ControlFlow::Continue(())
63    }
64}
65
66impl<T: Visit> Visit for Box<T> {
67    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
68        T::visit(self, visitor)
69    }
70}
71
72impl<T: VisitMut> VisitMut for Option<T> {
73    fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
74        if let Some(s) = self {
75            s.visit(visitor)?;
76        }
77        ControlFlow::Continue(())
78    }
79}
80
81impl<T: VisitMut> VisitMut for Vec<T> {
82    fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
83        for v in self {
84            v.visit(visitor)?;
85        }
86        ControlFlow::Continue(())
87    }
88}
89
90impl<T: VisitMut> VisitMut for Box<T> {
91    fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
92        T::visit(self, visitor)
93    }
94}
95
96macro_rules! visit_noop {
97    ($($t:ty),+) => {
98        $(impl Visit for $t {
99            fn visit<V: Visitor>(&self, _visitor: &mut V) -> ControlFlow<V::Break> {
100               ControlFlow::Continue(())
101            }
102        })+
103        $(impl VisitMut for $t {
104            fn visit<V: VisitorMut>(&mut self, _visitor: &mut V) -> ControlFlow<V::Break> {
105               ControlFlow::Continue(())
106            }
107        })+
108    };
109}
110
111visit_noop!(u8, u16, u32, u64, i8, i16, i32, i64, char, bool, String);
112
113#[cfg(feature = "bigdecimal")]
114visit_noop!(bigdecimal::BigDecimal);
115
116/// A visitor that can be used to walk an AST tree.
117///
118/// `pre_visit_` methods are invoked before visiting all children of the
119/// node and `post_visit_` methods are invoked after visiting all
120/// children of the node.
121///
122/// # See also
123///
124/// These methods provide a more concise way of visiting nodes of a certain type:
125/// * [visit_relations]
126/// * [visit_expressions]
127/// * [visit_statements]
128///
129/// # Example
130/// ```
131/// # use sqlparser::parser::Parser;
132/// # use sqlparser::dialect::GenericDialect;
133/// # use sqlparser::ast::{Visit, Visitor, ObjectName, Expr};
134/// # use core::ops::ControlFlow;
135/// // A structure that records statements and relations
136/// #[derive(Default)]
137/// struct V {
138///    visited: Vec<String>,
139/// }
140///
141/// // Visit relations and exprs before children are visited (depth first walk)
142/// // Note you can also visit statements and visit exprs after children have been visited
143/// impl Visitor for V {
144///   type Break = ();
145///
146///   fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<Self::Break> {
147///     self.visited.push(format!("PRE: RELATION: {}", relation));
148///     ControlFlow::Continue(())
149///   }
150///
151///   fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
152///     self.visited.push(format!("PRE: EXPR: {}", expr));
153///     ControlFlow::Continue(())
154///   }
155/// }
156///
157/// let sql = "SELECT a FROM foo where x IN (SELECT y FROM bar)";
158/// let statements = Parser::parse_sql(&GenericDialect{}, sql)
159///    .unwrap();
160///
161/// // Drive the visitor through the AST
162/// let mut visitor = V::default();
163/// statements.visit(&mut visitor);
164///
165/// // The visitor has visited statements and expressions in pre-traversal order
166/// let expected : Vec<_> = [
167///   "PRE: EXPR: a",
168///   "PRE: RELATION: foo",
169///   "PRE: EXPR: x IN (SELECT y FROM bar)",
170///   "PRE: EXPR: x",
171///   "PRE: EXPR: y",
172///   "PRE: RELATION: bar",
173/// ]
174///   .into_iter().map(|s| s.to_string()).collect();
175///
176/// assert_eq!(visitor.visited, expected);
177/// ```
178pub trait Visitor {
179    /// Type returned when the recursion returns early.
180    type Break;
181
182    /// Invoked for any relations (e.g. tables) that appear in the AST before visiting children
183    fn pre_visit_relation(&mut self, _relation: &ObjectName) -> ControlFlow<Self::Break> {
184        ControlFlow::Continue(())
185    }
186
187    /// Invoked for any relations (e.g. tables) that appear in the AST after visiting children
188    fn post_visit_relation(&mut self, _relation: &ObjectName) -> ControlFlow<Self::Break> {
189        ControlFlow::Continue(())
190    }
191
192    /// Invoked for any table factors that appear in the AST before visiting children
193    fn pre_visit_table_factor(&mut self, _table_factor: &TableFactor) -> ControlFlow<Self::Break> {
194        ControlFlow::Continue(())
195    }
196
197    /// Invoked for any table factors that appear in the AST after visiting children
198    fn post_visit_table_factor(&mut self, _table_factor: &TableFactor) -> ControlFlow<Self::Break> {
199        ControlFlow::Continue(())
200    }
201
202    /// Invoked for any expressions that appear in the AST before visiting children
203    fn pre_visit_expr(&mut self, _expr: &Expr) -> ControlFlow<Self::Break> {
204        ControlFlow::Continue(())
205    }
206
207    /// Invoked for any expressions that appear in the AST
208    fn post_visit_expr(&mut self, _expr: &Expr) -> ControlFlow<Self::Break> {
209        ControlFlow::Continue(())
210    }
211
212    /// Invoked for any statements that appear in the AST before visiting children
213    fn pre_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<Self::Break> {
214        ControlFlow::Continue(())
215    }
216
217    /// Invoked for any statements that appear in the AST after visiting children
218    fn post_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<Self::Break> {
219        ControlFlow::Continue(())
220    }
221}
222
223/// A visitor that can be used to mutate an AST tree.
224///
225/// `pre_visit_` methods are invoked before visiting all children of the
226/// node and `post_visit_` methods are invoked after visiting all
227/// children of the node.
228///
229/// # See also
230///
231/// These methods provide a more concise way of visiting nodes of a certain type:
232/// * [visit_relations_mut]
233/// * [visit_expressions_mut]
234/// * [visit_statements_mut]
235///
236/// # Example
237/// ```
238/// # use sqlparser::parser::Parser;
239/// # use sqlparser::dialect::GenericDialect;
240/// # use sqlparser::ast::{VisitMut, VisitorMut, ObjectName, Expr, Ident};
241/// # use core::ops::ControlFlow;
242///
243/// // A visitor that replaces "to_replace" with "replaced" in all expressions
244/// struct Replacer;
245///
246/// // Visit each expression after its children have been visited
247/// impl VisitorMut for Replacer {
248///   type Break = ();
249///
250///   fn post_visit_expr(&mut self, expr: &mut Expr) -> ControlFlow<Self::Break> {
251///     if let Expr::Identifier(Ident{ value, ..}) = expr {
252///         *value = value.replace("to_replace", "replaced")
253///     }
254///     ControlFlow::Continue(())
255///   }
256/// }
257///
258/// let sql = "SELECT to_replace FROM foo where to_replace IN (SELECT to_replace FROM bar)";
259/// let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap();
260///
261/// // Drive the visitor through the AST
262/// statements.visit(&mut Replacer);
263///
264/// assert_eq!(statements[0].to_string(), "SELECT replaced FROM foo WHERE replaced IN (SELECT replaced FROM bar)");
265/// ```
266pub trait VisitorMut {
267    /// Type returned when the recursion returns early.
268    type Break;
269
270    /// Invoked for any relations (e.g. tables) that appear in the AST before visiting children
271    fn pre_visit_relation(&mut self, _relation: &mut ObjectName) -> ControlFlow<Self::Break> {
272        ControlFlow::Continue(())
273    }
274
275    /// Invoked for any relations (e.g. tables) that appear in the AST after visiting children
276    fn post_visit_relation(&mut self, _relation: &mut ObjectName) -> ControlFlow<Self::Break> {
277        ControlFlow::Continue(())
278    }
279
280    /// Invoked for any table factors that appear in the AST before visiting children
281    fn pre_visit_table_factor(
282        &mut self,
283        _table_factor: &mut TableFactor,
284    ) -> ControlFlow<Self::Break> {
285        ControlFlow::Continue(())
286    }
287
288    /// Invoked for any table factors that appear in the AST after visiting children
289    fn post_visit_table_factor(
290        &mut self,
291        _table_factor: &mut TableFactor,
292    ) -> ControlFlow<Self::Break> {
293        ControlFlow::Continue(())
294    }
295
296    /// Invoked for any expressions that appear in the AST before visiting children
297    fn pre_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break> {
298        ControlFlow::Continue(())
299    }
300
301    /// Invoked for any expressions that appear in the AST
302    fn post_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break> {
303        ControlFlow::Continue(())
304    }
305
306    /// Invoked for any statements that appear in the AST before visiting children
307    fn pre_visit_statement(&mut self, _statement: &mut Statement) -> ControlFlow<Self::Break> {
308        ControlFlow::Continue(())
309    }
310
311    /// Invoked for any statements that appear in the AST after visiting children
312    fn post_visit_statement(&mut self, _statement: &mut Statement) -> ControlFlow<Self::Break> {
313        ControlFlow::Continue(())
314    }
315}
316
317struct RelationVisitor<F>(F);
318
319impl<E, F: FnMut(&ObjectName) -> ControlFlow<E>> Visitor for RelationVisitor<F> {
320    type Break = E;
321
322    fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<Self::Break> {
323        self.0(relation)
324    }
325}
326
327impl<E, F: FnMut(&mut ObjectName) -> ControlFlow<E>> VisitorMut for RelationVisitor<F> {
328    type Break = E;
329
330    fn post_visit_relation(&mut self, relation: &mut ObjectName) -> ControlFlow<Self::Break> {
331        self.0(relation)
332    }
333}
334
335/// Invokes the provided closure on all relations (e.g. table names) present in `v`
336///
337/// # Example
338/// ```
339/// # use sqlparser::parser::Parser;
340/// # use sqlparser::dialect::GenericDialect;
341/// # use sqlparser::ast::{visit_relations};
342/// # use core::ops::ControlFlow;
343/// let sql = "SELECT a FROM foo where x IN (SELECT y FROM bar)";
344/// let statements = Parser::parse_sql(&GenericDialect{}, sql)
345///    .unwrap();
346///
347/// // visit statements, capturing relations (table names)
348/// let mut visited = vec![];
349/// visit_relations(&statements, |relation| {
350///   visited.push(format!("RELATION: {}", relation));
351///   ControlFlow::<()>::Continue(())
352/// });
353///
354/// let expected : Vec<_> = [
355///   "RELATION: foo",
356///   "RELATION: bar",
357/// ]
358///   .into_iter().map(|s| s.to_string()).collect();
359///
360/// assert_eq!(visited, expected);
361/// ```
362pub fn visit_relations<V, E, F>(v: &V, f: F) -> ControlFlow<E>
363where
364    V: Visit,
365    F: FnMut(&ObjectName) -> ControlFlow<E>,
366{
367    let mut visitor = RelationVisitor(f);
368    v.visit(&mut visitor)?;
369    ControlFlow::Continue(())
370}
371
372/// Invokes the provided closure with a mutable reference to all relations (e.g. table names)
373/// present in `v`.
374///
375/// When the closure mutates its argument, the new mutated relation will not be visited again.
376///
377/// # Example
378/// ```
379/// # use sqlparser::parser::Parser;
380/// # use sqlparser::dialect::GenericDialect;
381/// # use sqlparser::ast::{ObjectName, visit_relations_mut};
382/// # use core::ops::ControlFlow;
383/// let sql = "SELECT a FROM foo";
384/// let mut statements = Parser::parse_sql(&GenericDialect{}, sql)
385///    .unwrap();
386///
387/// // visit statements, renaming table foo to bar
388/// visit_relations_mut(&mut statements, |table| {
389///   table.0[0].value = table.0[0].value.replace("foo", "bar");
390///   ControlFlow::<()>::Continue(())
391/// });
392///
393/// assert_eq!(statements[0].to_string(), "SELECT a FROM bar");
394/// ```
395pub fn visit_relations_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
396where
397    V: VisitMut,
398    F: FnMut(&mut ObjectName) -> ControlFlow<E>,
399{
400    let mut visitor = RelationVisitor(f);
401    v.visit(&mut visitor)?;
402    ControlFlow::Continue(())
403}
404
405struct ExprVisitor<F>(F);
406
407impl<E, F: FnMut(&Expr) -> ControlFlow<E>> Visitor for ExprVisitor<F> {
408    type Break = E;
409
410    fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
411        self.0(expr)
412    }
413}
414
415impl<E, F: FnMut(&mut Expr) -> ControlFlow<E>> VisitorMut for ExprVisitor<F> {
416    type Break = E;
417
418    fn post_visit_expr(&mut self, expr: &mut Expr) -> ControlFlow<Self::Break> {
419        self.0(expr)
420    }
421}
422
423/// Invokes the provided closure on all expressions (e.g. `1 + 2`) present in `v`
424///
425/// # Example
426/// ```
427/// # use sqlparser::parser::Parser;
428/// # use sqlparser::dialect::GenericDialect;
429/// # use sqlparser::ast::{visit_expressions};
430/// # use core::ops::ControlFlow;
431/// let sql = "SELECT a FROM foo where x IN (SELECT y FROM bar)";
432/// let statements = Parser::parse_sql(&GenericDialect{}, sql)
433///    .unwrap();
434///
435/// // visit all expressions
436/// let mut visited = vec![];
437/// visit_expressions(&statements, |expr| {
438///   visited.push(format!("EXPR: {}", expr));
439///   ControlFlow::<()>::Continue(())
440/// });
441///
442/// let expected : Vec<_> = [
443///   "EXPR: a",
444///   "EXPR: x IN (SELECT y FROM bar)",
445///   "EXPR: x",
446///   "EXPR: y",
447/// ]
448///   .into_iter().map(|s| s.to_string()).collect();
449///
450/// assert_eq!(visited, expected);
451/// ```
452pub fn visit_expressions<V, E, F>(v: &V, f: F) -> ControlFlow<E>
453where
454    V: Visit,
455    F: FnMut(&Expr) -> ControlFlow<E>,
456{
457    let mut visitor = ExprVisitor(f);
458    v.visit(&mut visitor)?;
459    ControlFlow::Continue(())
460}
461
462/// Invokes the provided closure iteratively with a mutable reference to all expressions
463/// present in `v`.
464///
465/// This performs a depth-first search, so if the closure mutates the expression
466///
467/// # Example
468///
469/// ## Remove all select limits in sub-queries
470/// ```
471/// # use sqlparser::parser::Parser;
472/// # use sqlparser::dialect::GenericDialect;
473/// # use sqlparser::ast::{Expr, visit_expressions_mut, visit_statements_mut};
474/// # use core::ops::ControlFlow;
475/// let sql = "SELECT (SELECT y FROM z LIMIT 9) FROM t LIMIT 3";
476/// let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap();
477///
478/// // Remove all select limits in sub-queries
479/// visit_expressions_mut(&mut statements, |expr| {
480///   if let Expr::Subquery(q) = expr {
481///      q.limit = None
482///   }
483///   ControlFlow::<()>::Continue(())
484/// });
485///
486/// assert_eq!(statements[0].to_string(), "SELECT (SELECT y FROM z) FROM t LIMIT 3");
487/// ```
488///
489/// ## Wrap column name in function call
490///
491/// This demonstrates how to effectively replace an expression with another more complicated one
492/// that references the original. This example avoids unnecessary allocations by using the
493/// [`std::mem`] family of functions.
494///
495/// ```
496/// # use sqlparser::parser::Parser;
497/// # use sqlparser::dialect::GenericDialect;
498/// # use sqlparser::ast::{Expr, Function, FunctionArg, FunctionArgExpr, Ident, ObjectName, Value, visit_expressions_mut, visit_statements_mut};
499/// # use core::ops::ControlFlow;
500/// let sql = "SELECT x, y FROM t";
501/// let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap();
502///
503/// visit_expressions_mut(&mut statements, |expr| {
504///   if matches!(expr, Expr::Identifier(col_name) if col_name.value == "x") {
505///     let old_expr = std::mem::replace(expr, Expr::Value(Value::Null));
506///     *expr = Expr::Function(Function {
507///           name: ObjectName(vec![Ident::new("f")]),
508///           args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(old_expr))],
509///           null_treatment: None,
510///           filter: None, over: None, distinct: false, special: false, order_by: vec![],
511///      });
512///   }
513///   ControlFlow::<()>::Continue(())
514/// });
515///
516/// assert_eq!(statements[0].to_string(), "SELECT f(x), y FROM t");
517/// ```
518pub fn visit_expressions_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
519where
520    V: VisitMut,
521    F: FnMut(&mut Expr) -> ControlFlow<E>,
522{
523    v.visit(&mut ExprVisitor(f))?;
524    ControlFlow::Continue(())
525}
526
527struct StatementVisitor<F>(F);
528
529impl<E, F: FnMut(&Statement) -> ControlFlow<E>> Visitor for StatementVisitor<F> {
530    type Break = E;
531
532    fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<Self::Break> {
533        self.0(statement)
534    }
535}
536
537impl<E, F: FnMut(&mut Statement) -> ControlFlow<E>> VisitorMut for StatementVisitor<F> {
538    type Break = E;
539
540    fn post_visit_statement(&mut self, statement: &mut Statement) -> ControlFlow<Self::Break> {
541        self.0(statement)
542    }
543}
544
545/// Invokes the provided closure iteratively with a mutable reference to all statements
546/// present in `v` (e.g. `SELECT`, `CREATE TABLE`, etc).
547///
548/// # Example
549/// ```
550/// # use sqlparser::parser::Parser;
551/// # use sqlparser::dialect::GenericDialect;
552/// # use sqlparser::ast::{visit_statements};
553/// # use core::ops::ControlFlow;
554/// let sql = "SELECT a FROM foo where x IN (SELECT y FROM bar); CREATE TABLE baz(q int)";
555/// let statements = Parser::parse_sql(&GenericDialect{}, sql)
556///    .unwrap();
557///
558/// // visit all statements
559/// let mut visited = vec![];
560/// visit_statements(&statements, |stmt| {
561///   visited.push(format!("STATEMENT: {}", stmt));
562///   ControlFlow::<()>::Continue(())
563/// });
564///
565/// let expected : Vec<_> = [
566///   "STATEMENT: SELECT a FROM foo WHERE x IN (SELECT y FROM bar)",
567///   "STATEMENT: CREATE TABLE baz (q INT)"
568/// ]
569///   .into_iter().map(|s| s.to_string()).collect();
570///
571/// assert_eq!(visited, expected);
572/// ```
573pub fn visit_statements<V, E, F>(v: &V, f: F) -> ControlFlow<E>
574where
575    V: Visit,
576    F: FnMut(&Statement) -> ControlFlow<E>,
577{
578    let mut visitor = StatementVisitor(f);
579    v.visit(&mut visitor)?;
580    ControlFlow::Continue(())
581}
582
583/// Invokes the provided closure on all statements (e.g. `SELECT`, `CREATE TABLE`, etc) present in `v`
584///
585/// # Example
586/// ```
587/// # use sqlparser::parser::Parser;
588/// # use sqlparser::dialect::GenericDialect;
589/// # use sqlparser::ast::{Statement, visit_statements_mut};
590/// # use core::ops::ControlFlow;
591/// let sql = "SELECT x FROM foo LIMIT 9+$limit; SELECT * FROM t LIMIT f()";
592/// let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap();
593///
594/// // Remove all select limits in outer statements (not in sub-queries)
595/// visit_statements_mut(&mut statements, |stmt| {
596///   if let Statement::Query(q) = stmt {
597///      q.limit = None
598///   }
599///   ControlFlow::<()>::Continue(())
600/// });
601///
602/// assert_eq!(statements[0].to_string(), "SELECT x FROM foo");
603/// assert_eq!(statements[1].to_string(), "SELECT * FROM t");
604/// ```
605pub fn visit_statements_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
606where
607    V: VisitMut,
608    F: FnMut(&mut Statement) -> ControlFlow<E>,
609{
610    v.visit(&mut StatementVisitor(f))?;
611    ControlFlow::Continue(())
612}
613
614#[cfg(test)]
615mod tests {
616    use super::*;
617    use crate::dialect::GenericDialect;
618    use crate::parser::Parser;
619    use crate::tokenizer::Tokenizer;
620
621    #[derive(Default)]
622    struct TestVisitor {
623        visited: Vec<String>,
624    }
625
626    impl Visitor for TestVisitor {
627        type Break = ();
628
629        fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<Self::Break> {
630            self.visited.push(format!("PRE: RELATION: {relation}"));
631            ControlFlow::Continue(())
632        }
633
634        fn post_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<Self::Break> {
635            self.visited.push(format!("POST: RELATION: {relation}"));
636            ControlFlow::Continue(())
637        }
638
639        fn pre_visit_table_factor(
640            &mut self,
641            table_factor: &TableFactor,
642        ) -> ControlFlow<Self::Break> {
643            self.visited
644                .push(format!("PRE: TABLE FACTOR: {table_factor}"));
645            ControlFlow::Continue(())
646        }
647
648        fn post_visit_table_factor(
649            &mut self,
650            table_factor: &TableFactor,
651        ) -> ControlFlow<Self::Break> {
652            self.visited
653                .push(format!("POST: TABLE FACTOR: {table_factor}"));
654            ControlFlow::Continue(())
655        }
656
657        fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
658            self.visited.push(format!("PRE: EXPR: {expr}"));
659            ControlFlow::Continue(())
660        }
661
662        fn post_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
663            self.visited.push(format!("POST: EXPR: {expr}"));
664            ControlFlow::Continue(())
665        }
666
667        fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<Self::Break> {
668            self.visited.push(format!("PRE: STATEMENT: {statement}"));
669            ControlFlow::Continue(())
670        }
671
672        fn post_visit_statement(&mut self, statement: &Statement) -> ControlFlow<Self::Break> {
673            self.visited.push(format!("POST: STATEMENT: {statement}"));
674            ControlFlow::Continue(())
675        }
676    }
677
678    fn do_visit(sql: &str) -> Vec<String> {
679        let dialect = GenericDialect {};
680        let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
681        let s = Parser::new(&dialect)
682            .with_tokens(tokens)
683            .parse_statement()
684            .unwrap();
685
686        let mut visitor = TestVisitor::default();
687        s.visit(&mut visitor);
688        visitor.visited
689    }
690
691    #[test]
692    fn test_sql() {
693        let tests = vec![
694            (
695                "SELECT * from table_name as my_table",
696                vec![
697                    "PRE: STATEMENT: SELECT * FROM table_name AS my_table",
698                    "PRE: TABLE FACTOR: table_name AS my_table",
699                    "PRE: RELATION: table_name",
700                    "POST: RELATION: table_name",
701                    "POST: TABLE FACTOR: table_name AS my_table",
702                    "POST: STATEMENT: SELECT * FROM table_name AS my_table",
703                ],
704            ),
705            (
706                "SELECT * from t1 join t2 on t1.id = t2.t1_id",
707                vec![
708                    "PRE: STATEMENT: SELECT * FROM t1 JOIN t2 ON t1.id = t2.t1_id",
709                    "PRE: TABLE FACTOR: t1",
710                    "PRE: RELATION: t1",
711                    "POST: RELATION: t1",
712                    "POST: TABLE FACTOR: t1",
713                    "PRE: TABLE FACTOR: t2",
714                    "PRE: RELATION: t2",
715                    "POST: RELATION: t2",
716                    "POST: TABLE FACTOR: t2",
717                    "PRE: EXPR: t1.id = t2.t1_id",
718                    "PRE: EXPR: t1.id",
719                    "POST: EXPR: t1.id",
720                    "PRE: EXPR: t2.t1_id",
721                    "POST: EXPR: t2.t1_id",
722                    "POST: EXPR: t1.id = t2.t1_id",
723                    "POST: STATEMENT: SELECT * FROM t1 JOIN t2 ON t1.id = t2.t1_id",
724                ],
725            ),
726            (
727                "SELECT * from t1 where EXISTS(SELECT column from t2)",
728                vec![
729                    "PRE: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2)",
730                    "PRE: TABLE FACTOR: t1",
731                    "PRE: RELATION: t1",
732                    "POST: RELATION: t1",
733                    "POST: TABLE FACTOR: t1",
734                    "PRE: EXPR: EXISTS (SELECT column FROM t2)",
735                    "PRE: EXPR: column",
736                    "POST: EXPR: column",
737                    "PRE: TABLE FACTOR: t2",
738                    "PRE: RELATION: t2",
739                    "POST: RELATION: t2",
740                    "POST: TABLE FACTOR: t2",
741                    "POST: EXPR: EXISTS (SELECT column FROM t2)",
742                    "POST: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2)",
743                ],
744            ),
745            (
746                "SELECT * from t1 where EXISTS(SELECT column from t2)",
747                vec![
748                    "PRE: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2)",
749                    "PRE: TABLE FACTOR: t1",
750                    "PRE: RELATION: t1",
751                    "POST: RELATION: t1",
752                    "POST: TABLE FACTOR: t1",
753                    "PRE: EXPR: EXISTS (SELECT column FROM t2)",
754                    "PRE: EXPR: column",
755                    "POST: EXPR: column",
756                    "PRE: TABLE FACTOR: t2",
757                    "PRE: RELATION: t2",
758                    "POST: RELATION: t2",
759                    "POST: TABLE FACTOR: t2",
760                    "POST: EXPR: EXISTS (SELECT column FROM t2)",
761                    "POST: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2)",
762                ],
763            ),
764            (
765                "SELECT * from t1 where EXISTS(SELECT column from t2) UNION SELECT * from t3",
766                vec![
767                    "PRE: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2) UNION SELECT * FROM t3",
768                    "PRE: TABLE FACTOR: t1",
769                    "PRE: RELATION: t1",
770                    "POST: RELATION: t1",
771                    "POST: TABLE FACTOR: t1",
772                    "PRE: EXPR: EXISTS (SELECT column FROM t2)",
773                    "PRE: EXPR: column",
774                    "POST: EXPR: column",
775                    "PRE: TABLE FACTOR: t2",
776                    "PRE: RELATION: t2",
777                    "POST: RELATION: t2",
778                    "POST: TABLE FACTOR: t2",
779                    "POST: EXPR: EXISTS (SELECT column FROM t2)",
780                    "PRE: TABLE FACTOR: t3",
781                    "PRE: RELATION: t3",
782                    "POST: RELATION: t3",
783                    "POST: TABLE FACTOR: t3",
784                    "POST: STATEMENT: SELECT * FROM t1 WHERE EXISTS (SELECT column FROM t2) UNION SELECT * FROM t3",
785                ],
786            ),
787        ];
788        for (sql, expected) in tests {
789            let actual = do_visit(sql);
790            let actual: Vec<_> = actual.iter().map(|x| x.as_str()).collect();
791            assert_eq!(actual, expected)
792        }
793    }
794}