Skip to content
Permalink
Browse files

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 96db1bdb7ed7164cc7673525b1b07a07646cb74d

This file was deleted.

This file was deleted.

This file was deleted.

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

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

@@ -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. );

0 comments on commit 96db1bd

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