QGIS API Documentation 3.99.0-Master (d270888f95f)
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
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:
34
44
52
53#ifdef SIP_RUN
54 SIP_PYOBJECT __repr__();
55 % MethodCode
56 QString str = u"<QgsExpressionNodeUnaryOperator: %1>"_s.arg( sipCpp->text() );
57 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
58 % End
59#endif
60
65
69 QgsExpressionNode *operand() const { return mOperand.get(); }
70
72 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
73 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
74 QString dump() const override;
75
76 QSet<QString> referencedColumns() const override;
77 QSet<QString> referencedVariables() const override;
78 QSet<QString> referencedFunctions() const override;
79 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
80 bool needsGeometry() const override;
81 QgsExpressionNode *clone() const override SIP_FACTORY;
82
83 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
84
89 QString text() const;
90
91 private:
92
94 QgsExpressionNodeUnaryOperator &operator=( const QgsExpressionNodeUnaryOperator &other ) = delete;
95
96#ifdef SIP_RUN
98#endif
99
100 UnaryOperator mOp;
101 std::unique_ptr<QgsExpressionNode> mOperand;
102
103 static const char *UNARY_OPERATOR_TEXT[];
104};
105
111{
112 public:
113
151
160
161#ifdef SIP_RUN
162 SIP_PYOBJECT __repr__();
163 % MethodCode
164 QString str = u"<QgsExpressionNodeBinaryOperator: %1>"_s.arg( sipCpp->text() );
165 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
166 % End
167#endif
168
173
178 QgsExpressionNode *opLeft() const { return mOpLeft.get(); }
179
184 QgsExpressionNode *opRight() const { return mOpRight.get(); }
185
186 QgsExpressionNode::NodeType nodeType() const override;
187 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
188 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
189 QString dump() const override;
190
191 QSet<QString> referencedColumns() const override;
192 QSet<QString> referencedVariables() const override;
193 QSet<QString> referencedFunctions() const override;
194 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
195
196 bool needsGeometry() const override;
197 QgsExpressionNode *clone() const override SIP_FACTORY;
198 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
199
203 int precedence() const;
204
208 bool leftAssociative() const;
209
214 QString text() const;
215
216 private:
217
219 QgsExpressionNodeBinaryOperator &operator=( const QgsExpressionNodeBinaryOperator &other ) = delete;
220
221#ifdef SIP_RUN
223#endif
224 qlonglong computeInt( qlonglong x, qlonglong y );
225 double computeDouble( double x, double y );
226
232 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
233
237 static QVariant compareNonNullValues( QgsExpression *parent, const QgsExpressionContext *context, const QVariant &vL, const QVariant &vR, BinaryOperator op );
238
239 BinaryOperator mOp;
240 std::unique_ptr<QgsExpressionNode> mOpLeft;
241 std::unique_ptr<QgsExpressionNode> mOpRight;
242
243 static const char *BINARY_OPERATOR_TEXT[];
244};
245
252{
253 public:
254
262
267 QgsExpressionNode *container() const { return mContainer.get(); }
268
273 QgsExpressionNode *index() const { return mIndex.get(); }
274
275 QgsExpressionNode::NodeType nodeType() const override;
276 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
277 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
278 QString dump() const override;
279
280 QSet<QString> referencedColumns() const override;
281 QSet<QString> referencedVariables() const override;
282 QSet<QString> referencedFunctions() const override;
283 QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP
284
285 bool needsGeometry() const override;
286 QgsExpressionNode *clone() const override SIP_FACTORY;
287 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
288
289 private:
290
292 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
293
294#ifdef SIP_RUN
296#endif
297
298 std::unique_ptr<QgsExpressionNode> mContainer;
299 std::unique_ptr<QgsExpressionNode> mIndex;
300
301};
302
309{
310 public:
311
316 : mNode( node )
317 , mLowerBound( nodeLowerBound )
318 , mHigherBound( nodeHigherBound )
319 , mNegate( negate )
320 {}
321
323
327 QgsExpressionNode *node() const { return mNode.get(); }
328
329
330 QgsExpressionNode::NodeType nodeType() const override;
331 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
332 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
333 QString dump() const override;
334
335 QSet<QString> referencedColumns() const override;
336 QSet<QString> referencedVariables() const override;
337 QSet<QString> referencedFunctions() const override;
338 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
339 bool needsGeometry() const override;
340 QgsExpressionNode *clone() const override SIP_FACTORY;
341 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
342
346 QgsExpressionNode *lowerBound() const;
347
351 QgsExpressionNode *higherBound() const;
352
356 bool negate() const;
357
358 private:
359
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};
374
380{
381 public:
382
387 : mNode( node )
388 , mList( list )
389 , mNotIn( notin )
390 {}
392
396 QgsExpressionNode *node() const { return mNode.get(); }
397
401 bool isNotIn() const { return mNotIn; }
402
406 QgsExpressionNode::NodeList *list() const { return mList.get(); }
407
408 QgsExpressionNode::NodeType nodeType() const override;
409 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
410 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
411 QString dump() const override;
412
413 QSet<QString> referencedColumns() const override;
414 QSet<QString> referencedVariables() const override;
415 QSet<QString> referencedFunctions() const override;
416 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
417 bool needsGeometry() const override;
418 QgsExpressionNode *clone() const override SIP_FACTORY;
419 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
420
421 private:
422
424 QgsExpressionNodeInOperator &operator=( const QgsExpressionNodeInOperator &other ) = delete;
425
426#ifdef SIP_RUN
428#endif
429
430 std::unique_ptr<QgsExpressionNode> mNode;
431 std::unique_ptr<QgsExpressionNodeInOperator::NodeList> mList;
432 bool mNotIn;
433};
434
440{
441 public:
442
448
450
451#ifdef SIP_RUN
452 SIP_PYOBJECT __repr__();
453 % MethodCode
454 QString function;
455 if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
456 {
457 function = fd->name();
458 }
459 else
460 {
461 function = QString::number( sipCpp->fnIndex() );
462 }
463
464 QString str = u"<QgsExpressionNodeFunction: %1>"_s.arg( function );
465 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
466 % End
467#endif
468
472 int fnIndex() const { return mFnIndex; }
473
477 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
478
479 QgsExpressionNode::NodeType nodeType() const override;
480 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
481 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
482 QString dump() const override;
483
484 QSet<QString> referencedColumns() const override;
485 QSet<QString> referencedVariables() const override;
486 QSet<QString> referencedFunctions() const override;
487
488 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
489 bool needsGeometry() const override;
490 QgsExpressionNode *clone() const override SIP_FACTORY;
491 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
492
494 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
495
496 private:
497
499 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
500
501#ifdef SIP_RUN
503#endif
504
505
506
507 int mFnIndex;
508 std::unique_ptr<NodeList> mArgs;
509};
510
516{
517 public:
518
523 : mValue( value )
524 {}
525
526#ifdef SIP_RUN
527 SIP_PYOBJECT __repr__();
528 % MethodCode
529 QString str = u"<QgsExpressionNodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
530 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
531 % End
532#endif
533
535 inline QVariant value() const { return mValue; }
536
537 QgsExpressionNode::NodeType nodeType() const override;
538 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
539 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
540 QString dump() const override;
541
542 QSet<QString> referencedColumns() const override;
543 QSet<QString> referencedVariables() const override;
544 QSet<QString> referencedFunctions() const override;
545
546 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
547 bool needsGeometry() const override;
548 QgsExpressionNode *clone() const override SIP_FACTORY;
549 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
550
556 QString valueAsString() const;
557
558 private:
559 QVariant mValue;
560};
561
567{
568 public:
569
575 : mName( name )
576 {}
577
578#ifdef SIP_RUN
579 SIP_PYOBJECT __repr__();
580 % MethodCode
581 QString str = u"<QgsExpressionNodeColumnRef: \"%1\">"_s.arg( sipCpp->name() );
582 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
583 % End
584#endif
585
587 QString name() const { return mName; }
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:
616
621 class CORE_EXPORT WhenThen
622 {
623 public:
624
629 ~WhenThen();
630
631 WhenThen( const WhenThen &rh ) = delete;
632 WhenThen &operator=( const WhenThen &rh ) = delete;
633
638
643 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
644
649
650 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
651
652 private:
653#ifdef SIP_RUN
655#endif
656 std::unique_ptr<QgsExpressionNode> mWhenExp;
657 std::unique_ptr<QgsExpressionNode> mThenExp;
658
660 };
661 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
662
667
675
677
678 QgsExpressionNode::NodeType nodeType() const override;
679 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
680 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
681 QString dump() const override;
682
687 WhenThenList conditions() const { return mConditions; }
688
693 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
694
695 QSet<QString> referencedColumns() const override;
696 QSet<QString> referencedVariables() const override;
697 QSet<QString> referencedFunctions() const override;
698
699 QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
700
701 bool needsGeometry() const override;
702 QgsExpressionNode *clone() const override SIP_FACTORY;
703 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
704
705 private:
706
708 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
709
710#ifdef SIP_RUN
712#endif
713
714
715 WhenThenList mConditions;
716 std::unique_ptr<QgsExpressionNode> mElseExp;
717};
718
719
720#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 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:50
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:84