Skip to content

Commit

Permalink
Fix broken compatibility of table join (project file) (fix #7068)
Browse files Browse the repository at this point in the history
  • Loading branch information
minorua committed Mar 29, 2013
1 parent ab55325 commit 30c343c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
14 changes: 11 additions & 3 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -844,15 +844,23 @@ void QgsVectorLayerProperties::addJoinToTreeWidget( const QgsVectorJoinInfo& joi
QTreeWidgetItem* joinItem = new QTreeWidgetItem();

QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( join.joinLayerId ) );
if ( !joinLayer )
if ( !layer || !joinLayer )
{
return;
}

joinItem->setText( 0, joinLayer->name() );
joinItem->setData( 0, Qt::UserRole, join.joinLayerId );
joinItem->setText( 1, join.joinFieldName );
joinItem->setText( 2, join.targetFieldName );

if ( join.joinFieldName.isEmpty() && join.joinFieldIndex >= 0 && join.joinFieldIndex < joinLayer->pendingFields().count() )
joinItem->setText( 1, joinLayer->pendingFields().field( join.joinFieldIndex ).name() ); //for compatibility with 1.x
else
joinItem->setText( 1, join.joinFieldName );

if ( join.targetFieldName.isEmpty() && join.targetFieldIndex >= 0 && join.targetFieldIndex < layer->pendingFields().count() )
joinItem->setText( 2, layer->pendingFields().field( join.targetFieldIndex ).name() ); //for compatibility with 1.x
else
joinItem->setText( 2, join.targetFieldName );

mJoinTreeWidget->addTopLevelItem( joinItem );
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/qgsvectorlayer.h
Expand Up @@ -135,9 +135,10 @@ struct CORE_EXPORT QgsVectorJoinInfo
*/
QHash< QString, QgsAttributes> cachedAttributes;

// the following are temporaries, assigned by QgsVectorLayerJoinBuffer::updateFields()
mutable int tmpTargetField;
mutable int tmpJoinField;
/**Join field index in the target layer. For backward compatibility with 1.x (x>=7)*/
int targetFieldIndex;
/**Join field index in the source layer. For backward compatibility with 1.x (x>=7)*/
int joinFieldIndex;
};


Expand Down
21 changes: 18 additions & 3 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -311,8 +311,17 @@ void QgsVectorLayerFeatureIterator::prepareJoins()
FetchJoinInfo info;
info.joinInfo = joinInfo;
info.joinLayer = joinLayer;
info.targetField = fields.indexFromName( joinInfo->targetFieldName );
info.joinField = joinLayer->pendingFields().indexFromName( joinInfo->joinFieldName );

if ( joinInfo->targetFieldName.isEmpty() )
info.targetField = joinInfo->targetFieldIndex; //for compatibility with 1.x
else
info.targetField = fields.indexFromName( joinInfo->targetFieldName );

if ( joinInfo->joinFieldName.isEmpty() )
info.joinField = joinInfo->joinFieldIndex; //for compatibility with 1.x
else
info.joinField = joinLayer->pendingFields().indexFromName( joinInfo->joinFieldName );

info.indexOffset = *attIt - sourceLayerIndex;
if ( info.joinField < sourceLayerIndex )
info.indexOffset++;
Expand Down Expand Up @@ -391,7 +400,13 @@ void QgsVectorLayerFeatureIterator::FetchJoinInfo::addJoinedAttributesDirect( Qg
subsetString.append( " AND " );
}

subsetString.append( "\"" + joinInfo->joinFieldName + "\"" + " = " + "\"" + joinValue.toString() + "\"" );
QString joinFieldName;
if ( joinInfo->joinFieldName.isEmpty() && joinInfo->joinFieldIndex >= 0 && joinInfo->joinFieldIndex < joinLayer->pendingFields().count() )
joinFieldName = joinLayer->pendingFields().field( joinInfo->joinFieldIndex ).name(); // for compatibility with 1.x
else
joinFieldName = joinInfo->joinFieldName;

