Skip to content

Commit

Permalink
[FEATURE] implement coalesce and concat function expressions (impleme…
Browse files Browse the repository at this point in the history
…nts #4551)
  • Loading branch information
jef-n committed Jun 22, 2012
1 parent 86e9129 commit 3599700
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/core/qgsexpression.cpp
Expand Up @@ -270,6 +270,16 @@ static QVariant fcnToString( const QVariantList& values, QgsFeature* , QgsExpres
{
return QVariant( getStringValue( values.at( 0 ), parent ) );
}
static QVariant fcnCoalesce( const QVariantList& values, QgsFeature* , QgsExpression* )
{
foreach( const QVariant &value, values )
{
if ( value.isNull() )
continue;
return value;
}
return QVariant();
}
static QVariant fcnLower( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
Expand Down Expand Up @@ -324,6 +334,15 @@ static QVariant fcnFeatureId( const QVariantList& , QgsFeature* f, QgsExpression
// TODO: handling of 64-bit feature ids?
return f ? QVariant(( int )f->id() ) : QVariant();
}
static QVariant fcnConcat( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString concat;
foreach( const QVariant &value, values )
{
concat += getStringValue( value, parent );
}
return concat;
}

#define ENSURE_GEOM_TYPE(f, g, geomtype) if (!f) return QVariant(); \
QgsGeometry* g = f->geometry(); \
Expand Down Expand Up @@ -431,13 +450,15 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
<< FunctionDef( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
<< FunctionDef( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
<< FunctionDef( "tostring", 1, fcnToString, QObject::tr( "Conversions" ) )
<< FunctionDef( "coalesce", -1, fcnCoalesce, QObject::tr( "Conversions" ) )
// string manipulation
<< FunctionDef( "lower", 1, fcnLower, QObject::tr( "String" ) )
<< FunctionDef( "upper", 1, fcnUpper, QObject::tr( "String" ) )
<< FunctionDef( "length", 1, fcnLength, QObject::tr( "String" ) )
<< FunctionDef( "replace", 3, fcnReplace, QObject::tr( "String" ) )
<< FunctionDef( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
<< FunctionDef( "concat", -1, fcnConcat, QObject::tr( "String" ) )
// geometry accessors
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
Expand Down Expand Up @@ -693,7 +714,7 @@ QgsExpression::Node* QgsExpression::Node::createFromOgcFilter( QDomElement &elem
QgsExpression::Node *operand2 = 0, *upperBound = 0;

QDomElement operandElem = element.firstChildElement();
while( !operandElem.isNull() )
while ( !operandElem.isNull() )
{
if ( operandElem.localName() == "LowerBoundary" )
{
Expand Down Expand Up @@ -1340,8 +1361,8 @@ QVariant QgsExpression::NodeFunction::eval( QgsExpression* parent, QgsFeature* f
{
QVariant v = n->eval( parent, f );
ENSURE_NO_EVAL_ERROR;
if ( isNull( v ) )
return QVariant(); // all "normal" functions return NULL when any parameter is NULL
if ( isNull( v ) && fd.mFcn != fcnCoalesce )
return QVariant(); // all "normal" functions return NULL, when any parameter is NULL (so coalesce is abnormal)
argValues.append( v );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpressionparser.yy
Expand Up @@ -165,7 +165,7 @@ expression:
exp_error("Function is not known");
YYERROR;
}
if (QgsExpression::BuiltinFunctions()[fnIndex].mParams != $3->count())
if ( QgsExpression::BuiltinFunctions()[fnIndex].mParams != -1 && QgsExpression::BuiltinFunctions()[fnIndex].mParams != $3->count() )
{
exp_error("Function is called with wrong number of arguments");
YYERROR;
Expand Down

0 comments on commit 3599700

Please sign in to comment.