Skip to content

Commit 68f9464

Browse files
committed
Fix #12165 - table can't be joined multiple times
1 parent a7f7740 commit 68f9464

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/core/qgsvectorlayerfeatureiterator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ void QgsVectorLayerFeatureIterator::prepareJoins()
453453
QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo->joinLayerId ) );
454454
Q_ASSERT( joinLayer );
455455

456-
if ( !mFetchJoinInfo.contains( joinLayer ) )
456+
if ( !mFetchJoinInfo.contains( joinInfo ) )
457457
{
458458
FetchJoinInfo info;
459459
info.joinInfo = joinInfo;
@@ -474,11 +474,11 @@ void QgsVectorLayerFeatureIterator::prepareJoins()
474474
if ( !fetchAttributes.contains( info.targetField ) )
475475
sourceJoinFields << info.targetField;
476476

477-
mFetchJoinInfo.insert( joinLayer, info );
477+
mFetchJoinInfo.insert( joinInfo, info );
478478
}
479479

480480
// store field source index - we'll need it when fetching from provider
481-
mFetchJoinInfo[ joinLayer ].attributes.push_back( sourceLayerIndex );
481+
mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
482482
}
483483

484484
// add sourceJoinFields if we're using a subset
@@ -525,7 +525,7 @@ void QgsVectorLayerFeatureIterator::prepareExpressions()
525525

526526
void QgsVectorLayerFeatureIterator::addJoinedAttributes( QgsFeature &f )
527527
{
528-
QMap<QgsVectorLayer*, FetchJoinInfo>::const_iterator joinIt = mFetchJoinInfo.constBegin();
528+
QMap<const QgsVectorJoinInfo*, FetchJoinInfo>::const_iterator joinIt = mFetchJoinInfo.constBegin();
529529
for ( ; joinIt != mFetchJoinInfo.constEnd(); ++joinIt )
530530
{
531531
const FetchJoinInfo& info = joinIt.value();

src/core/qgsvectorlayerfeatureiterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera
148148

149149
/** information about joins used in the current select() statement.
150150
Allows faster mapping of attribute ids compared to mVectorJoins */
151-
QMap<QgsVectorLayer*, FetchJoinInfo> mFetchJoinInfo;
151+
QMap<const QgsVectorJoinInfo*, FetchJoinInfo> mFetchJoinInfo;
152152

153153
QMap<int, QgsExpression*> mExpressionFieldInfo;
154154

tests/src/core/testqgsvectorlayerjoinbuffer.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TestVectorLayerJoinBuffer : public QObject
5252
void testJoinDetectCycle();
5353
void testJoinSubset_data();
5454
void testJoinSubset();
55+
void testJoinTwoTimes();
5556

5657
private:
5758
QgsVectorLayer* mLayerA;
@@ -301,6 +302,44 @@ void TestVectorLayerJoinBuffer::testJoinSubset()
301302
}
302303

303304

305+
void TestVectorLayerJoinBuffer::testJoinTwoTimes()
306+
{
307+
QVERIFY( mLayerA->pendingFields().count() == 1 );
308+
309+
QgsVectorJoinInfo joinInfo1;
310+
joinInfo1.targetFieldName = "id_a";
311+
joinInfo1.joinLayerId = mLayerB->id();
312+
joinInfo1.joinFieldName = "id_b";
313+
joinInfo1.memoryCache = true;
314+
joinInfo1.prefix = "j1_";
315+
mLayerA->addJoin( joinInfo1 );
316+
317+
QgsVectorJoinInfo joinInfo2;
318+
joinInfo2.targetFieldName = "id_a";
319+
joinInfo2.joinLayerId = mLayerB->id();
320+
joinInfo2.joinFieldName = "id_b";
321+
joinInfo2.memoryCache = true;
322+
joinInfo2.prefix = "j2_";
323+
mLayerA->addJoin( joinInfo2 );
324+
325+
QCOMPARE( mLayerA->vectorJoins().count(), 2 );
326+
327+
QVERIFY( mLayerA->pendingFields().count() == 3 );
328+
329+
QgsFeatureIterator fi = mLayerA->getFeatures();
330+
QgsFeature fA1; //, fA2;
331+
fi.nextFeature( fA1 );
332+
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
333+
QCOMPARE( fA1.attribute( "j1_value_b" ).toInt(), 11 );
334+
QCOMPARE( fA1.attribute( "j2_value_b" ).toInt(), 11 );
335+
336+
mLayerA->removeJoin( mLayerB->id() );
337+
mLayerA->removeJoin( mLayerB->id() );
338+
339+
QCOMPARE( mLayerA->vectorJoins().count(), 0 );
340+
}
341+
342+
304343
QTEST_MAIN( TestVectorLayerJoinBuffer )
305344
#include "testqgsvectorlayerjoinbuffer.moc"
306345

0 commit comments

Comments
 (0)