Skip to content

Commit 15546d8

Browse files
committedJul 5, 2017
Add a method which builds an expression to test if a field is equal to a value
1 parent 8e43aca commit 15546d8

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed
 

‎python/core/expression/qgsexpression.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,17 @@ return index of the function in Functions array
495495
:rtype: str
496496
%End
497497

498+
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
499+
%Docstring
500+
Create an expression allowing to evaluate if a field is equal to a
501+
value. The value may be null.
502+
\param fieldName the name of the field
503+
\param value the value of the field
504+
:return: the expression to evaluate field equality
505+
.. versionadded:: 3.0
506+
:rtype: str
507+
%End
508+
498509
};
499510

500511

‎src/core/expression/qgsexpression.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,18 @@ QString QgsExpression::formatPreviewString( const QVariant &value )
836836
}
837837
}
838838

839+
QString QgsExpression::createFieldEqualityExpression( const QString &fieldName, const QVariant &value )
840+
{
841+
QString expr;
842+
843+
if ( value.isNull() )
844+
expr = QStringLiteral( "%1 IS NULL" ).arg( quotedColumnRef( fieldName ) );
845+
else
846+
expr = QStringLiteral( "%1 = %2" ).arg( quotedColumnRef( fieldName ), quotedValue( value ) );
847+
848+
return expr;
849+
}
850+
839851
const QgsExpressionNode *QgsExpression::rootNode() const
840852
{
841853
return d->mRootNode;

‎src/core/expression/qgsexpression.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ class CORE_EXPORT QgsExpression
449449
*/
450450
static QString formatPreviewString( const QVariant &value );
451451

452+
/** Create an expression allowing to evaluate if a field is equal to a
453+
* value. The value may be null.
454+
* \param fieldName the name of the field
455+
* \param value the value of the field
456+
* \returns the expression to evaluate field equality
457+
* \since QGIS 3.0
458+
*/
459+
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
460+
452461
private:
453462
void initGeomCalculator();
454463

‎src/core/qgsrelation.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,8 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const
170170
{
171171
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );
172172
QgsField referencingField = referencingLayer()->fields().at( referencingIdx );
173-
const QString quotedColRef = QgsExpression::quotedColumnRef( fieldPair.referencingField() ) ;
174-
175173
QVariant val( feature.attribute( fieldPair.referencedField() ) );
176-
177-
if ( val.isNull() )
178-
{
179-
conditions << QStringLiteral( "%1 IS NULL" ).arg( quotedColRef );
180-
}
181-
else
182-
{
183-
conditions << QStringLiteral( "%1 = %2" ).arg( quotedColRef, QgsExpression::quotedValue( val ) );
184-
}
174+
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencingField(), val );
185175
}
186176

187177
return conditions.join( QStringLiteral( " AND " ) );

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -861,22 +861,7 @@ void QgsRelationReferenceWidget::filterChanged()
861861
if ( cb->currentIndex() != 0 )
862862
{
863863
const QString fieldName = cb->property( "Field" ).toString();
864-
865-
if ( cb->currentText() == nullValue.toString() )
866-
{
867-
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
868-
}
869-
else
870-
{
871-
if ( mReferencedLayer->fields().field( fieldName ).type() == QVariant::String )
872-
{
873-
filters << QStringLiteral( "\"%1\" = '%2'" ).arg( fieldName, cb->currentText() );
874-
}
875-
else
876-
{
877-
filters << QStringLiteral( "\"%1\" = %2" ).arg( fieldName, cb->currentText() );
878-
}
879-
}
864+
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
880865
attrs << mReferencedLayer->fields().lookupField( fieldName );
881866
}
882867
}

0 commit comments

Comments
 (0)
Please sign in to comment.