pglast.visitors
— Other ways to inspect and manipulate the AST¶
-
class
pglast.visitors.
Action
¶ Abstract action singleton.
-
class
pglast.visitors.
ActionMeta
¶ Metaclass used to implement action singleton.
-
class
pglast.visitors.
Add
¶ Marker used to tell the iterator to insert nodes in the current sequence.
-
class
pglast.visitors.
Ancestor
(parent=None, node=None, member=None)¶ Simple object to keep track of the node’s ancestors while it’s being visited.
An instance of this class represent a particular ancestor in the hierarchy chain: it carries a reference that points to the higher item in the chain, the associated
ast.Node
instance and a member, either the attribute name or sequential index in the parent node.Iteration yields the sequence of involved members, that is the path starting from the root of the AST tree that leads to leaf node.
Accessing an instance with a positive index returns the nth node up in the hierarchy.
When applied (using the
@
operator) to anast.Node
instance will traverse that node returning the leaf one corresponding to the whole chain.Example:
>>> tree = parse_sql('select 1') >>> root = Ancestor() >>> root ROOT >>> root@tree is tree True >>> root[0] is None True >>> select_stmt_path = root / (root, 0) / (tree[0], 'stmt') >>> select_stmt_path ROOT → 0 → stmt >>> select_stmt_path@tree is tree[0].stmt True >>> select_stmt_path[0] is tree[0] True >>> columns_path = select_stmt_path / (tree[0].stmt, 'targetList') >>> first_col_path = columns_path / (tree[0].stmt.targetList[0], 0) >>> first_col_path ROOT → 0 → stmt → targetList → 0 >>> first_col_path[0] <ResTarget val=<A_Const val=<Integer val=1>>> >>> first_col_path[1] is columns_path[0] True
-
class
pglast.visitors.
Continue
¶ Marker used to tell the iterator to keep going.
-
class
pglast.visitors.
Delete
¶ Marker used to tell the iterator to delete current node.
-
class
pglast.visitors.
RelationNames
¶ Concrete implementation of the
referenced_relations()
function.Calling an instance of this class will return a set of the names of the relations referenced by the given
node
.-
visit_CommonTableExpr
(ancestors, node)¶ Collect CTE names.
-
visit_RangeVar
(ancestors, node)¶ Collect relation names.
-
-
class
pglast.visitors.
Skip
¶ Marker used to tell the iterator to not descend into the current node.
-
class
pglast.visitors.
Visitor
¶ Base class implementing the visitor pattern.
To use it, you shall write a subclass that implements a set of particular named methods, specifically
visit_XYZ
whereXYZ
is the name of a class name defined in thepglast.ast
module.Instances of this class are callables and accept either a
pglast.ast.Node
instance or a sequence of instances, typically the result ofparse_sql
. The argument will be traversed in a breadth first order and eachNode
instance will be passed to the correspondingvisit_XYZ
method if it is implemented, falling back to the defaultvisit
method. If none of them are defined, the node will be ignored.The
visit_XYZ
methods receive two arguments: the ancestry chain of the node, an instance ofAncestor
and theNode
instance itself. The methods may return eitherNone
, an action or a new node that will replace the original one.-
iterate
(node)¶ Iterate thru node’s AST using a breadth-first traversing.
Parameters: node – either a Node
instance or a tuple of thoseThis is a generator, that yields
Node
instances together with their ancestors chain as it finds them while traversing the tree.
-
visit
= None¶ The default visit method for any node without a specific one. When
None
, nothing happens.
-
-
pglast.visitors.
referenced_relations
(stmt)¶ Return the set of relation names referenced in the given stmt.
Parameters: stmt – either a string containing the SQL
statement or aast.Node
instanceReturns: a set of strings, the names of the relations Example:
assert referenced_relations('WITH q1(x,y) AS (SELECT 1,2)' ' SELECT * FROM q1, q2') == {'q2'}
-
pglast.visitors.
visitable
(node)¶ Determine whether the given node is visitable or not.