Skip to content

Commit

Permalink
[bugfix] Fixes relation widget reference when filter value contains a…
Browse files Browse the repository at this point in the history
… quote #16399
  • Loading branch information
pblottiere committed Jul 10, 2017
1 parent e6dc103 commit a20b0dc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsexpression.sip
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,15 @@ class 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 2.18
*/
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );

protected:
void initGeomCalculator();
};
12 changes: 12 additions & 0 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5324,6 +5324,18 @@ QVariant QgsExpression::StaticFunction::func( const QVariantList &values, const
Q_NOWARN_DEPRECATED_POP
}

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

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

return expr;
}

const QgsExpression::Node* QgsExpression::rootNode() const
{
return d->mRootNode;
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,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 2.18
*/
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );

protected:
void initGeomCalculator();

Expand Down
30 changes: 3 additions & 27 deletions src/core/qgsrelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,8 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature& feature ) const

Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mFieldPairs )
{
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );
QgsField referencingField = referencingLayer()->fields().at( referencingIdx );

if ( referencingField.type() == QVariant::String )
{
// Use quotes
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencingField(), feature.attribute( fieldPair.referencedField() ).toString() );
}
else
{
// No quotes
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencingField(), feature.attribute( fieldPair.referencedField() ).toString() );
}
QVariant val( feature.attribute( fieldPair.referencedField() ) );
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencingField(), val );
}

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

Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mFieldPairs )
{
int referencedIdx = referencedLayer()->fields().indexFromName( fieldPair.referencedField() );
int referencingIdx = referencingLayer()->fields().indexFromName( fieldPair.referencingField() );

QgsField referencedField = referencedLayer()->fields().at( referencedIdx );

if ( referencedField.type() == QVariant::String )
{
// Use quotes
conditions << QString( "\"%1\" = '%2'" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
}
else
{
// No quotes
conditions << QString( "\"%1\" = %2" ).arg( fieldPair.referencedField(), attributes.at( referencingIdx ).toString() );
}
conditions << QgsExpression::createFieldEqualityExpression( fieldPair.referencedField(), attributes.at( referencingIdx ) );
}

QgsFeatureRequest myRequest;
Expand Down
9 changes: 1 addition & 8 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,7 @@ void QgsRelationReferenceWidget::filterChanged()
}
else
{
if ( mReferencedLayer->fields().field( fieldName ).type() == QVariant::String )
{
filters << QString( "\"%1\" = '%2'" ).arg( fieldName, cb->currentText() );
}
else
{
filters << QString( "\"%1\" = %2" ).arg( fieldName, cb->currentText() );
}
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
}
attrs << mReferencedLayer->fieldNameIndex( fieldName );
}
Expand Down

0 comments on commit a20b0dc

Please sign in to comment.