Skip to content

Commit 0dcfd56

Browse files
committedOct 9, 2017
Rowid column from ogr provider is ignored thanks to a blacklist join option
1 parent 4a8fce2 commit 0dcfd56

7 files changed

+102
-22
lines changed
 

‎python/core/qgsvectorlayerjoininfo.sip

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ Returns whether values from the joined layer should be cached in memory to speed
166166
:rtype: QgsFeature
167167
%End
168168

169+
void setJoinFieldNamesBlackList( const QStringList &blackList );
170+
171+
QStringList joinFieldNamesBlackList() const;
172+
%Docstring
173+
:rtype: list of str
174+
%End
175+
176+
bool hasSubset( bool blacklisted = true ) const;
177+
%Docstring
178+
:rtype: bool
179+
%End
180+
181+
static QStringList joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blacklisted = true );
182+
%Docstring
183+
:rtype: list of str
184+
%End
185+
169186
bool operator==( const QgsVectorLayerJoinInfo &other ) const;
170187

171188
void setJoinFieldNamesSubset( QStringList *fieldNamesSubset /Transfer/ );
@@ -194,6 +211,7 @@ Returns whether values from the joined layer should be cached in memory to speed
194211

195212

196213

214+
197215
};
198216

199217

‎src/core/qgsauxiliarystorage.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &fil
168168
mJoinInfo.setEditable( true );
169169
mJoinInfo.setUpsertOnEdit( true );
170170
mJoinInfo.setCascadedDelete( true );
171+
mJoinInfo.setJoinFieldNamesBlackList( QStringList() << "rowid" ); // introduced by ogr provider
171172
}
172173

173174
QgsVectorLayer *QgsAuxiliaryLayer::toSpatialLayer() const

