QGIS API Documentation 4.3.0-Master (9dce731a7a1)
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
28
34{
35 public:
45
53
54 // clang-format off
55#ifdef SIP_RUN
56 SIP_PYOBJECT __repr__();
57 % MethodCode QString str = u"<QgsExpressionNodeUnaryOperator: %1>"_s.arg( sipCpp->text() );
58 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
59 % End
60#endif
61
62 // clang-format on
63
64
69 {
70 return mOp;
71 }
72
76 QgsExpressionNode *operand() const { return mOperand.get(); }
77
79 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
80 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
81 QString dump() const override;
82
83 QSet<QString> referencedColumns() const override;
84 QSet<QString> referencedVariables() const override;
85 QSet<QString> referencedFunctions() const override;
86 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
87 bool needsGeometry() const override;
88 QgsExpressionNode *clone() const override SIP_FACTORY;
89 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
90 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
91
96 QString text() const;
97
98 private:
100 QgsExpressionNodeUnaryOperator &operator=( const QgsExpressionNodeUnaryOperator &other ) = delete;
101
102#ifdef SIP_RUN
104#endif
105
106 UnaryOperator mOp;
107 std::unique_ptr<QgsExpressionNode> mOperand;
108
109 static const char *UNARY_OPERATOR_TEXT[];
110};
111
117{
118 public:
156
165
166#ifdef SIP_RUN
167 SIP_PYOBJECT __repr__();
168 % MethodCode QString str = u"<QgsExpressionNodeBinaryOperator: %1>"_s.arg( sipCpp->text() );
169 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
170 % End
171#endif
172
177 {
178 return mOp;
179 }
180
185 QgsExpressionNode *opLeft() const { return mOpLeft.get(); }
186
191 QgsExpressionNode *opRight() const { return mOpRight.get(); }
192
193 QgsExpressionNode::NodeType nodeType() const override;
194 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
195 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
196 QString dump() const override;
197
198 QSet<QString> referencedColumns() const override;
199 QSet<QString> referencedVariables() const override;
200 QSet<QString> referencedFunctions() const override;
201 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
202
203 bool needsGeometry() const override;
204 QgsExpressionNode *clone() const override SIP_FACTORY;
205 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
206 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
207
211 int precedence() const;
212
216 bool leftAssociative() const;
217
222 QString text() const;
223
224 private:
226 QgsExpressionNodeBinaryOperator &operator=( const QgsExpressionNodeBinaryOperator &other ) = delete;
227
228#ifdef SIP_RUN
230#endif
231 qlonglong computeInt( qlonglong x, qlonglong y );
232 double computeDouble( double x, double y );
233
239 QDateTime computeDateTimeFromInterval( const QDateTime &d, QgsInterval *i );
240
244 static QVariant compareNonNullValues( QgsExpression *parent, const QgsExpressionContext *context, const QVariant &vL, const QVariant &vR, BinaryOperator op );
245
246 BinaryOperator mOp;
247 std::unique_ptr<QgsExpressionNode> mOpLeft;
248 std::unique_ptr<QgsExpressionNode> mOpRight;
249
250 static const char *BINARY_OPERATOR_TEXT[];
251};
252
259{
260 public:
268
273 QgsExpressionNode *container() const { return mContainer.get(); }
274
279 QgsExpressionNode *index() const { return mIndex.get(); }
280
281 QgsExpressionNode::NodeType nodeType() const override;
282 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
283 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
284 QString dump() const override;
285
286 QSet<QString> referencedColumns() const override;
287 QSet<QString> referencedVariables() const override;
288 QSet<QString> referencedFunctions() const override;
289 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
290
291 bool needsGeometry() const override;
292 QgsExpressionNode *clone() const override SIP_FACTORY;
293 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
294
295 private:
297 QgsExpressionNodeIndexOperator &operator=( const QgsExpressionNodeIndexOperator &other ) = delete;
298
299#ifdef SIP_RUN
301#endif
302
303 std::unique_ptr<QgsExpressionNode> mContainer;
304 std::unique_ptr<QgsExpressionNode> mIndex;
305};
306
313{
314 public:
319 : mNode( node )
320 , mLowerBound( nodeLowerBound )
321 , mHigherBound( nodeHigherBound )
322 , mNegate( negate )
323 {}
324
326
330 QgsExpressionNode *node() const { return mNode.get(); }
331
332
333 QgsExpressionNode::NodeType nodeType() const override;
334 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
335 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
336 QString dump() const override;
337
338 QSet<QString> referencedColumns() const override;
339 QSet<QString> referencedVariables() const override;
340 QSet<QString> referencedFunctions() const override;
341 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
342 bool needsGeometry() const override;
343 QgsExpressionNode *clone() const override SIP_FACTORY;
344 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
345
349 QgsExpressionNode *lowerBound() const;
350
354 QgsExpressionNode *higherBound() const;
355
359 bool negate() const;
360
361 private:
363 QgsExpressionNodeBetweenOperator &operator=( const QgsExpressionNodeBetweenOperator &other ) = delete;
364
365#ifdef SIP_RUN
367#endif
368
369
370 std::unique_ptr<QgsExpressionNode> mNode;
371 std::unique_ptr<QgsExpressionNode> mLowerBound;
372 std::unique_ptr<QgsExpressionNode> mHigherBound;
373 bool mNegate = false;
374};
375
381{
382 public:
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 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
421
422 private:
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:
447
455
457
458#ifdef SIP_RUN
459 SIP_PYOBJECT __repr__();
460 % MethodCode QString function;
461 std::shared_ptr<QgsExpressionFunction> fd = sipCpp->function();
462 if ( fd )
463 {
464 function = fd->name();
465 }
466 else
467 {
468 function = u"invalid"_s;
469 }
470
471 QString str = u"<QgsExpressionNodeFunction: %1>"_s.arg( function );
472 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
473 % End
474#endif
475
479 int fnIndex() const;
480
488 SIP_SKIP std::shared_ptr<QgsExpressionFunction> function() const;
489
493 QgsExpressionNode::NodeList *args() const { return mArgs.get(); }
494
495 QgsExpressionNode::NodeType nodeType() const override;
496 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
497 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
498 QString dump() const override;
499
500 QSet<QString> referencedColumns() const override;
501 QSet<QString> referencedVariables() const override;
502 QSet<QString> referencedFunctions() const override;
503
504 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
505 bool needsGeometry() const override;
506 QgsExpressionNode *clone() const override SIP_FACTORY;
507 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
508 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
509
511 static bool validateParams( int fnIndex, QgsExpressionNode::NodeList *args, QString &error );
512
513 private:
515 QgsExpressionNodeFunction &operator=( const QgsExpressionNodeFunction &other ) = delete;
516
517#ifdef SIP_RUN
519#endif
520
521 std::shared_ptr<QgsExpressionFunction> mFunction = nullptr;
522 std::unique_ptr<NodeList> mArgs;
523};
524
530{
531 public:
536 : mValue( value )
537 {}
538
539#ifdef SIP_RUN
540 SIP_PYOBJECT __repr__();
541 % MethodCode QString str = u"<QgsExpressionNodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
542 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
543 % End
544#endif
545
547 inline QVariant value() const
548 {
549 return mValue;
550 }
551
552 QgsExpressionNode::NodeType nodeType() const override;
553 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
554 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
555 QString dump() const override;
556
557 QSet<QString> referencedColumns() const override;
558 QSet<QString> referencedVariables() const override;
559 QSet<QString> referencedFunctions() const override;
560
561 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
562 bool needsGeometry() const override;
563 QgsExpressionNode *clone() const override SIP_FACTORY;
564 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
565
571 QString valueAsString() const;
572
573 private:
574 QVariant mValue;
575};
576
582{
583 public:
589 : mName( name )
590 {}
591
592#ifdef SIP_RUN
593 SIP_PYOBJECT __repr__();
594 % MethodCode QString str = u"<QgsExpressionNodeColumnRef: \"%1\">"_s.arg( sipCpp->name() );
595 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
596 % End
597#endif
598
600 QString name() const
601 {
602 return mName;
603 }
604
605 QgsExpressionNode::NodeType nodeType() const override;
606 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
607 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
608 QString dump() const override;
609
610 QSet<QString> referencedColumns() const override;
611 QSet<QString> referencedVariables() const override;
612 QSet<QString> referencedFunctions() const override;
613 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
614
615 bool needsGeometry() const override;
616
617 QgsExpressionNode *clone() const override SIP_FACTORY;
618 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
619
620 private:
621 QString mName;
622 int mIndex = -1;
623};
624
630{
631 public:
636 class CORE_EXPORT WhenThen
637 {
638 public:
643 ~WhenThen();
644
645 WhenThen( const WhenThen &rh ) = delete;
646 WhenThen &operator=( const WhenThen &rh ) = delete;
647
652
657 QgsExpressionNode *whenExp() const { return mWhenExp.get(); }
658
663
664 QgsExpressionNode *thenExp() const { return mThenExp.get(); }
665
666 private:
667#ifdef SIP_RUN
669#endif
670 std::unique_ptr<QgsExpressionNode> mWhenExp;
671 std::unique_ptr<QgsExpressionNode> mThenExp;
672
674 };
675 typedef QList<QgsExpressionNodeCondition::WhenThen *> WhenThenList;
676
681
686
688
689 QgsExpressionNode::NodeType nodeType() const override;
690 QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
691 bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) override;
692 QString dump() const override;
693
698 WhenThenList conditions() const { return mConditions; }
699
704 QgsExpressionNode *elseExp() const { return mElseExp.get(); }
705
706 QSet<QString> referencedColumns() const override;
707 QSet<QString> referencedVariables() const override;
708 QSet<QString> referencedFunctions() const override;
709
710 QList<const QgsExpressionNode *> nodes() const override SIP_SKIP;
711
712 bool needsGeometry() const override;
713 QgsExpressionNode *clone() const override SIP_FACTORY;
714 bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
715 QgsExpressionNode *simplifiedNode() const override SIP_FACTORY;
716
717 private:
719 QgsExpressionNodeCondition &operator=( const QgsExpressionNodeCondition &other ) = delete;
720
721#ifdef SIP_RUN
723#endif
724
725
726 WhenThenList mConditions;
727 std::unique_ptr<QgsExpressionNode> mElseExp;
728};
729
730
731#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.
std::shared_ptr< QgsExpressionFunction > function() const
Returns a reference to the node's 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:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_FACTORY
Definition qgis_sip.h:83