QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsexpressionnodeimpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionnodeimpl.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 QGSEXPRESSIONNODEIMPL_H
18 #define QGSEXPRESSIONNODEIMPL_H
19 
20 #include "qgsexpressionnode.h"
21 #include "qgsinterval.h"
22 
28 {
29  public:
30 
36  {
39  };
40 
45  : mOp( op )
46  , mOperand( operand )
47  {}
48  ~QgsExpressionNodeUnaryOperator() override { delete mOperand; }
49 
50 #ifdef SIP_RUN
51  SIP_PYOBJECT __repr__();
52  % MethodCode
53  QString str = QStringLiteral( "<QgsExpressionNodeUnaryOperator: %1>" ).arg( sipCpp->text() );
54  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
55  % End
56 #endif
57 
62 
66  QgsExpressionNode *operand() const { return mOperand; }
67 
68  QgsExpressionNode::NodeType nodeType() const override;
69  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
70  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
71  QString dump() const override;
72 
73  QSet<QString> referencedColumns() const override;
74  QSet<QString> referencedVariables() const override;
75  QSet<QString> referencedFunctions() const override;
76  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
77  bool needsGeometry() const override;
78  QgsExpressionNode *clone() const override SIP_FACTORY;
79 
80  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
81 
86  QString text() const;
87 
88  private:
89  UnaryOperator mOp;
90  QgsExpressionNode *mOperand = nullptr;
91 
92  static const char *UNARY_OPERATOR_TEXT[];
93 };
94 
100 {
101  public:
102 
108  {
109  // logical
112 
113  // comparison
127 
128  // math
136 
137  // strings
139  };
140 
145  : mOp( op )
146  , mOpLeft( opLeft )
147  , mOpRight( opRight )
148  {}
149  ~QgsExpressionNodeBinaryOperator() override { delete mOpLeft; delete mOpRight; }
150 
151 #ifdef SIP_RUN
152  SIP_PYOBJECT __repr__();
153  % MethodCode
154  QString str = QStringLiteral( "<QgsExpressionNodeBinaryOperator: %1>" ).arg( sipCpp->text() );
155  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
156  % End
157 #endif
158 
163 
168  QgsExpressionNode *opLeft() const { return mOpLeft; }
169 
174  QgsExpressionNode *opRight() const { return mOpRight; }
175 
176  QgsExpressionNode::NodeType nodeType() const override;
177  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
178  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
179  QString dump() const override;
180 
181  QSet<QString> referencedColumns() const override;
182  QSet<QString> referencedVariables() const override;
183  QSet<QString> referencedFunctions() const override;
184  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
185 
186  bool needsGeometry() const override;
187  QgsExpressionNode *clone() const override SIP_FACTORY;
188  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
189 
193  int precedence() const;
194 
198  bool leftAssociative() const;
199 
204  QString text() const;
205 
206  private:
207  bool compare( double diff );
208  qlonglong computeInt( qlonglong x, qlonglong y );
209  double computeDouble( double x, double y );
210 
216  QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
217 
218  BinaryOperator mOp;
219  QgsExpressionNode *mOpLeft = nullptr;
220  QgsExpressionNode *mOpRight = nullptr;
221 
222  static const char *BINARY_OPERATOR_TEXT[];
223 };
224 
231 {
232  public:
233 
238  : mContainer( container )
239  , mIndex( index )
240  {}
241  ~QgsExpressionNodeIndexOperator() override { delete mContainer; delete mIndex; }
242 
247  QgsExpressionNode *container() const { return mContainer; }
248 
253  QgsExpressionNode *index() const { return mIndex; }
254 
255  QgsExpressionNode::NodeType nodeType() const override;
256  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
257  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
258  QString dump() const override;
259 
260  QSet<QString> referencedColumns() const override;
261  QSet<QString> referencedVariables() const override;
262  QSet<QString> referencedFunctions() const override;
263  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
264 
265  bool needsGeometry() const override;
266  QgsExpressionNode *clone() const override SIP_FACTORY;
267  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
268 
269  private:
270 
271  QgsExpressionNode *mContainer = nullptr;
272  QgsExpressionNode *mIndex = nullptr;
273 
274 };
275 
281 {
282  public:
283 
288  : mNode( node )
289  , mList( list )
290  , mNotIn( notin )
291  {}
292  ~QgsExpressionNodeInOperator() override;
293 
297  QgsExpressionNode *node() const { return mNode; }
298 
302  bool isNotIn() const { return mNotIn; }
303 
307  QgsExpressionNode::NodeList *list() const { return mList; }
308 
309  QgsExpressionNode::NodeType nodeType() const override;
310  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
311  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
312  QString dump() const override;
313 
314  QSet<QString> referencedColumns() const override;
315  QSet<QString> referencedVariables() const override;
316  QSet<QString> referencedFunctions() const override;
317  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
318  bool needsGeometry() const override;
319  QgsExpressionNode *clone() const override SIP_FACTORY;
320  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
321 
322  private:
323  QgsExpressionNode *mNode = nullptr;
324  QgsExpressionNodeInOperator::NodeList *mList = nullptr;
325  bool mNotIn;
326 };
327 
333 {
334  public:
335 
341 
342  ~QgsExpressionNodeFunction() override;
343 
344 #ifdef SIP_RUN
345  SIP_PYOBJECT __repr__();
346  % MethodCode
347  QString function;
348  if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
349  {
350  function = fd->name();
351  }
352  else
353  {
354  function = QString::number( sipCpp->fnIndex() );
355  }
356 
357  QString str = QStringLiteral( "<QgsExpressionNodeFunction: %1>" ).arg( function );
358  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
359  % End
360 #endif
361 
365  int fnIndex() const { return mFnIndex; }
366 
370  QgsExpressionNode::NodeList *args() const { return mArgs; }
371 
372  QgsExpressionNode::NodeType nodeType() const override;
373  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
374  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
375  QString dump() const override;
376 
377  QSet<QString> referencedColumns() const override;
378  QSet<QString> referencedVariables() const override;
379  QSet<QString> referencedFunctions() const override;
380 
381  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
382  bool needsGeometry() const override;
383  QgsExpressionNode *clone() const override SIP_FACTORY;
384  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
385 
387  static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
388 
389  private:
390  int mFnIndex;
391  NodeList *mArgs = nullptr;
392 };
393 
398 class CORE_EXPORT QgsExpressionNodeLiteral : public QgsExpressionNode
399 {
400  public:
401 
405  QgsExpressionNodeLiteral( const QVariant &value )
406  : mValue( value )
407  {}
408 
409 #ifdef SIP_RUN
410  SIP_PYOBJECT __repr__();
411  % MethodCode
412  QString str = QStringLiteral( "<QgsExpressionNodeLiteral: %1>" ).arg( sipCpp->valueAsString() );
413  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
414  % End
415 #endif
416 
418  inline QVariant value() const { return mValue; }
419 
420  QgsExpressionNode::NodeType nodeType() const override;
421  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
422  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
423  QString dump() const override;
424 
425  QSet<QString> referencedColumns() const override;
426  QSet<QString> referencedVariables() const override;
427  QSet<QString> referencedFunctions() const override;
428 
429  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
430  bool needsGeometry() const override;
431  QgsExpressionNode *clone() const override SIP_FACTORY;
432  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
433 
439  QString valueAsString() const;
440 
441  private:
442  QVariant mValue;
443 };
444 
450 {
451  public:
452 
457  QgsExpressionNodeColumnRef( const QString &name )
458  : mName( name )
459  , mIndex( -1 )
460  {}
461 
462 #ifdef SIP_RUN
463  SIP_PYOBJECT __repr__();
464  % MethodCode
465  QString str = QStringLiteral( "<QgsExpressionNodeColumnRef: \"%1\">" ).arg( sipCpp->name() );
466  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
467  % End
468 #endif
469 
471  QString name() const { return mName; }
472 
473  QgsExpressionNode::NodeType nodeType() const override;
474  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
475  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
476  QString dump() const override;
477 
478  QSet<QString> referencedColumns() const override;
479  QSet<QString> referencedVariables() const override;
480  QSet<QString> referencedFunctions() const override;
481  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
482 
483  bool needsGeometry() const override;
484 
485  QgsExpressionNode *clone() const override SIP_FACTORY;
486  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
487 
488  private:
489  QString mName;
490  int mIndex;
491 };
492 
498 {
499  public:
500 
505  class CORE_EXPORT WhenThen
506  {
507  public:
508 
512  WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
513  ~WhenThen();
514 
516  WhenThen( const WhenThen &rh ) = delete;
518  WhenThen &operator=( const WhenThen &rh ) = delete;
519 
524 
529  QgsExpressionNode *whenExp() const { return mWhenExp; }
530 
536  QgsExpressionNode *thenExp() const { return mThenExp; }
537 
538  private:
539 #ifdef SIP_RUN
541 #endif
542  QgsExpressionNode *mWhenExp = nullptr;
543  QgsExpressionNode *mThenExp = nullptr;
544 
546  };
547  typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
548 
553 
558  : mConditions( conditions )
559  , mElseExp( elseExp )
560  {}
561 
562  ~QgsExpressionNodeCondition() override;
563 
564  QgsExpressionNode::NodeType nodeType() const override;
565  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
566  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
567  QString dump() const override;
568 
573  WhenThenList conditions() const { return mConditions; }
574 
579  QgsExpressionNode *elseExp() const { return mElseExp; }
580 
581  QSet<QString> referencedColumns() const override;
582  QSet<QString> referencedVariables() const override;
583  QSet<QString> referencedFunctions() const override;
584 
585  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
586 
587  bool needsGeometry() const override;
588  QgsExpressionNode *clone() const override SIP_FACTORY;
589  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
590 
591  private:
592  WhenThenList mConditions;
593  QgsExpressionNode *mElseExp = nullptr;
594 };
595 
596 
597 #endif // QGSEXPRESSIONNODEIMPL_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A abstract base class for defining QgsExpression functions.
A binary expression operator, which operates on two values.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
QgsExpressionNodeBinaryOperator(QgsExpressionNodeBinaryOperator::BinaryOperator op, QgsExpressionNode *opLeft, QgsExpressionNode *opRight)
Binary combination of the left and the right with op.
BinaryOperator
list of binary operators
An expression node which takes it value from a feature's field.
QgsExpressionNodeColumnRef(const QString &name)
Constructor for QgsExpressionNodeColumnRef, referencing the column with the specified name.
QString name() const
The name of the column.
Represents a "WHEN... THEN..." portation of a CASE WHEN clause in an expression.
WhenThen(const WhenThen &rh)=delete
WhenThen nodes cannot be copied.
QgsExpressionNode * thenExp() const
The expression node that makes the THEN result part of the condition.
WhenThen & operator=(const WhenThen &rh)=delete
WhenThen nodes cannot be copied.
An expression node for CASE WHEN clauses.
QgsExpressionNodeCondition(const QgsExpressionNodeCondition::WhenThenList &conditions, QgsExpressionNode *elseExp=nullptr)
Create a new node with the given list of conditions and an optional elseExp expression.
QList< QgsExpressionNodeCondition::WhenThen * > WhenThenList
QgsExpressionNodeCondition(QgsExpressionNodeCondition::WhenThenList *conditions, QgsExpressionNode *elseExp=nullptr)
Create a new node with the given list of conditions and an optional elseExp expression.
QgsExpressionNode * elseExp() const
The ELSE expression used for the condition.
QVariant evalNode(QgsExpression *parent, const QgsExpressionContext *context) override
Abstract virtual eval method Errors are reported to the parent.
bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const override
Returns true if this node can be evaluated for a static value.
bool prepareNode(QgsExpression *parent, const QgsExpressionContext *context) override
Abstract virtual preparation method Errors are reported to the parent.
WhenThenList conditions() const
The list of WHEN THEN expression parts of the expression.
An expression node for expression functions.
int fnIndex() const
Returns the index of the node's function.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
An expression node for value IN or NOT IN clauses.
QgsExpressionNode * node() const
Returns the expression node.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
QgsExpressionNodeInOperator(QgsExpressionNode *node, QgsExpressionNode::NodeList *list, bool notin=false)
This node tests if the result of node is in the result of list.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator.
A indexing expression operator, which allows use of square brackets [] to reference map and array ite...
QgsExpressionNodeIndexOperator(QgsExpressionNode *container, QgsExpressionNode *index)
Constructor for QgsExpressionNodeIndexOperator.
QgsExpressionNode * container() const
Returns the container node, representing an array or map value.
QgsExpressionNode * index() const
Returns the index node, representing an array element index or map key.
An expression node for literal values.
QVariant value() const
The value of the literal.
QgsExpressionNodeLiteral(const QVariant &value)
Constructor for QgsExpressionNodeLiteral, with the specified literal value.
A unary node is either negative as in boolean (not) or as in numbers (minus).
QgsExpressionNodeUnaryOperator::UnaryOperator op() const
Returns the unary operator.
QgsExpressionNodeUnaryOperator(QgsExpressionNodeUnaryOperator::UnaryOperator op, QgsExpressionNode *operand)
A node unary operator is modifying the value of operand by negating it with op.
UnaryOperator
list of unary operators
QgsExpressionNode * operand() const
Returns the node the operator will operate upon.
A list of expression nodes.
Abstract base class for all nodes that can appear in an expression.
virtual QList< const QgsExpressionNode * > nodes() const =0
Returns a list of all nodes which are used in this expression.
virtual QString dump() const =0
Dump this node into a serialized (part) of an expression.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
virtual QSet< QString > referencedColumns() const =0
Abstract virtual method which returns a list of columns required to evaluate this node.
NodeType
Known node types.
virtual bool needsGeometry() const =0
Abstract virtual method which returns if the geometry is required to evaluate this expression.
virtual QSet< QString > referencedFunctions() const =0
Returns a set of all functions which are used in this expression.
virtual QSet< QString > referencedVariables() const =0
Returns a set of all variables which are used in this expression.
virtual QgsExpressionNode * clone() const =0
Generate a clone of this node.
Class for parsing and evaluation of expressions (formerly called "search strings").
A representation of the interval between two datetime values.
Definition: qgsinterval.h:42
#define str(x)
Definition: qgis.cpp:37
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76