‎src/core/qgsvectorlayerfeatureiterator.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,12 @@ void QgsVectorLayerFeatureIterator::FetchJoinInfo::addJoinedAttributesDirect( Qg
944944

945945
// maybe user requested just a subset of layer's attributes
946946
// so we do not have to cache everything
947-
bool hasSubset = joinInfo->joinFieldNamesSubset();
948947
QVector<int> subsetIndices;
949-
if ( hasSubset )
950-
subsetIndices = QgsVectorLayerJoinBuffer::joinSubsetIndices( joinLayer, *joinInfo->joinFieldNamesSubset() );
948+
if ( joinInfo->hasSubset() )
949+
{
950+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( *joinInfo );
951+
subsetIndices = QgsVectorLayerJoinBuffer::joinSubsetIndices( joinLayer, subsetNames );
952+
}
951953

952954
// select (no geometry)
953955
QgsFeatureRequest request;
@@ -963,7 +965,7 @@ void QgsVectorLayerFeatureIterator::FetchJoinInfo::addJoinedAttributesDirect( Qg
963965
{
964966
int index = indexOffset;
965967
QgsAttributes attr = fet.attributes();
966-
if ( hasSubset )
968+
if ( joinInfo->hasSubset() )
967969
{
968970
for ( int i = 0; i < subsetIndices.count(); ++i )
969971
f.setAttribute( index++, attr.at( subsetIndices.at( i ) ) );

‎src/core/qgsvectorlayerjoinbuffer.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorLayerJoinInfo &joinInfo
137137
request.setFlags( QgsFeatureRequest::NoGeometry );
138138
// maybe user requested just a subset of layer's attributes
139139
// so we do not have to cache everything
140-
bool hasSubset = joinInfo.joinFieldNamesSubset();
141140
QVector<int> subsetIndices;
142-
if ( hasSubset )
141+
if ( joinInfo.hasSubset() )
143142
{
144-
subsetIndices = joinSubsetIndices( cacheLayer, *joinInfo.joinFieldNamesSubset() );
143+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( joinInfo );
144+
subsetIndices = joinSubsetIndices( cacheLayer, subsetNames );
145145

146146
// we need just subset of attributes - but make sure to include join field name
147147
QgsAttributeList cacheLayerAttrs = subsetIndices.toList();
@@ -156,7 +156,7 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorLayerJoinInfo &joinInfo
156156
{
157157
QgsAttributes attrs = f.attributes();
158158
QString key = attrs.at( joinFieldIndex ).toString();
159-
if ( hasSubset )
159+
if ( joinInfo.hasSubset() )
160160
{
161161
QgsAttributes subsetAttrs( subsetIndices.count() );
162162
for ( int i = 0; i < subsetIndices.count(); ++i )
@@ -213,11 +213,10 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields &fields )
213213
QString joinFieldName = joinIt->joinFieldName();
214214

215215
QSet<QString> subset;
216-
bool hasSubset = false;
217-
if ( joinIt->joinFieldNamesSubset() )
216+
if ( joinIt->hasSubset() )
218217
{
219-
hasSubset = true;
220-
subset = QSet<QString>::fromList( *joinIt->joinFieldNamesSubset() );
218+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( *joinIt );
219+
subset = QSet<QString>::fromList( subsetNames );
221220
}
222221

223222
if ( joinIt->prefix().isNull() )
@@ -232,12 +231,12 @@ void QgsVectorLayerJoinBuffer::updateFields( QgsFields &fields )
232231
for ( int idx = 0; idx < joinFields.count(); ++idx )
233232
{
234233
// if using just a subset of fields, filter some of them out
235-
if ( hasSubset && !subset.contains( joinFields.at( idx ).name() ) )
234+
if ( joinIt->hasSubset() && !subset.contains( joinFields.at( idx ).name() ) )
236235
continue;
237236

238237
//skip the join field to avoid double field names (fields often have the same name)
239238
// when using subset of field, use all the selected fields
240-
if ( hasSubset || joinFields.at( idx ).name() != joinFieldName )
239+
if ( joinIt->hasSubset() || joinFields.at( idx ).name() != joinFieldName )
241240
{
242241
QgsField f = joinFields.at( idx );
243242
f.setName( prefix + f.name() );
@@ -279,10 +278,12 @@ void QgsVectorLayerJoinBuffer::writeXml( QDomNode &layer_node, QDomDocument &doc
279278
joinElem.setAttribute( QStringLiteral( "upsertOnEdit" ), joinIt->hasUpsertOnEdit() );
280279
joinElem.setAttribute( QStringLiteral( "cascadedDelete" ), joinIt->hasCascadedDelete() );
281280

282-
if ( joinIt->joinFieldNamesSubset() )
281+
if ( joinIt->hasSubset() )
283282
{
284283
QDomElement subsetElem = document.createElement( QStringLiteral( "joinFieldsSubset" ) );
285-
Q_FOREACH ( const QString &fieldName, *joinIt->joinFieldNamesSubset() )
284+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( *joinIt );
285+
286+
Q_FOREACH ( const QString &fieldName, subsetNames )
286287
{
287288
QDomElement fieldElem = document.createElement( QStringLiteral( "field" ) );
288289
fieldElem.setAttribute( QStringLiteral( "name" ), fieldName );
@@ -552,10 +553,10 @@ bool QgsVectorLayerJoinBuffer::addFeatures( QgsFeatureList &features, QgsFeature
552553

553554
if ( existingFeature.isValid() )
554555
{
555-
const QStringList *subsetFields = info.joinFieldNamesSubset();
556-
if ( subsetFields )
556+
if ( info.hasSubset() )
557557
{
558-
Q_FOREACH ( const QString &field, *subsetFields )
558+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( info );
559+
Q_FOREACH ( const QString &field, subsetNames )
559560
{
560561
QVariant newValue = joinFeature.attribute( field );
561562
int fieldIndex = joinLayer->fields().indexOf( field );

‎src/core/qgsvectorlayerjoininfo.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,50 @@ QgsFeature QgsVectorLayerJoinInfo::extractJoinedFeature( const QgsFeature &featu
6868

6969
return joinFeature;
7070
}
71+
72+
QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blacklisted )
73+
{
74+
QStringList fieldNames;
75+
76+
if ( blacklisted && !info.joinFieldNamesBlackList().isEmpty() )
77+
{
78+
QStringList *lst = info.joinFieldNamesSubset();
79+
if ( lst )
80+
{
81+
Q_FOREACH ( const QString &s, *lst )
82+
{
83+
if ( !info.joinFieldNamesBlackList().contains( s ) )
84+
fieldNames.append( s );
85+
}
86+
}
87+
else
88+
{
89+
Q_FOREACH ( const QgsField &f, info.joinLayer()->fields() )
90+
{
91+
if ( !info.joinFieldNamesBlackList().contains( f.name() )
92+
&& f.name() != info.joinFieldName() )
93+
fieldNames.append( f.name() );
94+
}
95+
}
96+
}
97+
else
98+
{
99+
QStringList *lst = info.joinFieldNamesSubset();
100+
if ( lst )
101+
{
102+
fieldNames = *lst;
103+
}
104+
}
105+
106+
return fieldNames;
107+
}
108+
109+
bool QgsVectorLayerJoinInfo::hasSubset( bool blacklisted ) const
110+
{
111+
bool subset = joinFieldNamesSubset();
112+
113+
if ( blacklisted )
114+
subset |= !joinFieldNamesBlackList().isEmpty();
115+
116+
return subset;
117+
}

‎src/core/qgsvectorlayerjoininfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ class CORE_EXPORT QgsVectorLayerJoinInfo
141141
*/
142142
QgsFeature extractJoinedFeature( const QgsFeature &feature ) const;
143143

144+
void setJoinFieldNamesBlackList( const QStringList &blackList ) { mBlackList = blackList; }
145+
146+
QStringList joinFieldNamesBlackList() const { return mBlackList; }
147+
148+
bool hasSubset( bool blacklisted = true ) const;
149+
150+
static QStringList joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blacklisted = true );
151+
144152
bool operator==( const QgsVectorLayerJoinInfo &other ) const
145153
{
146154
return mTargetFieldName == other.mTargetFieldName &&
@@ -197,6 +205,8 @@ class CORE_EXPORT QgsVectorLayerJoinInfo
197205

198206
bool mCascadedDelete = false;
199207

208+
QStringList mBlackList;
209+
200210
//! Cache for joined attributes to provide fast lookup (size is 0 if no memory caching)
201211
QHash< QString, QgsAttributes> cachedAttributes;
202212

‎src/gui/qgsattributeform.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,10 +1996,11 @@ void QgsAttributeForm::updateJoinedFields( const QgsEditorWidgetWrapper &eww )
19961996

19971997
mJoinedFeatures[info] = joinFeature;
19981998

1999-
QStringList *subsetFields = info->joinFieldNamesSubset();
2000-
if ( subsetFields )
1999+
if ( info->hasSubset() )
20012000
{
2002-
Q_FOREACH ( const QString &field, *subsetFields )
2001+
const QStringList subsetNames = QgsVectorLayerJoinInfo::joinFieldNamesSubset( *info );
2002+
2003+
Q_FOREACH ( const QString &field, subsetNames )
20032004
{
20042005
QString prefixedName = info->prefixedFieldName( field );
20052006
QVariant val;

0 commit comments

Comments
 (0)
Please sign in to comment.