QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
214 {
215  public:
216 
221  : mNode( node )
222  , mList( list )
223  , mNotIn( notin )
224  {}
225  ~QgsExpressionNodeInOperator() override;
226 
230  QgsExpressionNode *node() const { return mNode; }
231 
235  bool isNotIn() const { return mNotIn; }
236 
240  QgsExpressionNode::NodeList *list() const { return mList; }
241 
242  QgsExpressionNode::NodeType nodeType() const override;
243  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
244  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
245  QString dump() const override;
246 
247  QSet<QString> referencedColumns() const override;
248  QSet<QString> referencedVariables() const override;
249  QSet<QString> referencedFunctions() const override;
250  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
251  bool needsGeometry() const override;
252  QgsExpressionNode *clone() const override SIP_FACTORY;
253  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
254 
255  private:
256  QgsExpressionNode *mNode = nullptr;
257  QgsExpressionNodeInOperator::NodeList *mList = nullptr;
258  bool mNotIn;
259 };
260 
266 {
267  public:
268 
274 
275  ~QgsExpressionNodeFunction() override;
276 
280  int fnIndex() const { return mFnIndex; }
281 
285  QgsExpressionNode::NodeList *args() const { return mArgs; }
286 
287  QgsExpressionNode::NodeType nodeType() const override;
288  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
289  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
290  QString dump() const override;
291 
292  QSet<QString> referencedColumns() const override;
293  QSet<QString> referencedVariables() const override;
294  QSet<QString> referencedFunctions() const override;
295 
296  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
297  bool needsGeometry() const override;
298  QgsExpressionNode *clone() const override SIP_FACTORY;
299  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
300 
302  static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
303 
304  private:
305  int mFnIndex;
306  NodeList *mArgs = nullptr;
307 };
308 
313 class CORE_EXPORT QgsExpressionNodeLiteral : public QgsExpressionNode
314 {
315  public:
316 
320  QgsExpressionNodeLiteral( const QVariant &value )
321  : mValue( value )
322  {}
323 
325  inline QVariant value() const { return mValue; }
326 
327  QgsExpressionNode::NodeType nodeType() const override;
328  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
329  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
330  QString dump() const override;
331 
332  QSet<QString> referencedColumns() const override;
333  QSet<QString> referencedVariables() const override;
334  QSet<QString> referencedFunctions() const override;
335 
336  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
337  bool needsGeometry() const override;
338  QgsExpressionNode *clone() const override SIP_FACTORY;
339  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
340 
341  private:
342  QVariant mValue;
343 };
344 
350 {
351  public:
352 
357  QgsExpressionNodeColumnRef( const QString &name )
358  : mName( name )
359  , mIndex( -1 )
360  {}
361 
363  QString name() const { return mName; }
364 
365  QgsExpressionNode::NodeType nodeType() const override;
366  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
367  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
368  QString dump() const override;
369 
370  QSet<QString> referencedColumns() const override;
371  QSet<QString> referencedVariables() const override;
372  QSet<QString> referencedFunctions() const override;
373  QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
374 
375  bool needsGeometry() const override;
376 
377  QgsExpressionNode *clone() const override SIP_FACTORY;
378  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
379 
380  private:
381  QString mName;
382  int mIndex;
383 };
384 
390 {
391  public:
392 
397  class CORE_EXPORT WhenThen
398  {
399  public:
400 
404  WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
405  ~WhenThen();
406 
408  WhenThen( const WhenThen &rh ) = delete;
410  WhenThen &operator=( const WhenThen &rh ) = delete;
411 
416 
421  QgsExpressionNode *whenExp() const { return mWhenExp; }
422 
428  QgsExpressionNode *thenExp() const { return mThenExp; }
429 
430  private:
431 #ifdef SIP_RUN
433 #endif
434  QgsExpressionNode *mWhenExp = nullptr;
435  QgsExpressionNode *mThenExp = nullptr;
436 
438  };
439  typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
440 
445 
450  : mConditions( conditions )
451  , mElseExp( elseExp )
452  {}
453 
454  ~QgsExpressionNodeCondition() override;
455 
456  QgsExpressionNode::NodeType nodeType() const override;
457  QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
458  bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
459  QString dump() const override;
460 
465  WhenThenList conditions() const { return mConditions; }
466 
471  QgsExpressionNode *elseExp() const { return mElseExp; }
472 
473  QSet<QString> referencedColumns() const override;
474  QSet<QString> referencedVariables() const override;
475  QSet<QString> referencedFunctions() const override;
476 
477  QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
478 
479  bool needsGeometry() const override;
480  QgsExpressionNode *clone() const override SIP_FACTORY;
481  bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
482 
483  private:
484  WhenThenList mConditions;
485  QgsExpressionNode *mElseExp = nullptr;
486 };
487 
488 
489 #endif // QGSEXPRESSIONNODEIMPL_H
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsExpressionNode * thenExp() const
The expression node that makes the THEN result part of the condition.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
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.
QgsExpressionNodeCondition(QgsExpressionNodeCondition::WhenThenList *conditions, QgsExpressionNode *elseExp=nullptr)
Create a new node with the given list of conditions and an optional elseExp expression.
QgsExpressionNode * whenExp() const
The expression that makes the WHEN part of the condition.
QgsExpressionNodeColumnRef(const QString &name)
Constructor for QgsExpressionNodeColumnRef, referencing the column with the specified name...
int fnIndex() const
Returns the index of the node&#39;s function.
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.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator...
QString name() const
The name of the column.
virtual QgsExpressionNode * clone() const =0
Generate a clone of this node.
QgsExpressionNode * elseExp() const
The ELSE expression used for the condition.
QgsExpressionNode * node() const
Returns the expression node.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
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.
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.
WhenThenList conditions() const
The list of WHEN THEN expression parts of the expression.
QgsExpressionNode * operand() const
Returns the node the operator will operate upon.
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.
QVariant value() const
The value of the literal.
QgsExpressionNodeLiteral(const QVariant &value)
Constructor for QgsExpressionNodeLiteral, with the specified literal value.
An expression node for literal values.
A unary node is either negative as in boolean (not) or as in numbers (minus).
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
A binary expression operator, which operates on two values.
QString dump() const override
Dump this node into a serialized (part) of an expression.
virtual bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const =0
Returns true if this node can be evaluated for a static value.
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
virtual QList< const QgsExpressionNode * > nodes() const =0
Returns a list of all nodes which are used in this expression.
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.
QgsExpressionNodeUnaryOperator::UnaryOperator op() const
Returns the unary 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...
QgsExpressionNodeBinaryOperator(QgsExpressionNodeBinaryOperator::BinaryOperator op, QgsExpressionNode *opLeft, QgsExpressionNode *opRight)
Binary combination of the left and the right with op.