mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[expression] Add try() function to provide a way to detect and handle
expressions which can intermittently fail.
This commit is contained in:
parent
1d8bd004a6
commit
7599d4f8d5
14
resources/function_help/json/try
Normal file
14
resources/function_help/json/try
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "try",
|
||||
"type": "function",
|
||||
"description": "Tries an expression and returns its value if error-free. If the expression returns an error, an alternative value will be returned when provided otherwise the function will return null.",
|
||||
"arguments": [
|
||||
{"arg":"expression","description":"the expression which should be run"},
|
||||
{"arg":"alternative","optional":true,"description":"the result which will be returned if the expression returns an error."}
|
||||
],
|
||||
"examples": [
|
||||
{ "expression":"try( to_int( '1' ), 0 )", "returns":"1"},
|
||||
{ "expression":"try( to_int( 'a' ), 0 )", "returns":"0"},
|
||||
{ "expression":"try( to_date( 'invalid_date' ) )", "returns":"NULL"}
|
||||
]
|
||||
}
|
@ -3688,6 +3688,21 @@ static QVariant fcnColorRgb( const QVariantList &values, const QgsExpressionCont
|
||||
return QStringLiteral( "%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
|
||||
}
|
||||
|
||||
static QVariant fcnTry( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
{
|
||||
QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
|
||||
QVariant value = node->eval( parent, context );
|
||||
if ( parent->hasEvalError() )
|
||||
{
|
||||
parent->setEvalErrorString( QString() );
|
||||
node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
|
||||
ENSURE_NO_EVAL_ERROR;
|
||||
value = node->eval( parent, context );
|
||||
ENSURE_NO_EVAL_ERROR;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static QVariant fcnIf( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
{
|
||||
QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
|
||||
@ -4763,6 +4778,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "coalesce" ), -1, fcnCoalesce, QStringLiteral( "Conditionals" ), QString(), false, QSet<QString>(), false, QStringList(), true )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "nullif" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value1" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value2" ) ), fcnNullIf, QStringLiteral( "Conditionals" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "if" ), 3, fcnIf, QStringLiteral( "Conditionals" ), QString(), false, QSet<QString>(), true )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "try" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "expression" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "alternative" ), true, QVariant() ), fcnTry, QStringLiteral( "Conditionals" ), QString(), false, QSet<QString>(), true )
|
||||
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "aggregate" ),
|
||||
QgsExpressionFunction::ParameterList()
|
||||
|
@ -1177,6 +1177,9 @@ class TestQgsExpression: public QObject
|
||||
QTest::newRow( "regexp match false" ) << "regexp_match('abc DEF','\\\\s[a-z]+')" << false << QVariant( 0 );
|
||||
QTest::newRow( "if true" ) << "if(1=1, 1, 0)" << false << QVariant( 1 );
|
||||
QTest::newRow( "if false" ) << "if(1=2, 1, 0)" << false << QVariant( 0 );
|
||||
QTest::newRow( "try valid" ) << "try(to_int('1'),0)" << false << QVariant( 1 );
|
||||
QTest::newRow( "try invalid with alternative" ) << "try(to_int('a'),0)" << false << QVariant( 0 );
|
||||
QTest::newRow( "try invalid without alternative" ) << "try(to_int('a'))" << false << QVariant();
|
||||
|
||||
// Datetime functions
|
||||
QTest::newRow( "to date" ) << "todate('2012-06-28')" << false << QVariant( QDate( 2012, 6, 28 ) );
|
||||
|
Loading…
x
Reference in New Issue
Block a user