Skip to content
Permalink
Browse files

[bugfix] Fixes relation widget reference when filter value contains a…

… quote #16399
  • Loading branch information
pblottiere committed Jul 10, 2017
1 parent e6dc103 commit a20b0dc4d334bfba52efc2740b74242a7f0d7924
@@ -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();
};
@@ -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;
@@ -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();

@@ -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 " );
@@ -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;
@@ -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 );
}

0 comments on commit a20b0dc

Please sign in to comment.
You can’t perform that action at this time.