Skip to content

Commit

Permalink
Introduce QgsVariantUtils::isNumericType
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 11, 2024
1 parent c2e80dd commit 61935ad
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/PyQt6/core/auto_additions/qgsvariantutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
try:
QgsVariantUtils.typeToDisplayString = staticmethod(QgsVariantUtils.typeToDisplayString)
QgsVariantUtils.isNull = staticmethod(QgsVariantUtils.isNull)
QgsVariantUtils.isNumericType = staticmethod(QgsVariantUtils.isNumericType)
except NameError:
pass
7 changes: 7 additions & 0 deletions python/PyQt6/core/auto_generated/qgsvariantutils.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ This method is more rigorous vs QVariant.isNull(), which will return
.. versionadded:: 3.28
%End

static bool isNumericType( QMetaType::Type metaType );
%Docstring
Returns ``True`` if the specified ``metaType`` is a numeric type.

.. versionadded:: 3.40
%End




Expand Down
1 change: 1 addition & 0 deletions python/core/auto_additions/qgsvariantutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
try:
QgsVariantUtils.typeToDisplayString = staticmethod(QgsVariantUtils.typeToDisplayString)
QgsVariantUtils.isNull = staticmethod(QgsVariantUtils.isNull)
QgsVariantUtils.isNumericType = staticmethod(QgsVariantUtils.isNumericType)
except NameError:
pass
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsvariantutils.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ This method is more rigorous vs QVariant.isNull(), which will return
.. versionadded:: 3.28
%End

static bool isNumericType( QMetaType::Type metaType );
%Docstring
Returns ``True`` if the specified ``metaType`` is a numeric type.

.. versionadded:: 3.40
%End




Expand Down
4 changes: 2 additions & 2 deletions src/core/qgssqlexpressioncompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode( const Qg
{
const QgsExpressionNodeColumnRef *col = static_cast<const QgsExpressionNodeColumnRef *>( node );
const int idx = mFields.indexFromName( col->name() );
return idx >= 0 && mFields[idx].isNumeric();
return idx >= 0 && QgsVariantUtils::isNumericType( mFields[idx].type() );
}
case QgsExpressionNode::ntLiteral:
{
const QgsExpressionNodeLiteral *lit = static_cast<const QgsExpressionNodeLiteral *>( node );
return lit->value().type() == QVariant::Double || lit->value().type() == QVariant::Int;
return QgsVariantUtils::isNumericType( static_cast< QMetaType::Type >( lit->value().userType() ) );
}
case QgsExpressionNode::ntBinaryOperator:
{
Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsvariantutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,27 @@ bool QgsVariantUtils::isNull( const QVariant &variant, bool silenceNullWarnings
return false;
}

bool QgsVariantUtils::isNumericType( QMetaType::Type metaType )
{
switch ( metaType )
{
case QMetaType::Type::Int:
case QMetaType::Type::UInt:
case QMetaType::Type::LongLong:
case QMetaType::Type::ULongLong:
case QMetaType::Type::Double:
case QMetaType::Type::Float:
case QMetaType::Type::Short:
case QMetaType::Type::UShort:
case QMetaType::Type::Char:
case QMetaType::Type::UChar:
case QMetaType::Type::SChar:
return true;
default:
return false;
}
}

QMetaType::Type QgsVariantUtils::variantTypeToMetaType( QVariant::Type variantType )
{
// variant types can be directly mapped to meta types
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvariantutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class CORE_EXPORT QgsVariantUtils
*/
static bool isNull( const QVariant &variant, bool silenceNullWarnings SIP_PYARGREMOVE = false );

/**
* Returns TRUE if the specified \a metaType is a numeric type.
* \since QGIS 3.40
*/
static bool isNumericType( QMetaType::Type metaType );

/**
* Converts a QVariant::Type to a QMetaType::Type.
* \see metaTypeToVariantType()
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ if (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsVectorFileWriterPostgres test_qgsvectorfilewriter_postgres.py)
ADD_PYTHON_TEST(PyQgsQueryResultModel test_qgsqueryresultmodel.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtilsPostgres test_qgsvectorlayerutils_postgres.py)
ADD_PYTHON_TEST(PyQgsVariantUtils test_qgsvariantutils.py)
ADD_PYTHON_TEST(PyQgsPostgresProviderLatency test_provider_postgres_latency.py TEST_TIMEOUT=600)
ADD_PYTHON_TEST(PyQgsPostgresRasterProvider test_provider_postgresraster.py)
ADD_PYTHON_TEST(PyQgsPostgresDomain test_qgspostgresdomain.py)
Expand Down

0 comments on commit 61935ad

Please sign in to comment.