QGIS API Documentation 4.3.0-Master (c789d26cbf6)
Loading...
Searching...
No Matches
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
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
32{
33 public:
43
51
52 // clang-format off
53#ifdef SIP_RUN
54 SIP_PYOBJECT __repr__();
55 % MethodCode QString str = u"<QgsExpressionNodeUnaryOperator: %1>"_s.arg( sipCpp->text() );
56 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
57 % End
58#endif
59
60 // clang-format on
61
62
67 {
68 return mOp;
69 }
70
74 QgsExpressionNode *operand() const { return mOperand.get(); }
75
77 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
78 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
79 QString dump() const override;
80
81 QSet<QString> referencedColumns() const override;
82 QSet<QString> referencedVariables() const override;
83 QSet<QString> referencedFunctions() const override;
84 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
85 bool needsGeometry() const override;
86 QgsExpressionNode *clone() const override SIP_FACTORY;
87 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
88 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
89
94 QString text() const;
95
96 private:
98 QgsExpressionNodeUnaryOperator &operator=( const QgsExpressionNodeUnaryOperator &other ) = delete;
99
100#ifdef SIP_RUN
102#endif
103
104 UnaryOperator mOp;
105 std::unique_ptr<QgsExpressionNode> mOperand;
106
107 static const char *UNARY_OPERATOR_TEXT[];
108};
109
115{
116 public:
154
163
164#ifdef SIP_RUN
165 SIP_PYOBJECT __repr__();
166 % MethodCode QString str = u"<QgsExpressionNodeBinaryOperator: %1>"_s.arg( sipCpp->text() );
167 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
168 % End
169#endif
170
175 {
176 return mOp;
177 }
178
183 QgsExpressionNode *opLeft() const { return mOpLeft.get(); }
184
189 QgsExpressionNode *opRight() const { return mOpRight.get(); }
190
191 QgsExpressionNode::NodeType nodeType() const override;
192 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
193 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
194 QString dump() const override;
195
196 QSet<QString> referencedColumns() const override;
197 QSet<QString> referencedVariables() const override;
198 QSet<QString> referencedFunctions() const override;
199 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
200
201 bool needsGeometry() const override;
202 QgsExpressionNode *clone() const override SIP_FACTORY;
203 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
204 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
205
209 int precedence() const;
210
214 bool leftAssociative() const;
215
220 QString text() const;
221
222 private:
224 QgsExpressionNodeBinaryOperator &operator=( const QgsExpressionNodeBinaryOperator &other ) = delete;
225
226#ifdef SIP_RUN
228#endif
229 qlonglong computeInt( qlonglong x, qlonglong y );
230 double computeDouble( double x, double y );
231
237 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
238
242 static QVariant compareNonNullValues( QgsExpression *parent, const QgsExpressionContext *context, const QVariant &vL, const QVariant &vR, BinaryOperator op );
243
244 BinaryOperator mOp;
245 std::unique_ptr<QgsExpressionNode> mOpLeft;
246 std::unique_ptr<QgsExpressionNode> mOpRight;
247
248 static const char *BINARY_OPERATOR_TEXT[];
249};
250
257{
258 public:
266
271 QgsExpressionNode *container() const { return mContainer.get(); }
272
277 QgsExpressionNode *index() const { return mIndex.get(); }
278
279 QgsExpressionNode::NodeType nodeType() const override;
280 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
281 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
282 QString dump() const override;
283
284 QSet<QString> referencedColumns() const override;
285 QSet<QString> referencedVariables() const override;
286 QSet<QString> referencedFunctions() const override;
287 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
288
289 bool needsGeometry() const override;
290 QgsExpressionNode *clone() const override SIP_FACTORY;
291 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
292
293 private:
295 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
296
297#ifdef SIP_RUN
299#endif
300
301 std::unique_ptr<QgsExpressionNode> mContainer;
302 std::unique_ptr<QgsExpressionNode> mIndex;
303};
304
311{
312 public:
317 : mNode( node )
318 , mLowerBound( nodeLowerBound )
319 , mHigherBound( nodeHigherBound )
320 , mNegate( negate )
321 {}
322
324
328 QgsExpressionNode *node() const { return mNode.get(); }
329
330
331 QgsExpressionNode::NodeType nodeType() const override;
332 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
333 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
334 QString dump() const override;
335
336 QSet<QString> referencedColumns() const override;
337 QSet<QString> referencedVariables() const override;
338 QSet<QString> referencedFunctions() const override;
339 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
340 bool needsGeometry() const override;
341 QgsExpressionNode *clone() const override SIP_FACTORY;
342 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
343
347 QgsExpressionNode *lowerBound() const;
348
352 QgsExpressionNode *higherBound() const;
353
357 bool negate() const;
358
359 private:
361 QgsExpressionNodeBetweenOperator &operator=( const QgsExpressionNodeBetweenOperator &other ) = delete;
362
363#ifdef SIP_RUN
365#endif
366
367
368 std::unique_ptr<QgsExpressionNode> mNode;
369 std::unique_ptr<QgsExpressionNode> mLowerBound;
370 std::unique_ptr<QgsExpressionNode> mHigherBound;
371 bool mNegate = false;
372};
373
379{
380 public:
385 : mNode( node )
386 , mList( list )
387 , mNotIn( notin )
388 {}
390
394 QgsExpressionNode *node() const { return mNode.get(); }
395
399 bool isNotIn() const { return mNotIn; }
400
404 QgsExpressionNode::NodeList *list() const { return mList.get(); }
405
406 QgsExpressionNode::NodeType nodeType() const override;
407 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
408 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
409 QString dump() const override;
410
411 QSet<QString> referencedColumns() const override;
412 QSet<QString> referencedVariables() const override;
413 QSet<QString> referencedFunctions() const override;
414 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
415 bool needsGeometry() const override;
416 QgsExpressionNode *clone() const override SIP_FACTORY;
417 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
418 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
419
420 private:
422 QgsExpressionNodeInOperator &operator=( const QgsExpressionNodeInOperator &other ) = delete;
423
424#ifdef SIP_RUN
426#endif
427
428 std::unique_ptr<QgsExpressionNode> mNode;
429 std::unique_ptr<QgsExpressionNodeInOperator::NodeList> mList;
430 bool mNotIn;
431};
432
438{
439 public:
445
447
448#ifdef SIP_RUN
449 SIP_PYOBJECT __repr__();
450 % MethodCode 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 = u"<QgsExpressionNodeFunction: %1>"_s.arg( function );
461 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
462 % End
463#endif
464
468 int fnIndex() const
469 {
470 return mFnIndex;
471 }
472
476 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
477
478 QgsExpressionNode::NodeType nodeType() const override;
479 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
480 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
481 QString dump() const override;
482
483 QSet<QString> referencedColumns() const override;
484 QSet<QString> referencedVariables() const override;
485 QSet<QString> referencedFunctions() const override;
486
487 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
488 bool needsGeometry() const override;
489 QgsExpressionNode *clone() const override SIP_FACTORY;
490 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
491 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
492
494 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
495
496 private:
498 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
499
500#ifdef SIP_RUN
502#endif
503
504
505 int mFnIndex;
506 std::unique_ptr<NodeList> mArgs;
507};
508
514{
515 public:
520 : mValue( value )
521 {}
522
523#ifdef SIP_RUN
524 SIP_PYOBJECT __repr__();
525 % MethodCode QString str = u"<QgsExpressionNodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
526 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
527 % End
528#endif
529
531 inline QVariant value() const
532 {
533 return mValue;
534 }
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
545 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
546 bool needsGeometry() const override;
547 QgsExpressionNode *clone() const override SIP_FACTORY;
548 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
549
555 QString valueAsString() const;
556
557 private:
558 QVariant mValue;
559};
560
566{
567 public:
573 : mName( name )
574 {}
575
576#ifdef SIP_RUN
577 SIP_PYOBJECT __repr__();
578 % MethodCode QString str = u"<QgsExpressionNodeColumnRef: \"%1\">"_s.arg( sipCpp->name() );
579 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
580 % End
581#endif
582
584 QString name() const
585 {
586 return mName;
587 }
588
589 QgsExpressionNode::NodeType nodeType() const override;
590 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
591 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
592 QString dump() const override;
593
594 QSet<QString> referencedColumns() const override;
595 QSet<QString> referencedVariables() const override;
596 QSet<QString> referencedFunctions() const override;
597 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
598
599 bool needsGeometry() const override;
600
601 QgsExpressionNode *clone() const override SIP_FACTORY;
602 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
603
604 private:
605 QString mName;
606 int mIndex = -1;
607};
608
614{
615 public:
620 class CORE_EXPORT WhenThen
621 {
622 public:
627 ~WhenThen();
628
629 WhenThen( const WhenThen &rh ) = delete;
630 WhenThen &operator=( const WhenThen &rh ) = delete;
631
636
641 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
642
647
648 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
649
650 private:
651#ifdef SIP_RUN
653#endif
654 std::unique_ptr<QgsExpressionNode> mWhenExp;
655 std::unique_ptr<QgsExpressionNode> mThenExp;
656
658 };
659 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
660
665
670
672
673 QgsExpressionNode::NodeType nodeType() const override;
674 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
675 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
676 QString dump() const override;
677
682 WhenThenList conditions() const { return mConditions; }
683
688 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
689
690 QSet<QString> referencedColumns() const override;
691 QSet<QString> referencedVariables() const override;
692 QSet<QString> referencedFunctions() const override;
693
694 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
695
696 bool needsGeometry() const override;
697 QgsExpressionNode *clone() const override SIP_FACTORY;
698 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
699 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
700
701 private:
703 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
704
705#ifdef SIP_RUN
707#endif
708
709
710 WhenThenList mConditions;
711 std::unique_ptr<QgsExpressionNode> mElseExp;
712};
713
714
715#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.
bool negate() const
Returns true if the predicate is an exclusion test (NOT BETWEEN).
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.
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
WhenThen(QgsExpressionNode *whenExp, QgsExpressionNode *thenExp)
A combination of when and then.
QgsExpressionNode * thenExp() const
The expression node that makes the THEN result part of the condition.
QgsExpressionNode * whenExp() const
The expression that makes the WHEN 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
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.
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.
QgsExpressionNodeFunction(int fnIndex, QgsExpressionNode::NodeList *args)
A function node consists of an index of the function in the global function array and a list of argum...
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.
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.
QgsExpressionNode()=default
NodeType
Known node types.
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:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_FACTORY
Definition qgis_sip.h:83