walker
ModuleWalker and [Visitor]: rewriting a tree, and the visitors the
library ships.
Contents
use utopia_query::ast::walker::ColumnValidator;pub struct ColumnValidatorRejects columns outside an allow-list, and * unless allowed.
use utopia_query::ast::walker::FilterInjector;pub struct FilterInjectorAdds a condition to the WHERE of statements (AND-ed with an existing one).
Walked, it applies to every statement, subqueries included; to change
only the outer one, call Visitor::visit_select on it directly.
use utopia_query::ast::walker::TableRenamer;pub struct TableRenamerRenames tables: table references (name and alias) and the table
qualifier of columns and table.*.
use utopia_query::ast::walker::Walker;pub struct WalkerApplies a Visitor to every node of a statement, bottom-up: the
children of a node first, then the node. Statements nested in
subqueries, IN, EXISTS, derived tables and CTEs are walked the same
way, and each gets Visitor::visit_select.
Order within a statement: columns, FROM, joins (table, then
condition), WHERE, GROUP BY, HAVING, ORDER BY, LIMIT,
OFFSET, CTEs, WINDOW definitions.
use std::collections::HashMap;
use utopia_query::ast::{Parser, Serializer, TableRenamer, Walker};
let select = Parser::new().parse_sql("SELECT u.id FROM users u WHERE u.active = 1")?;
let mut renamer = TableRenamer::new(HashMap::from([("users".to_owned(), "accounts".to_owned())]));
let select = Walker::new().walk(select, &mut renamer)?;
assert_eq!(Serializer::new().serialize(&select)?, "SELECT `u`.`id` FROM `accounts` AS `u` WHERE `u`.`active` = 1");use utopia_query::ast::walker::Visitor;pub trait VisitorInspects or rewrites the nodes a Walker visits: each method gets a
node (its children already walked) and returns it, or its replacement.
The defaults keep every node.