Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #12165 - table can't be joined multiple times
  • Loading branch information
wonder-sk committed Feb 10, 2015
1 parent a7f7740 commit 68f9464
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -453,7 +453,7 @@ void QgsVectorLayerFeatureIterator::prepareJoins()
QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo->joinLayerId ) );
Q_ASSERT( joinLayer );

if ( !mFetchJoinInfo.contains( joinLayer ) )
if ( !mFetchJoinInfo.contains( joinInfo ) )
{
FetchJoinInfo info;
info.joinInfo = joinInfo;
Expand All @@ -474,11 +474,11 @@ void QgsVectorLayerFeatureIterator::prepareJoins()
if ( !fetchAttributes.contains( info.targetField ) )
sourceJoinFields << info.targetField;

mFetchJoinInfo.insert( joinLayer, info );
mFetchJoinInfo.insert( joinInfo, info );
}

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

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

void QgsVectorLayerFeatureIterator::addJoinedAttributes( QgsFeature &f )
{
QMap<QgsVectorLayer*, FetchJoinInfo>::const_iterator joinIt = mFetchJoinInfo.constBegin();
QMap<const QgsVectorJoinInfo*, FetchJoinInfo>::const_iterator joinIt = mFetchJoinInfo.constBegin();
for ( ; joinIt != mFetchJoinInfo.constEnd(); ++joinIt )
{
const FetchJoinInfo& info = joinIt.value();
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerfeatureiterator.h
Expand Up @@ -148,7 +148,7 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera

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

QMap<int, QgsExpression*> mExpressionFieldInfo;

Expand Down
39 changes: 39 additions & 0 deletions tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -52,6 +52,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testJoinDetectCycle();
void testJoinSubset_data();
void testJoinSubset();
void testJoinTwoTimes();

private:
QgsVectorLayer* mLayerA;
Expand Down Expand Up @@ -301,6 +302,44 @@ void TestVectorLayerJoinBuffer::testJoinSubset()
}


void TestVectorLayerJoinBuffer::testJoinTwoTimes()
{
QVERIFY( mLayerA->pendingFields().count() == 1 );

QgsVectorJoinInfo joinInfo1;
joinInfo1.targetFieldName = "id_a";
joinInfo1.joinLayerId = mLayerB->id();
joinInfo1.joinFieldName = "id_b";
joinInfo1.memoryCache = true;
joinInfo1.prefix = "j1_";
mLayerA->addJoin( joinInfo1 );

QgsVectorJoinInfo joinInfo2;
joinInfo2.targetFieldName = "id_a";
joinInfo2.joinLayerId = mLayerB->id();
joinInfo2.joinFieldName = "id_b";
joinInfo2.memoryCache = true;
joinInfo2.prefix = "j2_";
mLayerA->addJoin( joinInfo2 );

QCOMPARE( mLayerA->vectorJoins().count(), 2 );

QVERIFY( mLayerA->pendingFields().count() == 3 );

QgsFeatureIterator fi = mLayerA->getFeatures();
QgsFeature fA1; //, fA2;
fi.nextFeature( fA1 );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QCOMPARE( fA1.attribute( "j1_value_b" ).toInt(), 11 );
QCOMPARE( fA1.attribute( "j2_value_b" ).toInt(), 11 );

mLayerA->removeJoin( mLayerB->id() );
mLayerA->removeJoin( mLayerB->id() );

QCOMPARE( mLayerA->vectorJoins().count(), 0 );
}


QTEST_MAIN( TestVectorLayerJoinBuffer )
#include "testqgsvectorlayerjoinbuffer.moc"

Expand Down

0 comments on commit 68f9464

Please sign in to comment.