Skip to content

Commit

Permalink
Update layer fields before caching joined layer
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 authored and nyalldawson committed Sep 28, 2021
1 parent 9fdc868 commit bcb1679
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/core/vector/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -76,12 +76,6 @@ bool QgsVectorLayerJoinBuffer::addJoin( const QgsVectorLayerJoinInfo &joinInfo )
return false;
}

//cache joined layer to virtual memory if specified by user
if ( joinInfo.isUsingMemoryCache() )
{
cacheJoinLayer( mVectorJoins.last() );
}

// Wait for notifications about changed fields in joined layer to propagate them.
// During project load the joined layers possibly do not exist yet so the connection will not be created,
// but then QgsProject makes sure to call createJoinCaches() which will do the connection.
Expand All @@ -91,9 +85,16 @@ bool QgsVectorLayerJoinBuffer::addJoin( const QgsVectorLayerJoinInfo &joinInfo )
connectJoinedLayer( vl );
}

mLayer->updateFields();

//cache joined layer to virtual memory if specified by user
if ( joinInfo.isUsingMemoryCache() )
{
cacheJoinLayer( mVectorJoins.last() );
}

locker.unlock();

emit joinedFieldsChanged();
return true;
}

Expand Down Expand Up @@ -178,17 +179,18 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorLayerJoinInfo &joinInfo
if ( i == joinFieldIndex )
continue;

if ( !joinInfo.prefix().isNull() ) // Default prefix 'layerName_' used
{
// Joined field name
const QString joinFieldName = joinInfo.prefix() + cacheLayer->fields().names().at( i );
QString joinInfoPrefix = joinInfo.prefix();
if ( joinInfoPrefix.isNull() ) // Default prefix 'layerName_' used
joinInfoPrefix = QString( "%1_" ).arg( cacheLayer->name() );

// Check for name collisions
int fieldIndex = mLayer->fields().indexFromName( joinFieldName );
if ( fieldIndex >= 0
&& mLayer->fields().fieldOrigin( fieldIndex ) != QgsFields::OriginJoin )
continue;
}
// Joined field name
const QString joinFieldName = joinInfoPrefix + cacheLayer->fields().names().at( i );

// Check for name collisions
int fieldIndex = mLayer->fields().indexFromName( joinFieldName );
if ( fieldIndex >= 0
&& mLayer->fields().fieldOrigin( fieldIndex ) != QgsFields::OriginJoin )
continue;

attributesCache.append( attrs.at( i ) );
}
Expand Down

0 comments on commit bcb1679

Please sign in to comment.