Skip to content

Commit

Permalink
Add a method which builds an expression to test if a field is equal t…
Browse files Browse the repository at this point in the history
…o a value
  • Loading branch information
pblottiere committed Jul 5, 2017
1 parent 8e43aca commit 15546d8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
11 changes: 11 additions & 0 deletions python/core/expression/qgsexpression.sip
Expand Up @@ -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

};


Expand Down
12 changes: 12 additions & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/core/expression/qgsexpression.h
Expand Up @@ -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();

Expand Down
12 changes: 1 addition & 11 deletions src/core/qgsrelation.cpp
Expand Up @@ -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 " ) );
Expand Down
17 changes: 1 addition & 16 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -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 );
}
}
Expand Down

0 comments on commit 15546d8

Please sign in to comment.