Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow expression functions to include 0 length parameter lists
This change allows for some cleanups to the built in expression
functions, by making it possible to differentiate variables
(eg $feature) from functions which don't require parameters
(eg uuid(), now()... ). Also adds aliases for uuid(), now(), and
pi().
  • Loading branch information
nyalldawson committed May 3, 2015
1 parent bd9e02b commit 96db1bd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 39 deletions.
12 changes: 0 additions & 12 deletions resources/function_help/$now

This file was deleted.

11 changes: 0 additions & 11 deletions resources/function_help/$pi

This file was deleted.

13 changes: 0 additions & 13 deletions resources/function_help/$uuid

This file was deleted.

6 changes: 3 additions & 3 deletions src/core/qgsexpression.cpp
Expand Up @@ -1728,7 +1728,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "scale_exp", 6, fcnExpScale, "Math" )
<< new StaticFunction( "floor", 1, fcnFloor, "Math" )
<< new StaticFunction( "ceil", 1, fcnCeil, "Math" )
<< new StaticFunction( "$pi", 0, fcnPi, "Math" )
<< new StaticFunction( "pi", 0, fcnPi, "Math", QString(), false, QStringList(), false, QStringList() << "$pi" )
<< new StaticFunction( "to_int", 1, fcnToInt, "Conversions", QString(), false, QStringList(), false, QStringList() << "toint" )
<< new StaticFunction( "to_real", 1, fcnToReal, "Conversions", QString(), false, QStringList(), false, QStringList() << "toreal" )
<< new StaticFunction( "to_string", 1, fcnToString, "Conversions", QString(), false, QStringList(), false, QStringList() << "tostring" )
Expand All @@ -1739,7 +1739,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "coalesce", -1, fcnCoalesce, "Conditionals" )
<< new StaticFunction( "if", 3, fcnIf, "Conditionals", "", False, QStringList(), true )
<< new StaticFunction( "regexp_match", 2, fcnRegexpMatch, "Conditionals" )
<< new StaticFunction( "$now", 0, fcnNow, "Date and Time" )
<< new StaticFunction( "now", 0, fcnNow, "Date and Time", QString(), false, QStringList(), false, QStringList() << "$now" )
<< new StaticFunction( "age", 2, fcnAge, "Date and Time" )
<< new StaticFunction( "year", 1, fcnYear, "Date and Time" )
<< new StaticFunction( "month", 1, fcnMonth, "Date and Time" )
Expand Down Expand Up @@ -1817,7 +1817,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "$id", 0, fcnFeatureId, "Record" )
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
<< new StaticFunction( "$scale", 0, fcnScale, "Record" )
<< new StaticFunction( "$uuid", 0, fcnUuid, "Record" )
<< new StaticFunction( "uuid", 0, fcnUuid, "Record", QString(), false, QStringList(), false, QStringList() << "$uuid" )
<< new StaticFunction( "get_feature", 3, fcnGetFeature, "Record", QString(), false, QStringList(), false, QStringList() << "getFeature" )

//return all attributes string for referencedColumns - this is caught by
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsexpressionparser.yy
Expand Up @@ -196,6 +196,25 @@ expression:
delete $1;
}

| FUNCTION '(' ')'
{
int fnIndex = QgsExpression::functionIndex(*$1);
if (fnIndex == -1)
{
// this should not actually happen because already in lexer we check whether an identifier is a known function
// (if the name is not known the token is parsed as a column)
exp_error(parser_ctx, "Function is not known");
YYERROR;
}
if ( QgsExpression::Functions()[fnIndex]->params() != 0 )
{
exp_error(parser_ctx, "Function is called with wrong number of arguments");
YYERROR;
}
$$ = new QgsExpression::NodeFunction(fnIndex, new QgsExpression::NodeList());
delete $1;
}

| expression IN '(' exp_list ')' { $$ = new QgsExpression::NodeInOperator($1, $4, false); }
| expression NOT IN '(' exp_list ')' { $$ = new QgsExpression::NodeInOperator($1, $5, true); }

Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -408,6 +408,8 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
continue;
if ( func->params() != 0 )
name += "(";
else if ( !name.startsWith( "$" ) )
name += "()";
registerItem( func->group(), func->name(), " " + name + " ", func->helptext() );
}

Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -297,6 +297,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "concat numbers" ) << "1 || 2" << false << QVariant( "12" );

// math functions
QTest::newRow( "pi" ) << "pi()" << false << QVariant( M_PI );
QTest::newRow( "sqrt" ) << "sqrt(16)" << false << QVariant( 4. );
QTest::newRow( "abs(0.1)" ) << "abs(0.1)" << false << QVariant( 0.1 );
QTest::newRow( "abs(0)" ) << "abs(0)" << false << QVariant( 0. );
Expand Down

0 comments on commit 96db1bd

Please sign in to comment.