Skip to content

Commit

Permalink
add optional type parameter to QgsExpression::createFieldEqualityExpr…
Browse files Browse the repository at this point in the history
…ession
  • Loading branch information
3nids committed Nov 8, 2021
1 parent 00554ef commit 4b3c50d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion python/core/auto_generated/expression/qgsexpression.sip.in
Expand Up @@ -673,13 +673,14 @@ length, and presents text representations of non numeric/text types (e.g., geome
.. versionadded:: 2.14
%End

static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType = QVariant::Type::Invalid );
%Docstring
Create an expression allowing to evaluate if a field is equal to a
value. The value may be null.

:param fieldName: the name of the field
:param value: the value of the field
:param fieldType: the type of the field on the left side used to quote the value. If not given, the value type is used instead

:return: the expression to evaluate field equality

Expand Down
6 changes: 4 additions & 2 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -1112,14 +1112,16 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
}
}

QString QgsExpression::createFieldEqualityExpression( const QString &fieldName, const QVariant &value )
QString QgsExpression::createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType )
{
QString expr;

if ( value.isNull() )
expr = QStringLiteral( "%1 IS NULL" ).arg( quotedColumnRef( fieldName ) );
else
else if ( fieldType == QVariant::Type::Invalid )
expr = QStringLiteral( "%1 = %2" ).arg( quotedColumnRef( fieldName ), quotedValue( value ) );
else
expr = QStringLiteral( "%1 = %2" ).arg( quotedColumnRef( fieldName ), quotedValue( value, fieldType ) );

return expr;
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/expression/qgsexpression.h
Expand Up @@ -659,10 +659,11 @@ class CORE_EXPORT QgsExpression
* value. The value may be null.
* \param fieldName the name of the field
* \param value the value of the field
* \param fieldType the type of the field on the left side used to quote the value. If not given, the value type is used instead
* \returns the expression to evaluate field equality
* \since QGIS 3.0
*/
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType = QVariant::Type::Invalid );

/**
* Returns TRUE if the given \a expression is a simple "field=value" type expression.
Expand Down

0 comments on commit 4b3c50d

Please sign in to comment.