Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsExpressionNode::setCachedStaticValue
  • Loading branch information
nyalldawson committed Jan 9, 2023
1 parent b468a18 commit 3cca02b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/expression/qgsexpressionnode.cpp
Expand Up @@ -56,6 +56,12 @@ bool QgsExpressionNode::prepare( QgsExpression *parent, const QgsExpressionConte
}
}

void QgsExpressionNode::setCachedStaticValue( const QVariant &value ) const
{
mHasCachedValue = true;
mCachedStaticValue = value;
}

QgsExpressionNode::QgsExpressionNode( const QgsExpressionNode &other )
: parserFirstLine( other.parserFirstLine )
, parserFirstColumn( other.parserFirstColumn )
Expand Down
9 changes: 9 additions & 0 deletions src/core/expression/qgsexpressionnode.h
Expand Up @@ -333,6 +333,15 @@ class CORE_EXPORT QgsExpressionNode SIP_ABSTRACT
*/
QVariant cachedStaticValue() const { return mCachedStaticValue; }

/**
* Sets the cached static \a value for the node.
*
* \note Not available from Python bindings.
*
* \since QGIS 3.30
*/
void setCachedStaticValue( const QVariant &value ) const SIP_SKIP;

/**
* Returns a reference to the simplest node which represents this node,
* after any compilation optimizations have been applied.
Expand Down
24 changes: 24 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -5592,6 +5592,30 @@ class TestQgsExpression: public QObject
QCOMPARE( exp.evaluate( &context ).toInt(), 20 );
}

void testNodeSetCachedStaticValue()
{
QgsFields fields;
fields.append( QgsField( QStringLiteral( "first_field" ), QVariant::Int ) );
fields.append( QgsField( QStringLiteral( "second_field" ), QVariant::Int ) );
fields.append( QgsField( QStringLiteral( "third_field" ), QVariant::Int ) );

QgsFeature f( fields );
f.setAttributes( QgsAttributes() << 11 << 20 << 300 );

QgsExpressionContext context;
context.setFields( fields );
context.setFeature( f );

QgsExpression exp( QStringLiteral( "CASE WHEN \"first_field\" = 5 then \"second_field\" when \"first_field\" = 6 then \"second_field\" * 2 else \"second_field\" * 3 end" ) );
QVERIFY( exp.prepare( &context ) );
QVERIFY( !exp.rootNode()->hasCachedStaticValue() );

// force set a cached static value
exp.rootNode()->setCachedStaticValue( 55 );
QVERIFY( exp.rootNode()->hasCachedStaticValue() );
QCOMPARE( exp.rootNode()->cachedStaticValue().toInt(), 55 );
}

void testExpressionUtilsToLocalizedString()
{
const QVariant t_int( 12346 );
Expand Down

0 comments on commit 3cca02b

Please sign in to comment.