Trait sqlparser::ast::VisitorMut

source ·
pub trait VisitorMut {
    type 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_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.

previst_ methods are invoked before visiting all children of the node and postvisit_ 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 returned when the recursion returns early.

Provided Methods§

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

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

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

Invoked for any expressions that appear in the AST

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

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

Implementors§