QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexpressionnode.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionnode.h
3  -------------------
4  begin : May 2017
5  copyright : (C) 2017 Matthias Kuhn
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 
17 #ifndef QGSEXPRESSIONNODE_H
18 #define QGSEXPRESSIONNODE_H
19 
20 #include <QSet>
21 #include <QVariant>
22 #include <QCoreApplication>
23 
24 #include "qgis.h"
25 
26 class QgsExpression;
28 
35 {
36 
37 #ifdef SIP_RUN
39  switch ( sipCpp->nodeType() )
40  {
42  sipType = sipType_QgsExpressionNodeUnaryOperator;
43  break;
45  sipType = sipType_QgsExpressionNodeBinaryOperator;
46  break;
48  sipType = sipType_QgsExpressionNodeInOperator;
49  break;
51  sipType = sipType_QgsExpressionNodeFunction;
52  break;
54  sipType = sipType_QgsExpressionNodeLiteral;
55  break;
57  sipType = sipType_QgsExpressionNodeColumnRef;
58  break;
60  sipType = sipType_QgsExpressionNodeCondition;
61  break;
63  sipType = sipType_QgsExpressionNodeBetweenOperator;
64  break;
65  default:
66  sipType = 0;
67  break;
68  }
69  SIP_END
70 #endif
71 
72  Q_DECLARE_TR_FUNCTIONS( QgsExpressionNode )
73 
74  public:
75 
77  enum NodeType
78  {
88  };
89 
90 
96  struct NamedNode
97  {
98  public:
99 
105  NamedNode( const QString &name, QgsExpressionNode *node )
106  : name( name )
107  , node( node )
108  {}
109 
111  QString name;
112 
114  QgsExpressionNode *node = nullptr;
115  };
116 
121  class CORE_EXPORT NodeList
122  {
123  public:
124  virtual ~NodeList();
126  void append( QgsExpressionNode *node SIP_TRANSFER ) { mList.append( node ); mNameList.append( QString() ); }
127 
132  void append( QgsExpressionNode::NamedNode *node SIP_TRANSFER );
133 
137  int count() const { return mList.count(); }
138 
143  bool hasNamedNodes() const { return mHasNamedNodes; }
144 
148  QList<QgsExpressionNode *> list() { return mList; }
149 
155  QgsExpressionNode *at( int i ) { return mList.at( i ); }
156 
161  QStringList names() const { return mNameList; }
162 
165 
169  virtual QString dump() const;
170 
171  private:
172  QList<QgsExpressionNode *> mList;
173  QStringList mNameList;
174 
175  bool mHasNamedNodes = false;
176 
180  static QString cleanNamedNodeName( const QString &name );
181 
182  public:
183  };
184 
185  virtual ~QgsExpressionNode() = default;
186 
192  virtual QgsExpressionNode::NodeType nodeType() const = 0;
193 
199  virtual QString dump() const = 0;
200 
208  QVariant eval( QgsExpression *parent, const QgsExpressionContext *context );
209 
216  virtual QgsExpressionNode *clone() const = 0;
217 
236  virtual QSet<QString> referencedColumns() const = 0;
237 
244  virtual QSet<QString> referencedVariables() const = 0;
245 
252  virtual QSet<QString> referencedFunctions() const = 0;
253 
260  virtual QList<const QgsExpressionNode *> nodes( ) const = 0; SIP_SKIP
261 
270  virtual bool needsGeometry() const = 0;
271 
280  virtual bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const = 0;
281 
290  bool prepare( QgsExpression *parent, const QgsExpressionContext *context );
291 
297  int parserFirstLine = 0;
298 
304  int parserFirstColumn = 0;
305 
311  int parserLastLine = 0;
312 
318  int parserLastColumn = 0;
319 
326  bool hasCachedStaticValue() const { return mHasCachedValue; }
327 
334  QVariant cachedStaticValue() const { return mCachedStaticValue; }
335 
348  const QgsExpressionNode *effectiveNode() const { return mCompiledSimplifiedNode ? mCompiledSimplifiedNode.get() : this; }
349 
350  protected:
351 
355  QgsExpressionNode() = default;
356 
358  QgsExpressionNode( const QgsExpressionNode &other );
360  QgsExpressionNode &operator=( const QgsExpressionNode &other );
361 
371  void cloneTo( QgsExpressionNode *target ) const SIP_SKIP;
372 
373 #ifndef SIP_RUN
374 
380  mutable bool mHasCachedValue = false;
381 
387  mutable QVariant mCachedStaticValue;
388 
399  mutable std::unique_ptr< QgsExpressionNode > mCompiledSimplifiedNode;
400 #endif
401 
402  private:
403 
409  virtual bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) = 0;
410 
416  virtual QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) = 0;
417 
418 };
419 
421 
422 #endif // QGSEXPRESSIONNODE_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsExpressionNode::NamedNode::name
QString name
Node name.
Definition: qgsexpressionnode.h:111
QgsExpressionNode::effectiveNode
const QgsExpressionNode * effectiveNode() const
Returns a reference to the simplest node which represents this node, after any compilation optimizati...
Definition: qgsexpressionnode.h:348
QgsExpressionNode::ntBetweenOperator
@ ntBetweenOperator
Between operator.
Definition: qgsexpressionnode.h:87
QgsExpressionNode::ntCondition
@ ntCondition
Definition: qgsexpressionnode.h:85
QgsExpressionNode::NodeType
NodeType
Known node types.
Definition: qgsexpressionnode.h:77
QgsExpressionNode::NodeList::hasNamedNodes
bool hasNamedNodes() const
Returns true if list contains any named nodes.
Definition: qgsexpressionnode.h:143
QgsExpressionNode::ntColumnRef
@ ntColumnRef
Definition: qgsexpressionnode.h:84
qgis.h
QgsExpressionNode::NamedNode::NamedNode
NamedNode(const QString &name, QgsExpressionNode *node)
Constructor for NamedNode.
Definition: qgsexpressionnode.h:105
QgsExpressionNode::NodeList::at
QgsExpressionNode * at(int i)
Gets the node at position i in the list.
Definition: qgsexpressionnode.h:155
QgsExpressionNode::mCachedStaticValue
QVariant mCachedStaticValue
Contains the static, precalculated value for the node if mHasCachedValue is true.
Definition: qgsexpressionnode.h:387
QgsExpressionNode::mCompiledSimplifiedNode
std::unique_ptr< QgsExpressionNode > mCompiledSimplifiedNode
Contains a compiled node which represents a simplified version of this node as a result of compilatio...
Definition: qgsexpressionnode.h:399
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsExpressionNode::ntFunction
@ ntFunction
Definition: qgsexpressionnode.h:82
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsExpressionNode::ntBinaryOperator
@ ntBinaryOperator
Definition: qgsexpressionnode.h:80
QgsExpressionNode::ntLiteral
@ ntLiteral
Definition: qgsexpressionnode.h:83
QgsExpressionNode::NodeList::list
QList< QgsExpressionNode * > list()
Gets a list of all the nodes.
Definition: qgsexpressionnode.h:148
QgsExpressionNode
Abstract base class for all nodes that can appear in an expression.
Definition: qgsexpressionnode.h:34
QgsExpressionNode::ntUnaryOperator
@ ntUnaryOperator
Definition: qgsexpressionnode.h:79
QgsExpressionNode::NodeList::append
void append(QgsExpressionNode *node)
Takes ownership of the provided node.
Definition: qgsexpressionnode.h:126
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsExpressionNode::NamedNode
Named node.
Definition: qgsexpressionnode.h:96
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
QgsExpressionNode::NodeList::count
int count() const
Returns the number of nodes in the list.
Definition: qgsexpressionnode.h:137
QgsExpressionNode::ntIndexOperator
@ ntIndexOperator
Index operator.
Definition: qgsexpressionnode.h:86
QgsExpressionNode::NodeList::names
QStringList names() const
Returns a list of names for nodes.
Definition: qgsexpressionnode.h:161
QgsExpressionNode::NodeList
A list of expression nodes.
Definition: qgsexpressionnode.h:121
SIP_ABSTRACT
#define SIP_ABSTRACT
Definition: qgis_sip.h:208
QgsExpressionNode::ntInOperator
@ ntInOperator
Definition: qgsexpressionnode.h:81
QgsExpressionNode::cachedStaticValue
QVariant cachedStaticValue() const
Returns the node's static cached value.
Definition: qgsexpressionnode.h:334