QGIS API Documentation 3.43.0-Master (a93bf8b6462)
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 : matthias@opengis.ch
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
40
48
49#ifdef SIP_RUN
50 SIP_PYOBJECT __repr__();
51 % MethodCode
52 QString str = QStringLiteral( "<QgsExpressionNodeUnaryOperator: %1>" ).arg( sipCpp->text() );
53 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
54 % End
55#endif
56
61
65 QgsExpressionNode *operand() const { return mOperand.get(); }
66
68 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
69 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
70 QString dump() const override;
71
72 QSet<QString> referencedColumns() const override;
73 QSet<QString> referencedVariables() const override;
74 QSet<QString> referencedFunctions() const override;
75 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
76 bool needsGeometry() const override;
77 QgsExpressionNode *clone() const override SIP_FACTORY;
78
79 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
80
85 QString text() const;
86
87 private:
88
90 QgsExpressionNodeUnaryOperator &operator=( const QgsExpressionNodeUnaryOperator &other ) = delete;
91
92#ifdef SIP_RUN
94#endif
95
96 UnaryOperator mOp;
97 std::unique_ptr<QgsExpressionNode> mOperand;
98
99 static const char *UNARY_OPERATOR_TEXT[];
100};
101
107{
108 public:
109
147
152 : mOp( op )
153 , mOpLeft( opLeft )
154 , mOpRight( opRight )
155 {}
156
157#ifdef SIP_RUN
158 SIP_PYOBJECT __repr__();
159 % MethodCode
160 QString str = QStringLiteral( "<QgsExpressionNodeBinaryOperator: %1>" ).arg( sipCpp->text() );
161 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
162 % End
163#endif
164
169
174 QgsExpressionNode *opLeft() const { return mOpLeft.get(); }
175
180 QgsExpressionNode *opRight() const { return mOpRight.get(); }
181
182 QgsExpressionNode::NodeType nodeType() const override;
183 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
184 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
185 QString dump() const override;
186
187 QSet<QString> referencedColumns() const override;
188 QSet<QString> referencedVariables() const override;
189 QSet<QString> referencedFunctions() const override;
190 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
191
192 bool needsGeometry() const override;
193 QgsExpressionNode *clone() const override SIP_FACTORY;
194 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
195
199 int precedence() const;
200
204 bool leftAssociative() const;
205
210 QString text() const;
211
212 private:
213
215 QgsExpressionNodeBinaryOperator &operator=( const QgsExpressionNodeBinaryOperator &other ) = delete;
216
217#ifdef SIP_RUN
219#endif
220 qlonglong computeInt( qlonglong x, qlonglong y );
221 double computeDouble( double x, double y );
222
228 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
229
233 static QVariant compareNonNullValues( QgsExpression *parent, const QgsExpressionContext *context, const QVariant &vL, const QVariant &vR, BinaryOperator op );
234
235 BinaryOperator mOp;
236 std::unique_ptr<QgsExpressionNode> mOpLeft;
237 std::unique_ptr<QgsExpressionNode> mOpRight;
238
239 static const char *BINARY_OPERATOR_TEXT[];
240};
241
248{
249 public:
250
255 : mContainer( container )
256 , mIndex( index )
257 {}
258
263 QgsExpressionNode *container() const { return mContainer.get(); }
264
269 QgsExpressionNode *index() const { return mIndex.get(); }
270
271 QgsExpressionNode::NodeType nodeType() const override;
272 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
273 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
274 QString dump() const override;
275
276 QSet<QString> referencedColumns() const override;
277 QSet<QString> referencedVariables() const override;
278 QSet<QString> referencedFunctions() const override;
279 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
280
281 bool needsGeometry() const override;
282 QgsExpressionNode *clone() const override SIP_FACTORY;
283 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
284
285 private:
286
288 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
289
290#ifdef SIP_RUN
292#endif
293
294 std::unique_ptr<QgsExpressionNode> mContainer;
295 std::unique_ptr<QgsExpressionNode> mIndex;
296
297};
298
305{
306 public:
307
312 : mNode( node )
313 , mLowerBound( nodeLowerBound )
314 , mHigherBound( nodeHigherBound )
315 , mNegate( negate )
316 {}
317
319
323 QgsExpressionNode *node() const { return mNode.get(); }
324
325
326 QgsExpressionNode::NodeType nodeType() const override;
327 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
328 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
329 QString dump() const override;
330
331 QSet<QString> referencedColumns() const override;
332 QSet<QString> referencedVariables() const override;
333 QSet<QString> referencedFunctions() const override;
334 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
335 bool needsGeometry() const override;
336 QgsExpressionNode *clone() const override SIP_FACTORY;
337 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
338
342 QgsExpressionNode *lowerBound() const;
343
347 QgsExpressionNode *higherBound() const;
348
352 bool negate() const;
353
354 private:
355
357 QgsExpressionNodeBetweenOperator &operator=( const QgsExpressionNodeBetweenOperator &other ) = delete;
358
359#ifdef SIP_RUN
361#endif
362
363
364 std::unique_ptr<QgsExpressionNode> mNode;
365 std::unique_ptr<QgsExpressionNode> mLowerBound;
366 std::unique_ptr<QgsExpressionNode> mHigherBound;
367 bool mNegate = false;
368
369};
370
376{
377 public:
378
383 : mNode( node )
384 , mList( list )
385 , mNotIn( notin )
386 {}
388
392 QgsExpressionNode *node() const { return mNode.get(); }
393
397 bool isNotIn() const { return mNotIn; }
398
402 QgsExpressionNode::NodeList *list() const { return mList.get(); }
403
404 QgsExpressionNode::NodeType nodeType() const override;
405 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
406 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
407 QString dump() const override;
408
409 QSet<QString> referencedColumns() const override;
410 QSet<QString> referencedVariables() const override;
411 QSet<QString> referencedFunctions() const override;
412 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
413 bool needsGeometry() const override;
414 QgsExpressionNode *clone() const override SIP_FACTORY;
415 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
416
417 private:
418
420 QgsExpressionNodeInOperator &operator=( const QgsExpressionNodeInOperator &other ) = delete;
421
422#ifdef SIP_RUN
424#endif
425
426 std::unique_ptr<QgsExpressionNode> mNode;
427 std::unique_ptr<QgsExpressionNodeInOperator::NodeList> mList;
428 bool mNotIn;
429};
430
436{
437 public:
438
444
446
447#ifdef SIP_RUN
448 SIP_PYOBJECT __repr__();
449 % MethodCode
450 QString function;
451 if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
452 {
453 function = fd->name();
454 }
455 else
456 {
457 function = QString::number( sipCpp->fnIndex() );
458 }
459
460 QString str = QStringLiteral( "<QgsExpressionNodeFunction: %1>" ).arg( function );
461 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
462 % End
463#endif
464
468 int fnIndex() const { return mFnIndex; }
469
473 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
474
475 QgsExpressionNode::NodeType nodeType() const override;
476 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
477 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
478 QString dump() const override;
479
480 QSet<QString> referencedColumns() const override;
481 QSet<QString> referencedVariables() const override;
482 QSet<QString> referencedFunctions() const override;
483
484 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
485 bool needsGeometry() const override;
486 QgsExpressionNode *clone() const override SIP_FACTORY;
487 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
488
490 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
491
492 private:
493
495 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
496
497#ifdef SIP_RUN
499#endif
500
501
502
503 int mFnIndex;
504 std::unique_ptr<NodeList> mArgs;
505};
506
512{
513 public:
514
518 QgsExpressionNodeLiteral( const QVariant &value )
519 : mValue( value )
520 {}
521
522#ifdef SIP_RUN
523 SIP_PYOBJECT __repr__();
524 % MethodCode
525 QString str = QStringLiteral( "<QgsExpressionNodeLiteral: %1>" ).arg( sipCpp->valueAsString() );
526 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
527 % End
528#endif
529
531 inline QVariant value() const { return mValue; }
532
533 QgsExpressionNode::NodeType nodeType() const override;
534 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
535 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
536 QString dump() const override;
537
538 QSet<QString> referencedColumns() const override;
539 QSet<QString> referencedVariables() const override;
540 QSet<QString> referencedFunctions() const override;
541
542 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
543 bool needsGeometry() const override;
544 QgsExpressionNode *clone() const override SIP_FACTORY;
545 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
546
552 QString valueAsString() const;
553
554 private:
555 QVariant mValue;
556};
557
563{
564 public:
565
570 QgsExpressionNodeColumnRef( const QString &name )
571 : mName( name )
572 , mIndex( -1 )
573 {}
574
575#ifdef SIP_RUN
576 SIP_PYOBJECT __repr__();
577 % MethodCode
578 QString str = QStringLiteral( "<QgsExpressionNodeColumnRef: \"%1\">" ).arg( sipCpp->name() );
579 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
580 % End
581#endif
582
584 QString name() const { return mName; }
585
586 QgsExpressionNode::NodeType nodeType() const override;
587 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
588 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
589 QString dump() const override;
590
591 QSet<QString> referencedColumns() const override;
592 QSet<QString> referencedVariables() const override;
593 QSet<QString> referencedFunctions() const override;
594 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
595
596 bool needsGeometry() const override;
597
598 QgsExpressionNode *clone() const override SIP_FACTORY;
599 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
600
601 private:
602 QString mName;
603 int mIndex;
604};
605
611{
612 public:
613
618 class CORE_EXPORT WhenThen
619 {
620 public:
621
625 WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
626 ~WhenThen();
627
628 WhenThen( const WhenThen &rh ) = delete;
629 WhenThen &operator=( const WhenThen &rh ) = delete;
630
635
640 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
641
647 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
648
649 private:
650#ifdef SIP_RUN
652#endif
653 std::unique_ptr<QgsExpressionNode> mWhenExp;
654 std::unique_ptr<QgsExpressionNode> mThenExp;
655
657 };
658 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
659
664
669 : mConditions( conditions )
670 , mElseExp( elseExp )
671 {}
672
674
675 QgsExpressionNode::NodeType nodeType() const override;
676 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
677 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
678 QString dump() const override;
679
684 WhenThenList conditions() const { return mConditions; }
685
690 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
691
692 QSet<QString> referencedColumns() const override;
693 QSet<QString> referencedVariables() const override;
694 QSet<QString> referencedFunctions() const override;
695
696 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
697
698 bool needsGeometry() const override;
699 QgsExpressionNode *clone() const override SIP_FACTORY;
700 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
701
702 private:
703
705 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
706
707#ifdef SIP_RUN
709#endif
710
711
712 WhenThenList mConditions;
713 std::unique_ptr<QgsExpressionNode> mElseExp;
714};
715
716
717#endif // QGSEXPRESSIONNODEIMPL_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An 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.
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
QgsExpressionNodeBinaryOperator(QgsExpressionNodeBinaryOperator::BinaryOperator op, QgsExpressionNode *opLeft, QgsExpressionNode *opRight)
Binary combination of the left and the right with op.
An expression node which takes its 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 & operator=(const WhenThen &rh)=delete
WhenThen(const WhenThen &rh)=delete
QgsExpressionNode * thenExp() const
The expression node that makes the THEN result part of the condition.
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
QgsExpressionNode * elseExp() const
The ELSE expression used for the condition.
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.
QgsExpressionNodeInOperator(QgsExpressionNode *node, QgsExpressionNode::NodeList *list, bool notin=false)
This node tests if the result of node is in the result of list.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator.
An indexing expression operator, which allows use of square brackets [] to reference map and array it...
QgsExpressionNodeIndexOperator(QgsExpressionNode *container, QgsExpressionNode *index)
Constructor for QgsExpressionNodeIndexOperator.
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.
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.
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 QString dump() const =0
Dump this node into a serialized (part) of an expression.
virtual QgsExpressionNode * clone() const =0
Generate a clone of this node.
virtual QSet< QString > referencedColumns() const =0
Abstract virtual method which returns a list of columns required to evaluate this node.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
virtual QSet< QString > referencedVariables() const =0
Returns a set of all variables which are used in this expression.
NodeType
Known node types.
virtual bool needsGeometry() const =0
Abstract virtual method which returns if the geometry is required to evaluate this expression.
virtual QList< const QgsExpressionNode * > nodes() const =0
Returns a list of all nodes which are used in this expression.
virtual QSet< QString > referencedFunctions() const =0
Returns a set of all functions which are used in this expression.
Handles parsing and evaluation of expressions (formerly called "search strings").
A representation of the interval between two datetime values.
Definition qgsinterval.h:46
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:76