sql::ast

Trait VisitorMut

pub trait VisitorMut {
    type Break;

    // Provided methods
    fn pre_visit_query(
        &mut self,
        _query: &mut Query,
    ) -> ControlFlow<Self::Break> { ... }
    fn post_visit_query(
        &mut self,
        _query: &mut Query,
    ) -> ControlFlow<Self::Break> { ... }
    fn pre_visit_relation(
        &mut self,
        _relation: &mut ObjectName,
    ) -> ControlFlow<Self::Break> { ... }
    fn post_visit_relation(
        &mut self,
        _relation: &mut ObjectName,
    ) -> ControlFlow<Self::Break> { ... }
    fn pre_visit_table_factor(
        &mut self,
        _table_factor: &mut TableFactor,
    ) -> ControlFlow<Self::Break> { ... }
    fn post_visit_table_factor(
        &mut self,
        _table_factor: &mut TableFactor,
    ) -> ControlFlow<Self::Break> { ... }
    fn pre_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break> { ... }
    fn post_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break> { ... }
    fn pre_visit_statement(
        &mut self,
        _statement: &mut Statement,
    ) -> ControlFlow<Self::Break> { ... }
    fn post_visit_statement(
        &mut self,
        _statement: &mut Statement,
    ) -> ControlFlow<Self::Break> { ... }
}
Expand description

A visitor that can be used to mutate an AST tree.

pre_visit_ methods are invoked before visiting all children of the node and post_visit_ methods are invoked after visiting all children of the node.

§See also

These methods provide a more concise way of visiting nodes of a certain type:

§Example


// A visitor that replaces "to_replace" with "replaced" in all expressions
struct Replacer;

// Visit each expression after its children have been visited
impl VisitorMut for Replacer {
  type Break = ();

  fn post_visit_expr(&mut self, expr: &mut Expr) -> ControlFlow<Self::Break> {
    if let Expr::Identifier(Ident{ value, ..}) = expr {
        *value = value.replace("to_replace", "replaced")
    }
    ControlFlow::Continue(())
  }
}

let sql = "SELECT to_replace FROM foo where to_replace IN (SELECT to_replace FROM bar)";
let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap();

// Drive the visitor through the AST
statements.visit(&mut Replacer);

assert_eq!(statements[0].to_string(), "SELECT replaced FROM foo WHERE replaced IN (SELECT replaced FROM bar)");

Required Associated Types§

type Break

Type returned when the recursion returns early.

Provided Methods§

fn pre_visit_query(&mut self, _query: &mut Query) -> ControlFlow<Self::Break>

Invoked for any queries that appear in the AST before visiting children

fn post_visit_query(&mut self, _query: &mut Query) -> ControlFlow<Self::Break>

Invoked for any queries that appear in the AST after visiting children

fn pre_visit_relation( &mut self, _relation: &mut ObjectName, ) -> ControlFlow<Self::Break>

Invoked for any relations (e.g. tables) that appear in the AST before visiting children

fn post_visit_relation( &mut self, _relation: &mut ObjectName, ) -> ControlFlow<Self::Break>

Invoked for any relations (e.g. tables) that appear in the AST after visiting children

fn pre_visit_table_factor( &mut self, _table_factor: &mut TableFactor, ) -> ControlFlow<Self::Break>

Invoked for any table factors that appear in the AST before visiting children

fn post_visit_table_factor( &mut self, _table_factor: &mut TableFactor, ) -> ControlFlow<Self::Break>

Invoked for any table factors that appear in the AST after visiting children

fn pre_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break>

Invoked for any expressions that appear in the AST before visiting children

fn post_visit_expr(&mut self, _expr: &mut Expr) -> ControlFlow<Self::Break>

Invoked for any expressions that appear in the AST

fn pre_visit_statement( &mut self, _statement: &mut Statement, ) -> ControlFlow<Self::Break>

Invoked for any statements that appear in the AST before visiting children

fn post_visit_statement( &mut self, _statement: &mut Statement, ) -> ControlFlow<Self::Break>

Invoked for any statements that appear in the AST after visiting children

Implementors§