Class: QgsExpressionNode¶
Abstract base class for all nodes that can appear in an expression.
Note
This is an abstract class, with methods which must be implemented by a subclass.
The following methods must be implemented: clone()
, dump()
, isStatic()
, needsGeometry()
, nodeType()
, referencedColumns()
, referencedFunctions()
, referencedVariables()
Class Hierarchy¶
Subclasses¶
SQL-like BETWEEN and NOT BETWEEN predicates. |
|
A binary expression operator, which operates on two values. |
|
An expression node which takes its value from a feature’s field. |
|
An expression node for CASE WHEN clauses. |
|
An expression node for expression functions. |
|
An expression node for value IN or NOT IN clauses. |
|
An indexing expression operator, which allows use of square brackets [] to reference map and array items. |
|
An expression node for literal values. |
|
A unary node is either negative as in boolean (not) or as in numbers (minus). |
Abstract Methods
Generate a clone of this node. |
|
Dump this node into a serialized (part) of an expression. |
|
Returns |
|
Abstract virtual method which returns if the geometry is required to evaluate this expression. |
|
Gets the type of this node. |
|
Abstract virtual method which returns a list of columns required to evaluate this node. |
|
Returns a set of all functions which are used in this expression. |
|
Returns a set of all variables which are used in this expression. |
Methods
Returns the node's static cached value. |
|
Returns a reference to the simplest node which represents this node, after any compilation optimizations have been applied. |
|
Evaluate this node with the given context and parent. |
|
Returns |
|
Prepare this node for evaluation. |
Attributes
First column in the parser this node was found. |
|
First line in the parser this node was found. |
|
Last column in the parser this node was found. |
|
Last line in the parser this node was found. |
- class qgis.core.QgsExpressionNode[source]¶
Bases:
object
- class NamedNode¶
Bases:
object
Named node
- name: str¶
Node name
- node: QgsExpressionNode¶
Node
- class NodeList[source]¶
Bases:
object
A list of expression nodes.
- append(self, node: QgsExpressionNode | None)[source]¶
Takes ownership of the provided node
- Parameters:
node (Optional[QgsExpressionNode])
- append(self, node: QgsExpressionNode.NamedNode | None)[source]
Adds a named node. Takes ownership of the provided node.
- Parameters:
node (Optional[QgsExpressionNode.NamedNode])
- at(self, i: int) QgsExpressionNode | None [source]¶
Gets the node at position i in the list.
- Parameters:
i (int)
- Return type:
Optional[QgsExpressionNode]
- clone(self) QgsExpressionNode.NodeList | None [source]¶
Creates a deep copy of this list. Ownership is transferred to the caller
- Return type:
Optional[QgsExpressionNode.NodeList]
- list(self) List[QgsExpressionNode] ¶
Gets a list of all the nodes.
- Return type:
List[QgsExpressionNode]
- class NodeType¶
Bases:
int
- cachedStaticValue(self) Any [source]¶
Returns the node’s static cached value. Only valid if
hasCachedStaticValue()
isTrue
.See also
Added in version 3.18.
- Return type:
Any
- abstract clone(self) QgsExpressionNode | None [source]¶
Generate a clone of this node. Ownership is transferred to the caller.
- Return type:
Optional[QgsExpressionNode]
- Returns:
a deep copy of this node.
- abstract dump(self) str [source]¶
Dump this node into a serialized (part) of an expression. The returned expression does not necessarily literally match the original expression, it’s just guaranteed to behave the same way.
- Return type:
str
- effectiveNode(self) QgsExpressionNode | None [source]¶
Returns a reference to the simplest node which represents this node, after any compilation optimizations have been applied.
Eg. a node like “CASE WHEN true THEN “some_field” WHEN other condition THEN … END” can effectively be replaced entirely by a
QgsExpressionNodeColumnRef
referencing the “some_field” field, as the CASE WHEN … will ALWAYS evaluate to “some_field”.Returns a reference to the current object if no optimizations were applied.
Added in version 3.20.
- Return type:
Optional[QgsExpressionNode]
- eval(self, parent: QgsExpression | None, context: QgsExpressionContext | None) Any [source]¶
Evaluate this node with the given context and parent. This will return a cached value if it has been determined to be static during the
prepare()
execution.- Parameters:
parent (Optional[QgsExpression])
context (Optional[QgsExpressionContext])
- Return type:
Any
- hasCachedStaticValue(self) bool [source]¶
Returns
True
if the node can be replaced by a static cached value.See also
Added in version 3.18.
- Return type:
bool
- abstract isStatic(self, parent: QgsExpression | None, context: QgsExpressionContext | None) bool [source]¶
Returns
True
if this node can be evaluated for a static value. This is used during theprepare()
step and in case it returnsTrue
, the value of this node will already be evaluated and the result cached (and therefore not re-evaluated in subsequent calls toeval()
). In case this returnsTrue
,prepareNode()
will never be called.- Parameters:
parent (Optional[QgsExpression])
context (Optional[QgsExpressionContext])
- Return type:
bool
- abstract needsGeometry(self) bool [source]¶
Abstract virtual method which returns if the geometry is required to evaluate this expression.
This needs to call :py:func:`~QgsExpressionNode.needsGeometry` recursively on any child nodes.
- Return type:
bool
- Returns:
True
if a geometry is required to evaluate this expression
- abstract nodeType(self) QgsExpressionNode.NodeType [source]¶
Gets the type of this node.
- Return type:
- Returns:
The type of this node
- ntBetweenOperator = 8¶
- ntBinaryOperator = 1¶
- ntColumnRef = 5¶
- ntCondition = 6¶
- ntFunction = 3¶
- ntInOperator = 2¶
- ntIndexOperator = 7¶
- ntLiteral = 4¶
- ntUnaryOperator = 0¶
- parserFirstColumn: int¶
First column in the parser this node was found.
Note
This might not be complete for all nodes. Currently only
QgsExpressionNode
has this complete
- parserFirstLine: int¶
First line in the parser this node was found.
Note
This might not be complete for all nodes. Currently only
QgsExpressionNode
has this complete
- parserLastColumn: int¶
Last column in the parser this node was found.
Note
This might not be complete for all nodes. Currently only
QgsExpressionNode
has this complete
- parserLastLine: int¶
Last line in the parser this node was found.
Note
This might not be complete for all nodes. Currently only
QgsExpressionNode
has this complete
- prepare(self, parent: QgsExpression | None, context: QgsExpressionContext | None) bool [source]¶
Prepare this node for evaluation. This will check if the node content is static and in this case cache the value. If it’s not static it will call
prepareNode()
to allow the node to do initialization work like for example resolving a column name to an attribute index.- Parameters:
parent (Optional[QgsExpression])
context (Optional[QgsExpressionContext])
- Return type:
bool
- abstract referencedColumns(self) Set[str] ¶
Abstract virtual method which returns a list of columns required to evaluate this node.
When reimplementing this, you need to return any column that is required to evaluate this node and in addition recursively collect all the columns required to evaluate child nodes.
Warning
If the expression has been prepared via a call to
QgsExpression.prepare()
, or a call toQgsExpressionNode.prepare()
for a node has been made, then some nodes in the expression may have been determined to evaluate to a static pre-calculatable value. In this case the results will omit attribute indices which are used by these pre-calculated nodes, regardless of their actual referenced columns. If you are seeking to use these functions to introspect an expression you must take care to do this with an unprepared expression node.- Return type:
Set[str]
- Returns:
A list of columns required to evaluate this expression
- abstract referencedFunctions(self) Set[str] ¶
Returns a set of all functions which are used in this expression.
Note
In contrast to the
referencedColumns()
function this method is not affected by any previous calls toQgsExpressionNode.prepare()
.- Return type:
Set[str]
- abstract referencedVariables(self) Set[str] ¶
Returns a set of all variables which are used in this expression.
Note
In contrast to the
referencedColumns()
function this method is not affected by any previous calls toQgsExpressionNode.prepare()
.- Return type:
Set[str]