QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
54 
58  QgsExpressionNode *operand() const { return mOperand; }
59 
60  QgsExpressionNode::NodeType nodeType() const override;
61  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
62  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
63  QString dump() const override;
64 
65  QSet<QString> referencedColumns() const override;
66  QSet<QString> referencedVariables() const override;
67  QSet<QString> referencedFunctions() const override;
68  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
69  bool needsGeometry() const override;
70  QgsExpressionNode *clone() const override SIP_FACTORY;
71 
72  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
73 
78  QString text() const;
79 
80  private:
81  UnaryOperator mOp;
82  QgsExpressionNode *mOperand = nullptr;
83 
84  static const char *UNARY_OPERATOR_TEXT[];
85 };
86 
92 {
93  public:
94 
100  {
101  // logical
104 
105  // comparison
119 
120  // math
128 
129  // strings
131  };
132 
137  : mOp( op )
138  , mOpLeft( opLeft )
139  , mOpRight( opRight )
140  {}
141  ~QgsExpressionNodeBinaryOperator() override { delete mOpLeft; delete mOpRight; }
142 
147 
152  QgsExpressionNode *opLeft() const { return mOpLeft; }
153 
158  QgsExpressionNode *opRight() const { return mOpRight; }
159 
160  QgsExpressionNode::NodeType nodeType() const override;
161  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
162  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
163  QString dump() const override;
164 
165  QSet<QString> referencedColumns() const override;
166  QSet<QString> referencedVariables() const override;
167  QSet<QString> referencedFunctions() const override;
168  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
169 
170  bool needsGeometry() const override;
171  QgsExpressionNode *clone() const override SIP_FACTORY;
172  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
173 
177  int precedence() const;
178 
182  bool leftAssociative() const;
183 
188  QString text() const;
189 
190  private:
191  bool compare( double diff );
192  qlonglong computeInt( qlonglong x, qlonglong y );
193  double computeDouble( double x, double y );
194 
200  QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
201 
202  BinaryOperator mOp;
203  QgsExpressionNode *mOpLeft = nullptr;
204  QgsExpressionNode *mOpRight = nullptr;
205 
206  static const char *BINARY_OPERATOR_TEXT[];
207 };
208 
215 {
216  public:
217 
222  : mContainer( container )
223  , mIndex( index )
224  {}
225  ~QgsExpressionNodeIndexOperator() override { delete mContainer; delete mIndex; }
226 
231  QgsExpressionNode *container() const { return mContainer; }
232 
237  QgsExpressionNode *index() const { return mIndex; }
238 
239  QgsExpressionNode::NodeType nodeType() const override;
240  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
241  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
242  QString dump() const override;
243 
244  QSet<QString> referencedColumns() const override;
245  QSet<QString> referencedVariables() const override;
246  QSet<QString> referencedFunctions() const override;
247  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
248 
249  bool needsGeometry() const override;
250  QgsExpressionNode *clone() const override SIP_FACTORY;
251  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
252 
253  private:
254 
255  QgsExpressionNode *mContainer = nullptr;
256  QgsExpressionNode *mIndex = nullptr;
257 
258 };
259 
265 {
266  public:
267 
272  : mNode( node )
273  , mList( list )
274  , mNotIn( notin )
275  {}
276  ~QgsExpressionNodeInOperator() override;
277 
281  QgsExpressionNode *node() const { return mNode; }
282 
286  bool isNotIn() const { return mNotIn; }
287 
291  QgsExpressionNode::NodeList *list() const { return mList; }
292 
293  QgsExpressionNode::NodeType nodeType() const override;
294  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
295  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
296  QString dump() const override;
297 
298  QSet<QString> referencedColumns() const override;
299  QSet<QString> referencedVariables() const override;
300  QSet<QString> referencedFunctions() const override;
301  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
302  bool needsGeometry() const override;
303  QgsExpressionNode *clone() const override SIP_FACTORY;
304  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
305 
306  private:
307  QgsExpressionNode *mNode = nullptr;
308  QgsExpressionNodeInOperator::NodeList *mList = nullptr;
309  bool mNotIn;
310 };
311 
317 {
318  public:
319 
325 
326  ~QgsExpressionNodeFunction() override;
327 
331  int fnIndex() const { return mFnIndex; }
332 
336  QgsExpressionNode::NodeList *args() const { return mArgs; }
337 
338  QgsExpressionNode::NodeType nodeType() const override;
339  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
340  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
341  QString dump() const override;
342 
343  QSet<QString> referencedColumns() const override;
344  QSet<QString> referencedVariables() const override;
345  QSet<QString> referencedFunctions() const override;
346 
347  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
348  bool needsGeometry() const override;
349  QgsExpressionNode *clone() const override SIP_FACTORY;
350  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
351 
353  static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
354 
355  private:
356  int mFnIndex;
357  NodeList *mArgs = nullptr;
358 };
359 
364 class CORE_EXPORT QgsExpressionNodeLiteral : public QgsExpressionNode
365 {
366  public:
367 
371  QgsExpressionNodeLiteral( const QVariant &value )
372  : mValue( value )
373  {}
374 
376  inline QVariant value() const { return mValue; }
377 
378  QgsExpressionNode::NodeType nodeType() const override;
379  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
380  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
381  QString dump() const override;
382 
383  QSet<QString> referencedColumns() const override;
384  QSet<QString> referencedVariables() const override;
385  QSet<QString> referencedFunctions() const override;
386 
387  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
388  bool needsGeometry() const override;
389  QgsExpressionNode *clone() const override SIP_FACTORY;
390  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
391 
392  private:
393  QVariant mValue;
394 };
395 
401 {
402  public:
403 
408  QgsExpressionNodeColumnRef( const QString &name )
409  : mName( name )
410  , mIndex( -1 )
411  {}
412 
414  QString name() const { return mName; }
415 
416  QgsExpressionNode::NodeType nodeType() const override;
417  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
418  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
419  QString dump() const override;
420 
421  QSet<QString> referencedColumns() const override;
422  QSet<QString> referencedVariables() const override;
423  QSet<QString> referencedFunctions() const override;
424  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
425 
426  bool needsGeometry() const override;
427 
428  QgsExpressionNode *clone() const override SIP_FACTORY;
429  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
430 
431  private:
432  QString mName;
433  int mIndex;
434 };
435 
441 {
442  public:
443 
448  class CORE_EXPORT WhenThen
449  {
450  public:
451 
455  WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
456  ~WhenThen();
457 
459  WhenThen( const WhenThen &rh ) = delete;
461  WhenThen &operator=( const WhenThen &rh ) = delete;
462 
467 
472  QgsExpressionNode *whenExp() const { return mWhenExp; }
473 
479  QgsExpressionNode *thenExp() const { return mThenExp; }
480 
481  private:
482 #ifdef SIP_RUN
484 #endif
485  QgsExpressionNode *mWhenExp = nullptr;
486  QgsExpressionNode *mThenExp = nullptr;
487 
489  };
490  typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
491 
496 
501  : mConditions( conditions )
502  , mElseExp( elseExp )
503  {}
504 
505  ~QgsExpressionNodeCondition() override;
506 
507  QgsExpressionNode::NodeType nodeType() const override;
508  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
509  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
510  QString dump() const override;
511 
516  WhenThenList conditions() const { return mConditions; }
517 
522  QgsExpressionNode *elseExp() const { return mElseExp; }
523 
524  QSet<QString> referencedColumns() const override;
525  QSet<QString> referencedVariables() const override;
526  QSet<QString> referencedFunctions() const override;
527 
528  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
529 
530  bool needsGeometry() const override;
531  QgsExpressionNode *clone() const override SIP_FACTORY;
532  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
533 
534  private:
535  WhenThenList mConditions;
536  QgsExpressionNode *mElseExp = nullptr;
537 };
538 
539 
540 #endif // QGSEXPRESSIONNODEIMPL_H
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsExpressionNodeCondition(const QgsExpressionNodeCondition::WhenThenList &conditions, QgsExpressionNode *elseExp=nullptr)
Create a new node with the given list of conditions and an optional elseExp expression.
virtual QString dump() const =0
Dump this node into a serialized (part) of an expression.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
QgsExpressionNodeCondition(QgsExpressionNodeCondition::WhenThenList *conditions, QgsExpressionNode *elseExp=nullptr)
Create a new node with the given list of conditions and an optional elseExp expression.
QgsExpressionNode * thenExp() const
The expression node that makes the THEN result part of the condition.
QgsExpressionNodeColumnRef(const QString &name)
Constructor for QgsExpressionNodeColumnRef, referencing the column with the specified name...
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
WhenThenList conditions() const
The list of WHEN THEN expression parts of the expression.
NodeType
Known node types.
QgsExpressionNodeInOperator(QgsExpressionNode *node, QgsExpressionNode::NodeList *list, bool notin=false)
This node tests if the result of node is in the result of list.
An expression node for CASE WHEN clauses.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
virtual QgsExpressionNode * clone() const =0
Generate a clone of this node.
QString name() const
The name of the column.
QgsExpressionNode::NodeType nodeType() const override
Gets the type of this node.
QSet< QString > referencedFunctions() const override
Returns a set of all functions which are used in this expression.
QgsExpressionNode * index() const
Returns the index node, representing an array element index or map key.
QgsExpressionNode * container() const
Returns the container node, representing an array or map value.
virtual QSet< QString > referencedColumns() const =0
Abstract virtual method which returns a list of columns required to evaluate this node...
virtual QSet< QString > referencedVariables() const =0
Returns a set of all variables which are used in this expression.
#define SIP_SKIP
Definition: qgis_sip.h:119
QList< const QgsExpressionNode * > nodes() const override
Returns a list of all nodes which are used in this expression.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
BinaryOperator
list of binary operators
An expression node for value IN or NOT IN clauses.
QSet< QString > referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsExpressionNode * clone() const override
Generate a clone of this node.
An expression node which takes it value from a feature&#39;s field.
virtual QSet< QString > referencedFunctions() const =0
Returns a set of all functions which are used in this expression.
Abstract base class for all nodes that can appear in an expression.
#define SIP_FACTORY
Definition: qgis_sip.h:69
An expression node for expression functions.
bool prepareNode(QgsExpression *parent, const QgsExpressionContext *context) override
Abstract virtual preparation method Errors are reported to the parent.
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.
QList< QgsExpressionNodeCondition::WhenThen * > WhenThenList
QgsExpressionNodeUnaryOperator(QgsExpressionNodeUnaryOperator::UnaryOperator op, QgsExpressionNode *operand)
A node unary operator is modifying the value of operand by negating it with op.
bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const override
Returns true if this node can be evaluated for a static value.
A representation of the interval between two datetime values.
Definition: qgsinterval.h:39
virtual bool needsGeometry() const =0
Abstract virtual method which returns if the geometry is required to evaluate this expression...
A list of expression nodes.
QgsExpressionNodeLiteral(const QVariant &value)
Constructor for QgsExpressionNodeLiteral, with the specified literal value.
QgsExpressionNodeUnaryOperator::UnaryOperator op() const
Returns the unary operator.
A indexing expression operator, which allows use of square brackets [] to reference map and array ite...
An expression node for literal values.
A unary node is either negative as in boolean (not) or as in numbers (minus).
A binary expression operator, which operates on two values.
QString dump() const override
Dump this node into a serialized (part) of an expression.
QgsExpressionNodeIndexOperator(QgsExpressionNode *container, QgsExpressionNode *index)
Constructor for QgsExpressionNodeIndexOperator.
virtual bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const =0
Returns true if this node can be evaluated for a static value.
virtual QList< const QgsExpressionNode * > nodes() const =0
Returns a list of all nodes which are used in this expression.
QgsExpressionNode * node() const
Returns the expression node.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator...
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
QSet< QString > referencedVariables() const override
Returns a set of all variables which are used in this expression.
QgsExpressionNode * operand() const
Returns the node the operator will operate upon.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
Represents a "WHEN... THEN..." portation of a CASE WHEN clause in an expression.
UnaryOperator
list of unary operators
bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
QgsExpressionNode * whenExp() const
The expression that makes the WHEN part of the condition.
QVariant value() const
The value of the literal.
int fnIndex() const
Returns the index of the node&#39;s function.
QgsExpressionNodeBinaryOperator(QgsExpressionNodeBinaryOperator::BinaryOperator op, QgsExpressionNode *opLeft, QgsExpressionNode *opRight)
Binary combination of the left and the right with op.