Skip to content
Permalink
Browse files

Add a method which builds an expression to test if a field is equal t…

…o a value
  • Loading branch information
pblottiere committed Jul 5, 2017
1 parent 8e43aca commit 15546d84cfbb401b5a31a3bcb878eddc5a0c42f5
@@ -495,6 +495,17 @@ return index of the function in Functions array
:rtype: str
%End

static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
%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
:return: the expression to evaluate field equality
.. versionadded:: 3.0
:rtype: str
%End

};


@@ -836,6 +836,18 @@ QString QgsExpression::formatPreviewString( const QVariant &value )
}
}

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

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

return expr;
}

const QgsExpressionNode *QgsExpression::rootNode() const
{
return d->mRootNode;
@@ -449,6 +449,15 @@ class CORE_EXPORT QgsExpression
*/
static QString formatPreviewString( const QVariant &value );

/** 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
* \returns the expression to evaluate field equality
* \since QGIS 3.0
*/
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );

private:
void initGeomCalculator();

@@ -170,18 +170,8 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const
{
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );
QgsField referencingField = referencingLayer()->fields().at( referencingIdx );
const QString quotedColRef = QgsExpression::quotedColumnRef( fieldPair.referencingField() ) ;

QVariant val( feature.attribute( fieldPair.referencedField() ) );

if ( val.isNull() )
{
conditions << QStringLiteral( "%1 IS NULL" ).arg( quotedColRef );
}
else
{
conditions << QStringLiteral( "%1 = %2" ).arg( quotedColRef, QgsExpression::quotedValue( val ) );
}
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencingField(), val );
}

return conditions.join( QStringLiteral( " AND " ) );
@@ -861,22 +861,7 @@ void QgsRelationReferenceWidget::filterChanged()
if ( cb->currentIndex() != 0 )
{
const QString fieldName = cb->property( "Field" ).toString();

if ( cb->currentText() == nullValue.toString() )
{
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
}
else
{
if ( mReferencedLayer->fields().field( fieldName ).type() == QVariant::String )
{
filters << QStringLiteral( "\"%1\" = '%2'" ).arg( fieldName, cb->currentText() );
}
else
{
filters << QStringLiteral( "\"%1\" = %2" ).arg( fieldName, cb->currentText() );
}
}
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
attrs << mReferencedLayer->fields().lookupField( fieldName );
}
}

0 comments on commit 15546d8

Please sign in to comment.
You can’t perform that action at this time.