Skip to content
Permalink
Browse files

Fix #12165 - table can't be joined multiple times

  • Loading branch information
wonder-sk committed Feb 10, 2015
1 parent a7f7740 commit 68f94647fddf3b972bbc96db5a53c81fa654662f
@@ -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;
@@ -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
@@ -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();
@@ -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;

@@ -52,6 +52,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testJoinDetectCycle();
void testJoinSubset_data();
void testJoinSubset();
void testJoinTwoTimes();

private:
QgsVectorLayer* mLayerA;
@@ -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"

0 comments on commit 68f9464

Please sign in to comment.
You can’t perform that action at this time.