QGIS API Documentation 4.1.0-Master (60fea48833c)
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:
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
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
208 int precedence() const;
209
213 bool leftAssociative() const;
214
219 QString text() const;
220
221 private:
223 QgsExpressionNodeBinaryOperator &operator=( const QgsExpressionNodeBinaryOperator &other ) = delete;
224
225#ifdef SIP_RUN
227#endif
228 qlonglong computeInt( qlonglong x, qlonglong y );
229 double computeDouble( double x, double y );
230
236 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
237
241 static QVariant compareNonNullValues( QgsExpression *parent, const QgsExpressionContext *context, const QVariant &vL, const QVariant &vR, BinaryOperator op );
242
243 BinaryOperator mOp;
244 std::unique_ptr<QgsExpressionNode> mOpLeft;
245 std::unique_ptr<QgsExpressionNode> mOpRight;
246
247 static const char *BINARY_OPERATOR_TEXT[];
248};
249
256{
257 public:
265
270 QgsExpressionNode *container() const { return mContainer.get(); }
271
276 QgsExpressionNode *index() const { return mIndex.get(); }
277
278 QgsExpressionNode::NodeType nodeType() const override;
279 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
280 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
281 QString dump() const override;
282
283 QSet<QString> referencedColumns() const override;
284 QSet<QString> referencedVariables() const override;
285 QSet<QString> referencedFunctions() const override;
286 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
287
288 bool needsGeometry() const override;
289 QgsExpressionNode *clone() const override SIP_FACTORY;
290 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
291
292 private:
294 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
295
296#ifdef SIP_RUN
298#endif
299
300 std::unique_ptr<QgsExpressionNode> mContainer;
301 std::unique_ptr<QgsExpressionNode> mIndex;
302};
303
310{
311 public:
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:
360 QgsExpressionNodeBetweenOperator &operator=( const QgsExpressionNodeBetweenOperator &other ) = delete;
361
362#ifdef SIP_RUN
364#endif
365
366
367 std::unique_ptr<QgsExpressionNode> mNode;
368 std::unique_ptr<QgsExpressionNode> mLowerBound;
369 std::unique_ptr<QgsExpressionNode> mHigherBound;
370 bool mNegate = false;
371};
372
378{
379 public:
384 : mNode( node )
385 , mList( list )
386 , mNotIn( notin )
387 {}
389
393 QgsExpressionNode *node() const { return mNode.get(); }
394
398 bool isNotIn() const { return mNotIn; }
399
403 QgsExpressionNode::NodeList *list() const { return mList.get(); }
404
405 QgsExpressionNode::NodeType nodeType() const override;
406 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
407 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
408 QString dump() const override;
409
410 QSet<QString> referencedColumns() const override;
411 QSet<QString> referencedVariables() const override;
412 QSet<QString> referencedFunctions() const override;
413 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
414 bool needsGeometry() const override;
415 QgsExpressionNode *clone() const override SIP_FACTORY;
416 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
417
418 private:
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:
443
445
446#ifdef SIP_RUN
447 SIP_PYOBJECT __repr__();
448 % MethodCode QString function;
449 if ( QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[sipCpp->fnIndex()] )
450 {
451 function = fd->name();
452 }
453 else
454 {
455 function = QString::number( sipCpp->fnIndex() );
456 }
457
458 QString str = u"<QgsExpressionNodeFunction: %1>"_s.arg( function );
459 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
460 % End
461#endif
462
466 int fnIndex() const
467 {
468 return mFnIndex;
469 }
470
474 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
475
476 QgsExpressionNode::NodeType nodeType() const override;
477 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
478 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
479 QString dump() const override;
480
481 QSet<QString> referencedColumns() const override;
482 QSet<QString> referencedVariables() const override;
483 QSet<QString> referencedFunctions() const override;
484
485 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
486 bool needsGeometry() const override;
487 QgsExpressionNode *clone() const override SIP_FACTORY;
488 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
489
491 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
492
493 private:
495 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
496
497#ifdef SIP_RUN
499#endif
500
501
502 int mFnIndex;
503 std::unique_ptr<NodeList> mArgs;
504};
505
511{
512 public:
517 : mValue( value )
518 {}
519
520#ifdef SIP_RUN
521 SIP_PYOBJECT __repr__();
522 % MethodCode QString str = u"<QgsExpressionNodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
523 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
524 % End
525#endif
526
528 inline QVariant value() const
529 {
530 return mValue;
531 }
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:
570 : mName( name )
571 {}
572
573#ifdef SIP_RUN
574 SIP_PYOBJECT __repr__();
575 % MethodCode QString str = u"<QgsExpressionNodeColumnRef: \"%1\">"_s.arg( sipCpp->name() );
576 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
577 % End
578#endif
579
581 QString name() const
582 {
583 return mName;
584 }
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 = -1;
604};
605
611{
612 public:
617 class CORE_EXPORT WhenThen
618 {
619 public:
624 ~WhenThen();
625
626 WhenThen( const WhenThen &rh ) = delete;
627 WhenThen &operator=( const WhenThen &rh ) = delete;
628
633
638 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
639
644
645 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
646
647 private:
648#ifdef SIP_RUN
650#endif
651 std::unique_ptr<QgsExpressionNode> mWhenExp;
652 std::unique_ptr<QgsExpressionNode> mThenExp;
653
655 };
656 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
657
662
667
669
670 QgsExpressionNode::NodeType nodeType() const override;
671 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
672 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
673 QString dump() const override;
674
679 WhenThenList conditions() const { return mConditions; }
680
685 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
686
687 QSet<QString> referencedColumns() const override;
688 QSet<QString> referencedVariables() const override;
689 QSet<QString> referencedFunctions() const override;
690
691 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
692
693 bool needsGeometry() const override;
694 QgsExpressionNode *clone() const override SIP_FACTORY;
695 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
696
697 private:
699 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
700
701#ifdef SIP_RUN
703#endif
704
705
706 WhenThenList mConditions;
707 std::unique_ptr<QgsExpressionNode> mElseExp;
708};
709
710
711#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