Skip to content

Commit a20b0dc

Browse files
committedJul 10, 2017
[bugfix] Fixes relation widget reference when filter value contains a quote #16399
1 parent e6dc103 commit a20b0dc

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed
 

‎python/core/qgsexpression.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,15 @@ class QgsExpression
10081008
*/
10091009
static QString formatPreviewString( const QVariant& value );
10101010

1011+
/** Create an expression allowing to evaluate if a field is equal to a
1012+
* value. The value may be null.
1013+
* @param fieldName the name of the field
1014+
* @param value the value of the field
1015+
* @returns the expression to evaluate field equality
1016+
* @since QGIS 2.18
1017+
*/
1018+
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
1019+
10111020
protected:
10121021
void initGeomCalculator();
10131022
};

‎src/core/qgsexpression.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5324,6 +5324,18 @@ QVariant QgsExpression::StaticFunction::func( const QVariantList &values, const
53245324
Q_NOWARN_DEPRECATED_POP
53255325
}
53265326

5327+
QString QgsExpression::createFieldEqualityExpression( const QString &fieldName, const QVariant &value )
5328+
{
5329+
QString expr;
5330+
5331+
if ( value.isNull() )
5332+
expr = QString( "%1 IS NULL" ).arg( quotedColumnRef( fieldName ) );
5333+
else
5334+
expr = QString( "%1 = %2" ).arg( quotedColumnRef( fieldName ), quotedValue( value ) );
5335+
5336+
return expr;
5337+
}
5338+
53275339
const QgsExpression::Node* QgsExpression::rootNode() const
53285340
{
53295341
return d->mRootNode;

‎src/core/qgsexpression.h

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

1491+
/** Create an expression allowing to evaluate if a field is equal to a
1492+
* value. The value may be null.
1493+
* @param fieldName the name of the field
1494+
* @param value the value of the field
1495+
* @returns the expression to evaluate field equality
1496+
* @since QGIS 2.18
1497+
*/
1498+
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
1499+
14911500
protected:
14921501
void initGeomCalculator();
14931502

‎src/core/qgsrelation.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,8 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature& feature ) const
167167

168168
Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mFieldPairs )
169169
{
170-
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );
171-
QgsField referencingField = referencingLayer()->fields().at( referencingIdx );
172-
173-
if ( referencingField.type() == QVariant::String )
174-
{
175-
// Use quotes
176-
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencingField(), feature.attribute( fieldPair.referencedField() ).toString() );
177-
}
178-
else
179-
{
180-
// No quotes
181-
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencingField(), feature.attribute( fieldPair.referencedField() ).toString() );
182-
}
170+
QVariant val( feature.attribute( fieldPair.referencedField() ) );
171+
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencingField(), val );
183172
}
184173

185174
return conditions.join( " AND " );
@@ -191,21 +180,8 @@ QgsFeatureRequest QgsRelation::getReferencedFeatureRequest( const QgsAttributes&
191180

192181
Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mFieldPairs )
193182
{
194-
int referencedIdx = referencedLayer()->fields().indexFromName( fieldPair.referencedField() );
195183
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );
196-
197-
QgsField referencedField = referencedLayer()->fields().at( referencedIdx );
198-
199-
if ( referencedField.type() == QVariant::String )
200-
{
201-
// Use quotes
202-
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
203-
}
204-
else
205-
{
206-
// No quotes
207-
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
208-
}
184+
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencedField(), attributes.at( referencingIdx ) );
209185
}
210186

211187
QgsFeatureRequest myRequest;

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -846,14 +846,7 @@ void QgsRelationReferenceWidget::filterChanged()
846846
}
847847
else
848848
{
849-
if ( mReferencedLayer->fields().field( fieldName ).type() == QVariant::String )
850-
{
851-
filters << QString( "\"%1\" = '%2'" ).arg( fieldName, cb->currentText() );
852-
}
853-
else
854-
{
855-
filters << QString( "\"%1\" = %2" ).arg( fieldName, cb->currentText() );
856-
}
849+
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
857850
}
858851
attrs << mReferencedLayer->fieldNameIndex( fieldName );
859852
}

0 commit comments

Comments
 (0)
Please sign in to comment.