Skip to content

Commit 48e4d1c

Browse files
committed
Add a method for getting a quoted string value for use as a literal
in expressions
1 parent 95d2271 commit 48e4d1c

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

python/core/qgsexpression.sip

+28-2
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,37 @@ class QgsExpression
340340
*/
341341
static QList<QgsExpression::Function *> specialColumns();
342342

343-
//! return quoted column reference (in double quotes)
343+
/** Returns a quoted column reference (in double quotes)
344+
* @see quotedString()
345+
* @see quotedValue()
346+
*/
344347
static QString quotedColumnRef( QString name );
345-
//! return quoted string (in single quotes)
348+
349+
/** Returns a quoted version of a string (in single quotes)
350+
* @see quotedValue()
351+
* @see quotedColumnRef()
352+
*/
346353
static QString quotedString( QString text );
347354

355+
/** Returns a string representation of a literal value, including appropriate
356+
* quotations where required.
357+
* @param value value to convert to a string representation
358+
* @note added in QGIS 2.14
359+
* @see quotedString()
360+
* @see quotedColumnRef()
361+
*/
362+
static QString quotedValue( const QVariant& value );
363+
364+
/** Returns a string representation of a literal value, including appropriate
365+
* quotations where required.
366+
* @param value value to convert to a string representation
367+
* @param type value type
368+
* @note added in QGIS 2.14
369+
* @see quotedString()
370+
* @see quotedColumnRef()
371+
*/
372+
static QString quotedValue( const QVariant& value, QVariant::Type type );
373+
348374
//////
349375

350376
enum NodeType

src/core/qgsexpression.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,33 @@ QString QgsExpression::quotedString( QString text )
26232623
return QString( "'%1'" ).arg( text );
26242624
}
26252625

2626+
QString QgsExpression::quotedValue( const QVariant &value )
2627+
{
2628+
return quotedValue( value, value.type() );
2629+
}
2630+
2631+
QString QgsExpression::quotedValue( const QVariant& value, QVariant::Type type )
2632+
{
2633+
if ( value.isNull() )
2634+
return "NULL";
2635+
2636+
switch ( type )
2637+
{
2638+
case QVariant::Int:
2639+
case QVariant::LongLong:
2640+
case QVariant::Double:
2641+
return value.toString();
2642+
2643+
case QVariant::Bool:
2644+
return value.toBool() ? "TRUE" : "FALSE";
2645+
2646+
default:
2647+
case QVariant::String:
2648+
return quotedString( value.toString() );
2649+
}
2650+
2651+
}
2652+
26262653
bool QgsExpression::isFunctionName( const QString &name )
26272654
{
26282655
return functionIndex( name ) != -1;

src/core/qgsexpression.h

+28-2
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,37 @@ class CORE_EXPORT QgsExpression
543543
*/
544544
static QList<Function*> specialColumns();
545545

546-
//! return quoted column reference (in double quotes)
546+
/** Returns a quoted column reference (in double quotes)
547+
* @see quotedString()
548+
* @see quotedValue()
549+
*/
547550
static QString quotedColumnRef( QString name );
548-
//! return quoted string (in single quotes)
551+
552+
/** Returns a quoted version of a string (in single quotes)
553+
* @see quotedValue()
554+
* @see quotedColumnRef()
555+
*/
549556
static QString quotedString( QString text );
550557

558+
/** Returns a string representation of a literal value, including appropriate
559+
* quotations where required.
560+
* @param value value to convert to a string representation
561+
* @note added in QGIS 2.14
562+
* @see quotedString()
563+
* @see quotedColumnRef()
564+
*/
565+
static QString quotedValue( const QVariant& value );
566+
567+
/** Returns a string representation of a literal value, including appropriate
568+
* quotations where required.
569+
* @param value value to convert to a string representation
570+
* @param type value type
571+
* @note added in QGIS 2.14
572+
* @see quotedString()
573+
* @see quotedColumnRef()
574+
*/
575+
static QString quotedValue( const QVariant& value, QVariant::Type type );
576+
551577
//////
552578

553579
class Visitor; // visitor interface is defined below

tests/src/core/testqgsexpression.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,20 @@ class TestQgsExpression: public QObject
15561556
QCOMPARE( QgsExpression::quotedString( "hello\\world" ), QString( "'hello\\\\world'" ) );
15571557
}
15581558

1559+
void quoted_value()
1560+
{
1561+
QCOMPARE( QgsExpression::quotedValue( QVariant( "a string" ) ), QString( "'a string'" ) );
1562+
QCOMPARE( QgsExpression::quotedValue( QVariant( "a\nstring" ) ), QString( "'a\\nstring'" ) );
1563+
QCOMPARE( QgsExpression::quotedValue( QVariant( 5 ) ), QString( "5" ) );
1564+
QCOMPARE( QgsExpression::quotedValue( QVariant( 5 ), QVariant::String ), QString( "'5'" ) );
1565+
QCOMPARE( QgsExpression::quotedValue( QVariant( 5LL ) ), QString( "5" ) );
1566+
QCOMPARE( QgsExpression::quotedValue( QVariant( 5.5 ) ), QString( "5.5" ) );
1567+
QCOMPARE( QgsExpression::quotedValue( QVariant( true ) ), QString( "TRUE" ) );
1568+
QCOMPARE( QgsExpression::quotedValue( QVariant( true ), QVariant::String ), QString( "'true'" ) );
1569+
QCOMPARE( QgsExpression::quotedValue( QVariant() ), QString( "NULL" ) );
1570+
QCOMPARE( QgsExpression::quotedValue( QVariant(), QVariant::String ), QString( "NULL" ) );
1571+
}
1572+
15591573
void reentrant()
15601574
{
15611575
// this simply should not crash

0 commit comments

Comments
 (0)