16#ifndef QGSEXPRESSION_H
17#define QGSEXPRESSION_H
27#include <QCoreApplication>
28#include <QDomDocument>
31#include <QReadWriteLock>
32#include <QRecursiveMutex>
38using namespace Qt::StringLiterals;
50class QgsExpressionPrivate;
57 HelpArg(
const QString &arg,
const QString &desc,
bool descOnly =
false,
bool syntaxOnly =
false,
bool optional =
false,
const QString &defaultVal = QString() )
59 , mDescription( desc )
60 , mDescOnly( descOnly )
61 , mSyntaxOnly( syntaxOnly )
62 , mOptional( optional )
63 , mDefaultVal( defaultVal )
76 HelpExample(
const QString &expression,
const QString &returns,
const QString ¬e = QString() )
77 : mExpression( expression )
92 const QString &description,
93 const QList<HelpArg> &arguments = QList<HelpArg>(),
94 bool variableLenArguments =
false,
95 const QList<HelpExample> &examples = QList<HelpExample>(),
96 const QString ¬es = QString(),
97 const QStringList &tags = QStringList()
100 , mDescription( description )
101 , mArguments( arguments )
102 , mVariableLenArguments( variableLenArguments )
103 , mExamples( examples )
109 QString mDescription;
110 QList<HelpArg> mArguments;
111 bool mVariableLenArguments;
112 QList<HelpExample> mExamples;
122 Help(
const QString &name,
const QString &type,
const QString &description,
const QList<HelpVariant> &variants )
125 , mDescription( description )
126 , mVariants( variants )
131 QString mDescription;
132 QList<HelpVariant> mVariants;
135typedef QHash<QString, Help> HelpTextHash;
236 QMap< QString, int> mFunctionIndexMap;
412 QList<const QgsExpressionNode *>
nodes()
const;
422 QList<const T *> lst;
423 const QList<const QgsExpressionNode *> allNodes(
nodes() );
424 for (
const auto &node : allNodes )
426 const T *n =
dynamic_cast<const T *
>( node );
446 QSet<int> referencedAttributeIndexes(
const QgsFields &fields )
const;
449 bool needsGeometry()
const;
467 bool hasEvalError()
const;
469 QString evalErrorString()
const;
471 void setEvalErrorString(
const QString &str );
478 bool isField()
const;
492 static int expressionToLayerFieldIndex(
const QString &expression,
const QgsVectorLayer *layer );
506 static QString quoteFieldExpression(
const QString &expression,
const QgsVectorLayer *layer );
521 void setExpression(
const QString &expression );
528 QString expression()
const;
536 QString dump()
const;
617 static QSet<QString> referencedVariables(
const QString &text );
628 static double evaluateToDouble(
const QString &text,
double fallbackValue );
648 SIP_SKIP static const FunctionListSnapshot getFunctionsSnapshot();
657 Q_DECL_DEPRECATED
static QList<QgsExpressionFunction *> Functions();
664 static QStringList BuiltinFunctions();
680 static bool unregisterFunction(
const QString &name );
685 static void cleanRegisteredFunctions();
688 static bool isFunctionName(
const QString &name );
697 Q_DECL_DEPRECATED
static int functionIndex(
const QString &name );
703 static int functionCount();
710 static QString quotedColumnRef( QString name );
717 static QString quotedString( QString text );
726 static QString quotedValue(
const QVariant &value );
736 static QString quotedValue(
const QVariant &value, QMetaType::Type type );
747 Q_DECL_DEPRECATED
static QString quotedValue(
const QVariant &value, QVariant::Type type )
SIP_DEPRECATED;
753 static HelpTextHash &functionHelpTexts();
763 static QString helpText( QString name );
770 static QStringList tags(
const QString &name );
784 static bool addVariableHelpText(
const QString name,
const QString &description );
792 static QString variableHelpText(
const QString &variableName );
802 static QString
formatVariableHelp(
const QString &description,
bool showValue =
true,
const QVariant &value = QVariant() );
808 static QString group(
const QString &group );
818 static QString formatPreviewString(
const QVariant &value,
bool htmlOutput =
true,
int maximumPreviewLength = 60 );
828 static QString createFieldEqualityExpression(
const QString &fieldName,
const QVariant &value, QMetaType::Type fieldType = QMetaType::Type::UnknownType );
839 Q_DECL_DEPRECATED
static QString createFieldEqualityExpression(
const QString &fieldName,
const QVariant &value, QVariant::Type fieldType )
SIP_DEPRECATED;
851 static bool isFieldEqualityExpression(
const QString &expression, QString &field
SIP_OUT, QVariant &value
SIP_OUT );
867 static bool attemptReduceToInClause(
const QStringList &expressions, QString &result
SIP_OUT );
882 SIP_PYOBJECT __repr__();
884 QString str = u
"<QgsExpression: '%1'>"_s.arg( sipCpp->expression() );
885 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
902 QgsExpressionPrivate *d =
nullptr;
904 static HelpTextHash sFunctionHelpTexts;
907 static
void initFunctionHelp()
SIP_SKIP;
909 static
void buildFunctionHelp()
SIP_SKIP;
911 static
void initVariableHelp()
SIP_SKIP;
913 static
void buildVariableHelp()
SIP_SKIP;
917#if defined( __clang__ )
920 static std::atomic<FunctionListSnapshot> sFunctions;
922 static QMutex sFunctionWriteMutex;
924 static QStringList sBuiltinFunctionNames;
An atomic shared_ptr, for use in LLVM/clang builds (temporarily, until LLVM adds support for c++20 at...
DistanceUnit
Units of distance.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An abstract base class for defining QgsExpression functions.
Abstract base class for all nodes that can appear in an expression.
A thread-safe container of shared pointers to registered functions.
int functionIndex(const QString &name) const
Returns the index of the function in the list.
void updateFunctionNameMap()
Updates the map of function names to indices.
SharedFunctionPtr getFunction(int index) const
Returns a shared pointer for the function with the specified index.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
QgsExpression & operator=(const QgsExpression &other)
Create a copy of this expression.
QList< const T * > findNodes() const
Returns a list of all nodes of the given class which are used in this expression.
QList< const QgsExpressionNode * > nodes() const
Returns a list of all nodes which are used in this expression.
QSet< QString > referencedVariables() const
Returns a list of all variables which are used in this expression.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
std::shared_ptr< const SharedFunctionPtrList > FunctionListSnapshot
QList< QgsExpression::ParserError > parserErrors() const
Returns parser error details including location of error.
QString parserErrorString() const
Returns parser error.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
QSet< QString > referencedFunctions() const
Returns a list of the names of all functions which are used in this expression.
friend class QgsExpressionNodeFunction
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
QgsExpression(const QString &expr)
Creates a new expression based on the provided string.
std::shared_ptr< QgsExpressionFunction > SharedFunctionPtr
bool isValid() const
Checks if this expression is valid.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
Provides various utility functions for conversion between OGC (Open Geospatial Consortium) standards ...
Base class for vector data providers.
Represents a vector layer which manages a vector based dataset.
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QString formatVariableHelp(const QString &variable, const QString &description, bool showValue, const QVariant &value)
Returns a HTML formatted string for use as a variable item help.
Details about any parser errors that were found when parsing the expression.
QgsExpression::ParserError::ParserErrorType errorType
The type of parser error that was found.
int lastColumn
The last column that contained the error in the parser.
int lastLine
The last line that contained the error in the parser.
int firstColumn
The first column that contained the error in the parser.
QString errorMsg
The message for the error at this location.
@ FunctionInvalidParams
Function was called with invalid args.
@ Unknown
Unknown error type.
@ FunctionUnknown
Function was unknown.
@ FunctionNamedArgsError
Non named function arg used after named arg.
@ FunctionWrongArgs
Function was called with the wrong number of args.
int firstLine
The first line that contained the error in the parser.