Skip to content

Commit febe30d

Browse files
committedMay 2, 2016
[expressions] Fix fetching joined column refs when expression is
not prepared (fix #14746)
1 parent d3fcdb4 commit febe30d

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,11 +4283,23 @@ QgsExpression::Node*QgsExpression::NodeLiteral::clone() const
42834283
QVariant QgsExpression::NodeColumnRef::eval( QgsExpression *parent, const QgsExpressionContext *context )
42844284
{
42854285
Q_UNUSED( parent );
4286+
int index = mIndex;
4287+
4288+
if ( index < 0 )
4289+
{
4290+
// have not yet found field index - first check explicitly set fields collection
4291+
if ( context->hasVariable( QgsExpressionContext::EXPR_FIELDS ) )
4292+
{
4293+
QgsFields fields = qvariant_cast<QgsFields>( context->variable( QgsExpressionContext::EXPR_FIELDS ) );
4294+
index = fields.fieldNameIndex( mName );
4295+
}
4296+
}
4297+
42864298
if ( context && context->hasVariable( QgsExpressionContext::EXPR_FEATURE ) )
42874299
{
42884300
QgsFeature feature = qvariant_cast<QgsFeature>( context->variable( QgsExpressionContext::EXPR_FEATURE ) );
4289-
if ( mIndex >= 0 )
4290-
return feature.attribute( mIndex );
4301+
if ( index >= 0 )
4302+
return feature.attribute( index );
42914303
else
42924304
return feature.attribute( mName );
42934305
}

‎tests/src/core/testqgsexpression.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,47 @@ class TestQgsExpression: public QObject
21012101
QgsExpression nodeExpression( "1 IN (1, 2, 3, 4)" );
21022102
QgsExpression nodeExpression2( nodeExpression );
21032103
}
2104+
2105+
void test_columnRefUnprepared()
2106+
{
2107+
//test retrieving fields from feature when expression is unprepared - explicitly specified fields collection
2108+
//should take precedence over feature's field collection
2109+
2110+
QgsFields fields;
2111+
fields.append( QgsField( "f1", QVariant::String ) );
2112+
2113+
QgsFeature f( 1 );
2114+
f.setFields( fields );
2115+
2116+
//also add a joined field - this will not be available in feature's field collection
2117+
fields.append( QgsField( "j1", QVariant::String ), QgsFields::OriginJoin, 1 );
2118+
2119+
f.setAttributes( QgsAttributes() << QVariant( "f1" ) << QVariant( "j1" ) );
2120+
f.setValid( true );
2121+
2122+
QgsExpression e( "\"f1\"" );
2123+
QgsExpressionContext context;
2124+
context.setFeature( f );
2125+
context.setFields( fields );
2126+
QVariant result = e.evaluate( &context );
2127+
QCOMPARE( result.toString(), QString( "f1" ) );
2128+
2129+
//test joined field
2130+
QgsExpression e2( "\"j1\"" );
2131+
result = e2.evaluate( &context );
2132+
QCOMPARE( result.toString(), QString( "j1" ) );
2133+
2134+
// final test - check that feature's field collection is also used when corresponding field NOT found
2135+
// in explicitly passed field collection
2136+
fields.append( QgsField( "f2", QVariant::String ) );
2137+
f.setFields( fields );
2138+
f.setAttributes( QgsAttributes() << QVariant( "f1" ) << QVariant( "j1" ) << QVariant( "f2" ) );
2139+
context.setFeature( f );
2140+
QgsExpression e3( "\"f2\"" );
2141+
result = e3.evaluate( &context );
2142+
QCOMPARE( result.toString(), QString( "f2" ) );
2143+
}
2144+
21042145
};
21052146

21062147
QTEST_MAIN( TestQgsExpression )

0 commit comments

Comments
 (0)
Please sign in to comment.