Skip to content
Permalink
Browse files

Add a method for getting a quoted string value for use as a literal

in expressions
  • Loading branch information
nyalldawson committed Dec 1, 2015
1 parent 95d2271 commit 48e4d1c977740b7f1c6cccba0506eb468ec70881
Showing with 97 additions and 4 deletions.
  1. +28 −2 python/core/qgsexpression.sip
  2. +27 −0 src/core/qgsexpression.cpp
  3. +28 −2 src/core/qgsexpression.h
  4. +14 −0 tests/src/core/testqgsexpression.cpp
@@ -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
@@ -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;
@@ -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
@@ -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

0 comments on commit 48e4d1c

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