QGIS API Documentation 3.43.0-Master (ac9f54ad1f7)
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
221 bool compare( double diff );
222 qlonglong computeInt( qlonglong x, qlonglong y );
223 double computeDouble( double x, double y );
224
230 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
231
232 BinaryOperator mOp;
233 std::unique_ptr<QgsExpressionNode> mOpLeft;
234 std::unique_ptr<QgsExpressionNode> mOpRight;
235
236 static const char *BINARY_OPERATOR_TEXT[];
237};
238
245{
246 public:
247
252 : mContainer( container )
253 , mIndex( index )
254 {}
255
260 QgsExpressionNode *container() const { return mContainer.get(); }
261
266 QgsExpressionNode *index() const { return mIndex.get(); }
267
268 QgsExpressionNode::NodeType nodeType() const override;
269 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
270 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
271 QString dump() const override;
272
273 QSet<QString> referencedColumns() const override;
274 QSet<QString> referencedVariables() const override;
275 QSet<QString> referencedFunctions() const override;
276 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
277
278 bool needsGeometry() const override;
279 QgsExpressionNode *clone() const override SIP_FACTORY;
280 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
281
282 private:
283
285 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
286
287#ifdef SIP_RUN
289#endif
290
291 std::unique_ptr<QgsExpressionNode> mContainer;
292 std::unique_ptr<QgsExpressionNode> mIndex;
293
294};
295
302{
303 public:
304
309 : mNode( node )
310 , mLowerBound( nodeLowerBound )
311 , mHigherBound( nodeHigherBound )
312 , mNegate( negate )
313 {}
314
316
320 QgsExpressionNode *node() const { return mNode.get(); }
321
322
323 QgsExpressionNode::NodeType nodeType() const override;
324 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
325 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
326 QString dump() const override;
327
328 QSet<QString> referencedColumns() const override;
329 QSet<QString> referencedVariables() const override;
330 QSet<QString> referencedFunctions() const override;
331 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
332 bool needsGeometry() const override;
333 QgsExpressionNode *clone() const override SIP_FACTORY;
334 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
335
339 QgsExpressionNode *lowerBound() const;
340
344 QgsExpressionNode *higherBound() const;
345
349 bool negate() const;
350
351 private:
352
354 QgsExpressionNodeBetweenOperator &operator=( const QgsExpressionNodeBetweenOperator &other ) = delete;
355
356#ifdef SIP_RUN
358#endif
359
360
361 std::unique_ptr<QgsExpressionNode> mNode;
362 std::unique_ptr<QgsExpressionNode> mLowerBound;
363 std::unique_ptr<QgsExpressionNode> mHigherBound;
364 bool mNegate = false;
365
366};
367
373{
374 public:
375
380 : mNode( node )
381 , mList( list )
382 , mNotIn( notin )
383 {}
385
389 QgsExpressionNode *node() const { return mNode.get(); }
390
394 bool isNotIn() const { return mNotIn; }
395
399 QgsExpressionNode::NodeList *list() const { return mList.get(); }
400
401 QgsExpressionNode::NodeType nodeType() const override;
402 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
403 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
404 QString dump() const override;
405
406 QSet<QString> referencedColumns() const override;
407 QSet<QString> referencedVariables() const override;
408 QSet<QString> referencedFunctions() const override;
409 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
410 bool needsGeometry() const override;
411 QgsExpressionNode *clone() const override SIP_FACTORY;
412 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
413
414 private:
415
417 QgsExpressionNodeInOperator &operator=( const QgsExpressionNodeInOperator &other ) = delete;
418
419#ifdef SIP_RUN
421#endif
422
423 std::unique_ptr<QgsExpressionNode> mNode;
424 std::unique_ptr<QgsExpressionNodeInOperator::NodeList> mList;
425 bool mNotIn;
426};
427
433{
434 public:
435
441
443
444#ifdef SIP_RUN
445 SIP_PYOBJECT __repr__();
446 % MethodCode
447 QString function;
448 if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
449 {
450 function = fd->name();
451 }
452 else
453 {
454 function = QString::number( sipCpp->fnIndex() );
455 }
456
457 QString str = QStringLiteral( "<QgsExpressionNodeFunction: %1>" ).arg( function );
458 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
459 % End
460#endif
461
465 int fnIndex() const { return mFnIndex; }
466
470 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
471
472 QgsExpressionNode::NodeType nodeType() const override;
473 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
474 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
475 QString dump() const override;
476
477 QSet<QString> referencedColumns() const override;
478 QSet<QString> referencedVariables() const override;
479 QSet<QString> referencedFunctions() const override;
480
481 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
482 bool needsGeometry() const override;
483 QgsExpressionNode *clone() const override SIP_FACTORY;
484 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
485
487 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
488
489 private:
490
492 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
493
494#ifdef SIP_RUN
496#endif
497
498
499
500 int mFnIndex;
501 std::unique_ptr<NodeList> mArgs;
502};
503
509{
510 public:
511
515 QgsExpressionNodeLiteral( const QVariant &value )
516 : mValue( value )
517 {}
518
519#ifdef SIP_RUN
520 SIP_PYOBJECT __repr__();
521 % MethodCode
522 QString str = QStringLiteral( "<QgsExpressionNodeLiteral: %1>" ).arg( sipCpp->valueAsString() );
523 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
524 % End
525#endif
526
528 inline QVariant value() const { return mValue; }
529
530 QgsExpressionNode::NodeType nodeType() const override;
531 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
532 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
533 QString dump() const override;
534
535 QSet<QString> referencedColumns() const override;
536 QSet<QString> referencedVariables() const override;
537 QSet<QString> referencedFunctions() const override;
538
539 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
540 bool needsGeometry() const override;
541 QgsExpressionNode *clone() const override SIP_FACTORY;
542 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
543
549 QString valueAsString() const;
550
551 private:
552 QVariant mValue;
553};
554
560{
561 public:
562
567 QgsExpressionNodeColumnRef( const QString &name )
568 : mName( name )
569 , mIndex( -1 )
570 {}
571
572#ifdef SIP_RUN
573 SIP_PYOBJECT __repr__();
574 % MethodCode
575 QString str = QStringLiteral( "<QgsExpressionNodeColumnRef: \"%1\">" ).arg( sipCpp->name() );
576 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
577 % End
578#endif
579
581 QString name() const { return mName; }
582
583 QgsExpressionNode::NodeType nodeType() const override;
584 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
585 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
586 QString dump() const override;
587
588 QSet<QString> referencedColumns() const override;
589 QSet<QString> referencedVariables() const override;
590 QSet<QString> referencedFunctions() const override;
591 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
592
593 bool needsGeometry() const override;
594
595 QgsExpressionNode *clone() const override SIP_FACTORY;
596 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
597
598 private:
599 QString mName;
600 int mIndex;
601};
602
608{
609 public:
610
615 class CORE_EXPORT WhenThen
616 {
617 public:
618
622 WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp );
623 ~WhenThen();
624
625 WhenThen( const WhenThen &rh ) = delete;
626 WhenThen &operator=( const WhenThen &rh ) = delete;
627
632
637 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
638
644 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
645
646 private:
647#ifdef SIP_RUN
649#endif
650 std::unique_ptr<QgsExpressionNode> mWhenExp;
651 std::unique_ptr<QgsExpressionNode> mThenExp;
652
654 };
655 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
656
661
666 : mConditions( conditions )
667 , mElseExp( elseExp )
668 {}
669
671
672 QgsExpressionNode::NodeType nodeType() const override;
673 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
674 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
675 QString dump() const override;
676
681 WhenThenList conditions() const { return mConditions; }
682
687 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
688
689 QSet<QString> referencedColumns() const override;
690 QSet<QString> referencedVariables() const override;
691 QSet<QString> referencedFunctions() const override;
692
693 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
694
695 bool needsGeometry() const override;
696 QgsExpressionNode *clone() const override SIP_FACTORY;
697 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
698
699 private:
700
702 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
703
704#ifdef SIP_RUN
706#endif
707
708
709 WhenThenList mConditions;
710 std::unique_ptr<QgsExpressionNode> mElseExp;
711};
712
713
714#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