File tree Expand file tree Collapse file tree 5 files changed +34
-27
lines changed Expand file tree Collapse file tree 5 files changed +34
-27
lines changed Original file line number Diff line number Diff line change @@ -495,6 +495,17 @@ return index of the function in Functions array
495
495
:rtype: str
496
496
%End
497
497
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
+
498
509
};
499
510
500
511
Original file line number Diff line number Diff line change @@ -836,6 +836,18 @@ QString QgsExpression::formatPreviewString( const QVariant &value )
836
836
}
837
837
}
838
838
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
+
839
851
const QgsExpressionNode *QgsExpression::rootNode () const
840
852
{
841
853
return d->mRootNode ;
Original file line number Diff line number Diff line change @@ -449,6 +449,15 @@ class CORE_EXPORT QgsExpression
449
449
*/
450
450
static QString formatPreviewString ( const QVariant &value );
451
451
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
+
452
461
private:
453
462
void initGeomCalculator ();
454
463
Original file line number Diff line number Diff line change @@ -170,18 +170,8 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const
170
170
{
171
171
int referencingIdx = referencingLayer ()->fields ().indexFromName ( fieldPair.referencingField () );
172
172
QgsField referencingField = referencingLayer ()->fields ().at ( referencingIdx );
173
- const QString quotedColRef = QgsExpression::quotedColumnRef ( fieldPair.referencingField () ) ;
174
-
175
173
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 );
185
175
}
186
176
187
177
return conditions.join ( QStringLiteral ( " AND " ) );
Original file line number Diff line number Diff line change @@ -861,22 +861,7 @@ void QgsRelationReferenceWidget::filterChanged()
861
861
if ( cb->currentIndex () != 0 )
862
862
{
863
863
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 () );
880
865
attrs << mReferencedLayer ->fields ().lookupField ( fieldName );
881
866
}
882
867
}
You can’t perform that action at this time.
0 commit comments