Navigation Menu

Skip to content

Commit

Permalink
Add a method for getting a quoted string value for use as a literal
Browse files Browse the repository at this point in the history
in expressions
  • Loading branch information
nyalldawson committed Dec 1, 2015
1 parent 95d2271 commit 48e4d1c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
30 changes: 28 additions & 2 deletions python/core/qgsexpression.sip
Expand Up @@ -340,11 +340,37 @@ class QgsExpression
*/
static QList<QgsExpression::Function *> specialColumns();

//! return quoted column reference (in double quotes)
/** Returns a quoted column reference (in double quotes)
* @see quotedString()
* @see quotedValue()
*/
static QString quotedColumnRef( QString name );
//! return quoted string (in single quotes)

/** Returns a quoted version of a string (in single quotes)
* @see quotedValue()
* @see quotedColumnRef()
*/
static QString quotedString( QString text );

/** Returns a string representation of a literal value, including appropriate
* quotations where required.
* @param value value to convert to a string representation
* @note added in QGIS 2.14
* @see quotedString()
* @see quotedColumnRef()
*/
static QString quotedValue( const QVariant& value );

/** Returns a string representation of a literal value, including appropriate
* quotations where required.
* @param value value to convert to a string representation
* @param type value type
* @note added in QGIS 2.14
* @see quotedString()
* @see quotedColumnRef()
*/
static QString quotedValue( const QVariant& value, QVariant::Type type );

//////

enum NodeType
Expand Down
27 changes: 27 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -2623,6 +2623,33 @@ QString QgsExpression::quotedString( QString text )
return QString( "'%1'" ).arg( text );
}

QString QgsExpression::quotedValue( const QVariant &value )
{
return quotedValue( value, value.type() );
}

QString QgsExpression::quotedValue( const QVariant& value, QVariant::Type type )
{
if ( value.isNull() )
return "NULL";

switch ( type )
{
case QVariant::Int:
case QVariant::LongLong:
case QVariant::Double:
return value.toString();

case QVariant::Bool:
return value.toBool() ? "TRUE" : "FALSE";

default:
case QVariant::String:
return quotedString( value.toString() );
}

}

bool QgsExpression::isFunctionName( const QString &name )
{
return functionIndex( name ) != -1;
Expand Down
30 changes: 28 additions & 2 deletions src/core/qgsexpression.h
Expand Up @@ -543,11 +543,37 @@ class CORE_EXPORT QgsExpression
*/
static QList<Function*> specialColumns();

//! return quoted column reference (in double quotes)
/** Returns a quoted column reference (in double quotes)
* @see quotedString()
* @see quotedValue()
*/
static QString quotedColumnRef( QString name );
//! return quoted string (in single quotes)

/** Returns a quoted version of a string (in single quotes)
* @see quotedValue()
* @see quotedColumnRef()
*/
static QString quotedString( QString text );

/** Returns a string representation of a literal value, including appropriate
* quotations where required.
* @param value value to convert to a string representation
* @note added in QGIS 2.14
* @see quotedString()
* @see quotedColumnRef()
*/
static QString quotedValue( const QVariant& value );

/** Returns a string representation of a literal value, including appropriate
* quotations where required.
* @param value value to convert to a string representation
* @param type value type
* @note added in QGIS 2.14
* @see quotedString()
* @see quotedColumnRef()
*/
static QString quotedValue( const QVariant& value, QVariant::Type type );

//////

class Visitor; // visitor interface is defined below
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1556,6 +1556,20 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression::quotedString( "hello\\world" ), QString( "'hello\\\\world'" ) );
}

void quoted_value()
{
QCOMPARE( QgsExpression::quotedValue( QVariant( "a string" ) ), QString( "'a string'" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( "a\nstring" ) ), QString( "'a\\nstring'" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( 5 ) ), QString( "5" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( 5 ), QVariant::String ), QString( "'5'" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( 5LL ) ), QString( "5" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( 5.5 ) ), QString( "5.5" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( true ) ), QString( "TRUE" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant( true ), QVariant::String ), QString( "'true'" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant() ), QString( "NULL" ) );
QCOMPARE( QgsExpression::quotedValue( QVariant(), QVariant::String ), QString( "NULL" ) );
}

void reentrant()
{
// this simply should not crash
Expand Down

0 comments on commit 48e4d1c

Please sign in to comment.