QGIS API Documentation  3.25.0-Master (dec16ba68b)
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 
282 {
283  public:
284 
289  : mNode( node )
290  , mLowerBound( nodeLowerBound )
291  , mHigherBound( nodeHigherBound )
292  , mNegate( negate )
293  {}
294 
296 
300  QgsExpressionNode *node() const { return mNode; }
301 
302 
303  QgsExpressionNode::NodeType nodeType() const override;
304  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
305  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
306  QString dump() const override;
307 
308  QSet<QString> referencedColumns() const override;
309  QSet<QString> referencedVariables() const override;
310  QSet<QString> referencedFunctions() const override;
311  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
312  bool needsGeometry() const override;
313  QgsExpressionNode *clone() const override SIP_FACTORY;
314  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
315 
319  QgsExpressionNode *lowerBound() const;
320 
324  QgsExpressionNode *higherBound() const;
325 
329  bool negate() const;
330 
331  private:
332  QgsExpressionNode *mNode = nullptr;
333  QgsExpressionNode *mLowerBound = nullptr;
334  QgsExpressionNode *mHigherBound = nullptr;
335  bool mNegate = false;
336 
337 };
338 
344 {
345  public:
346 
351  : mNode( node )
352  , mList( list )
353  , mNotIn( notin )
354  {}
355  ~QgsExpressionNodeInOperator() override;
356 
360  QgsExpressionNode *node() const { return mNode; }
361 
365  bool isNotIn() const { return mNotIn; }
366 
370  QgsExpressionNode::NodeList *list() const { return mList; }
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  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
381  bool needsGeometry() const override;
382  QgsExpressionNode *clone() const override SIP_FACTORY;
383  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
384 
385  private:
386  QgsExpressionNode *mNode = nullptr;
387  QgsExpressionNodeInOperator::NodeList *mList = nullptr;
388  bool mNotIn;
389 };
390 
396 {
397  public:
398 
404 
405  ~QgsExpressionNodeFunction() override;
406 
407 #ifdef SIP_RUN
408  SIP_PYOBJECT __repr__();
409  % MethodCode
410  QString function;
411  if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
412  {
413  function = fd->name();
414  }
415  else
416  {
417  function = QString::number( sipCpp->fnIndex() );
418  }
419 
420  QString str = QStringLiteral( "<QgsExpressionNodeFunction: %1>" ).arg( function );
421  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
422  % End
423 #endif
424 
428  int fnIndex() const { return mFnIndex; }
429 
433  QgsExpressionNode::NodeList *args() const { return mArgs; }
434 
435  QgsExpressionNode::NodeType nodeType() const override;
436  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
437  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
438  QString dump() const override;
439 
440  QSet<QString> referencedColumns() const override;
441  QSet<QString> referencedVariables() const override;
442  QSet<QString> referencedFunctions() const override;
443 
444  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
445  bool needsGeometry() const override;
446  QgsExpressionNode *clone() const override SIP_FACTORY;
447  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
448 
450  static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
451 
452  private:
453  int mFnIndex;
454  NodeList *mArgs = nullptr;
455 };
456 
461 class CORE_EXPORT QgsExpressionNodeLiteral : public QgsExpressionNode
462 {
463  public:
464 
468  QgsExpressionNodeLiteral( const QVariant &value )
469  : mValue( value )
470  {}
471 
472 #ifdef SIP_RUN
473  SIP_PYOBJECT __repr__();
474  % MethodCode
475  QString str = QStringLiteral( "<QgsExpressionNodeLiteral: %1>" ).arg( sipCpp->valueAsString() );
476  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
477  % End
478 #endif
479 
481  inline QVariant value() const { return mValue; }
482 
483  QgsExpressionNode::NodeType nodeType() const override;
484  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
485  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
486  QString dump() const override;
487 
488  QSet<QString> referencedColumns() const override;
489  QSet<QString> referencedVariables() const override;
490  QSet<QString> referencedFunctions() const override;
491 
492  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
493  bool needsGeometry() const override;
494  QgsExpressionNode *clone() const override SIP_FACTORY;
495  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
496 
502  QString valueAsString() const;
503 
504  private:
505  QVariant mValue;
506 };
507 
513 {
514  public:
515 
520  QgsExpressionNodeColumnRef( const QString &name )
521  : mName( name )
522  , mIndex( -1 )
523  {}
524 
525 #ifdef SIP_RUN
526  SIP_PYOBJECT __repr__();
527  % MethodCode
528  QString str = QStringLiteral( "<QgsExpressionNodeColumnRef: \"%1\">" ).arg( sipCpp->name() );
529  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
530  % End
531 #endif
532 
534  QString name() const { return mName; }
535 
536  QgsExpressionNode::NodeType nodeType() const override;
537  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
538  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
539  QString dump() const override;
540 
541  QSet<QString> referencedColumns() const override;
542  QSet<QString> referencedVariables() const override;
543  QSet<QString> referencedFunctions() const override;
544  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
545 
546  bool needsGeometry() const override;
547 
548  QgsExpressionNode *clone() const override SIP_FACTORY;
549  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
550 
551  private:
552  QString mName;
553  int mIndex;
554 };
555 
561 {
562  public:
563 
568  class CORE_EXPORT WhenThen
569  {
570  public:
571 
575  WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
576  ~WhenThen();
577 
579  WhenThen( const WhenThen &rh ) = delete;
581  WhenThen &operator=( const WhenThen &rh ) = delete;
582 
587 
592  QgsExpressionNode *whenExp() const { return mWhenExp; }
593 
599  QgsExpressionNode *thenExp() const { return mThenExp; }
600 
601  private:
602 #ifdef SIP_RUN
604 #endif
605  QgsExpressionNode *mWhenExp = nullptr;
606  QgsExpressionNode *mThenExp = nullptr;
607 
609  };
610  typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
611 
616 
621  : mConditions( conditions )
622  , mElseExp( elseExp )
623  {}
624 
625  ~QgsExpressionNodeCondition() override;
626 
627  QgsExpressionNode::NodeType nodeType() const override;
628  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
629  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
630  QString dump() const override;
631 
636  WhenThenList conditions() const { return mConditions; }
637 
642  QgsExpressionNode *elseExp() const { return mElseExp; }
643 
644  QSet<QString> referencedColumns() const override;
645  QSet<QString> referencedVariables() const override;
646  QSet<QString> referencedFunctions() const override;
647 
648  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
649 
650  bool needsGeometry() const override;
651  QgsExpressionNode *clone() const override SIP_FACTORY;
652  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
653 
654  private:
655  WhenThenList mConditions;
656  QgsExpressionNode *mElseExp = nullptr;
657 };
658 
659 
660 #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.
SQL-like BETWEEN and NOT BETWEEN predicates.
QgsExpressionNode * node() const
Returns the expression node.
QgsExpressionNodeBetweenOperator(QgsExpressionNode *node, QgsExpressionNode *nodeLowerBound, QgsExpressionNode *nodeHigherBound, bool negate=false)
This node tests if the result of node is between the result of nodeLowerBound and nodeHigherBound nod...
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