Skip to content

Commit

Permalink
Fix broken QMap<QVariantList,...> finding, which causes case-insensitive
Browse files Browse the repository at this point in the history
comparisons to be made when resolving primary keys in the Oracle and
Postgres providers

qt's built in qMapLessThanKey for QVariantList is broken and does a
case-insensitive operation, so we replace it with a working version instead...
  • Loading branch information
nyalldawson committed Nov 21, 2019
1 parent b14d7a8 commit 701ea05
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -258,6 +258,8 @@ QVariant data types (such as strings, numeric values, dates and times)
.. seealso:: :py:func:`qgsVariantLessThan`
%End



QString qgsVsiPrefix( const QString &path );


Expand Down
8 changes: 8 additions & 0 deletions src/core/qgis.cpp
Expand Up @@ -294,3 +294,11 @@ QString Qgis::devVersion()
{
return QString::fromUtf8( QGIS_DEV_VERSION );
}

template<>
bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
{
// qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
// this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
}
6 changes: 6 additions & 0 deletions src/core/qgis.h
Expand Up @@ -571,6 +571,12 @@ CORE_EXPORT bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );
*/
CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );

/**
* Compares two QVariantList values and returns whether the first is less than the second.
*/
template<> CORE_EXPORT bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 ) SIP_SKIP;


CORE_EXPORT QString qgsVsiPrefix( const QString &path );

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgis.cpp
Expand Up @@ -49,6 +49,7 @@ class TestQgis : public QObject
void testQgsVariantEqual();
void testQgsEnumValueToKey();
void testQgsEnumKeyToValue();
void testQMapQVariantList();

private:
QString mReport;
Expand Down Expand Up @@ -410,6 +411,24 @@ void TestQgis::testQgsEnumKeyToValue()
QCOMPARE( qgsEnumKeyToValue<QgsMapLayerModel::ItemDataRole>( QStringLiteral( "UnknownKey" ), QgsMapLayerModel::LayerIdRole ), QgsMapLayerModel::LayerIdRole );
}

void TestQgis::testQMapQVariantList()
{
QMap<QVariantList, long> ids;
ids.insert( QVariantList() << "B" << "c", 5 );
ids.insert( QVariantList() << "b" << "C", 7 );

QVariantList v = QVariantList() << "b" << "C";
QMap<QVariantList, long>::const_iterator it = ids.constFind( v );

QVERIFY( it != ids.constEnd() );
QCOMPARE( it.value(), 7L );

v = QVariantList() << "B" << "c";
it = ids.constFind( v );

QVERIFY( it != ids.constEnd() );
QCOMPARE( it.value(), 5L );
}


QGSTEST_MAIN( TestQgis )
Expand Down

0 comments on commit 701ea05

Please sign in to comment.