subsetString.append( "\"" + joinFieldName + "\"" + " = " + "\"" + joinValue.toString() + "\"" );
joinLayer->dataProvider()->setSubsetString( subsetString, false );

// select (no geometry)
Expand Down
37 changes: 28 additions & 9 deletions src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -63,7 +63,11 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
QgsVectorLayer* cacheLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) );
if ( cacheLayer )
{
int joinFieldIndex = cacheLayer->pendingFields().indexFromName( joinInfo.joinFieldName );
int joinFieldIndex;
if ( joinInfo.joinFieldName.isEmpty() )
joinFieldIndex = joinInfo.joinFieldIndex; //for compatibility with 1.x
else
joinFieldIndex = cacheLayer->pendingFields().indexFromName( joinInfo.joinFieldName );

joinInfo.cachedAttributes.clear();

Expand All @@ -88,15 +92,17 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
continue;
}

joinIt->tmpTargetField = fields.indexFromName( joinIt->targetFieldName );

const QgsFields& joinFields = joinLayer->pendingFields();
joinIt->tmpJoinField = joinFields.indexFromName( joinIt->joinFieldName );
QString joinFieldName;
if ( joinIt->joinFieldName.isEmpty() && joinIt->joinFieldIndex >= 0 && joinIt->joinFieldIndex < joinFields.count() )
joinFieldName = joinFields.field( joinIt->joinFieldIndex ).name(); //for compatibility with 1.x
else
joinFieldName = joinIt->joinFieldName;

for ( int idx = 0; idx < joinFields.count(); ++idx )
{
//skip the join field to avoid double field names (fields often have the same name)
if ( joinFields[idx].name() != joinIt->joinFieldName )
if ( joinFields[idx].name() != joinFieldName )
{
QgsField f = joinFields[idx];
f.setName( joinLayer->name() + "_" + f.name() );
Expand Down Expand Up @@ -124,9 +130,18 @@ void QgsVectorLayerJoinBuffer::writeXml( QDomNode& layer_node, QDomDocument& doc
for ( ; joinIt != mVectorJoins.constEnd(); ++joinIt )
{
QDomElement joinElem = document.createElement( "join" );
joinElem.setAttribute( "targetFieldName", joinIt->targetFieldName );

if ( joinIt->targetFieldName.isEmpty() )
joinElem.setAttribute( "targetField", joinIt->targetFieldIndex ); //for compatibility with 1.x
else
joinElem.setAttribute( "targetFieldName", joinIt->targetFieldName );

joinElem.setAttribute( "joinLayerId", joinIt->joinLayerId );
joinElem.setAttribute( "joinFieldName", joinIt->joinFieldName );
if ( joinIt->joinFieldName.isEmpty() )
joinElem.setAttribute( "joinField", joinIt->joinFieldIndex ); //for compatibility with 1.x
else
joinElem.setAttribute( "joinFieldName", joinIt->joinFieldName );

joinElem.setAttribute( "memoryCache", !joinIt->cachedAttributes.isEmpty() );
vectorJoinsElem.appendChild( joinElem );
}
Expand All @@ -143,10 +158,14 @@ void QgsVectorLayerJoinBuffer::readXml( const QDomNode& layer_node )
{
QDomElement infoElem = joinList.at( i ).toElement();
QgsVectorJoinInfo info;
info.joinFieldName = infoElem.attribute( "joinFieldName" ); // TODO[MD]: compatibility with 1.x?
info.joinFieldName = infoElem.attribute( "joinFieldName" );
info.joinLayerId = infoElem.attribute( "joinLayerId" );
info.targetFieldName = infoElem.attribute( "targetFieldName" ); // TODO[MD]: compatibility with 1.x?
info.targetFieldName = infoElem.attribute( "targetFieldName" );
info.memoryCache = infoElem.attribute( "memoryCache" ).toInt();

info.joinFieldIndex = infoElem.attribute( "joinField" ).toInt(); //for compatibility with 1.x
info.targetFieldIndex = infoElem.attribute( "targetField" ).toInt(); //for compatibility with 1.x

addJoin( info );
}
}
Expand Down

0 comments on commit 30c343c

Please sign in to comment.