29#include "moc_qgsvectorlayerjoinbuffer.cpp"
31using namespace Qt::StringLiterals;
39 QList<QgsVectorLayer *> lst;
49static bool _hasCycleDFS(
QgsVectorLayer *n, QHash<QgsVectorLayer *, int> &mark )
51 if ( mark.value( n ) == 1 )
53 if ( mark.value( n ) == 0 )
56 const auto outEdges { _outEdges( n ) };
59 if ( _hasCycleDFS( m, mark ) )
70 QMutexLocker locker( &mMutex );
71 mVectorJoins.push_back( joinInfo );
75 QHash<QgsVectorLayer *, int> markDFS;
76 if ( mLayer && _hasCycleDFS( mLayer, markDFS ) )
79 mVectorJoins.pop_back();
89 connectJoinedLayer( vl );
95 mLayer->updateFields();
102 cacheJoinLayer( mVectorJoins.last() );
115 QMutexLocker locker( &mMutex );
116 for (
int i = 0; i < mVectorJoins.size(); ++i )
118 if ( mVectorJoins.at( i ).joinLayerId() == joinLayerId )
125 mVectorJoins.removeAt( i );
143 QgsVectorLayer *cacheLayer = joinInfo.
joinLayer();
148 if ( joinFieldIndex < 0 || joinFieldIndex >= cacheLayer->
fields().
count() )
153 QgsFeatureRequest request;
157 QVector<int> subsetIndices;
165 if ( !cacheLayerAttrs.contains( joinFieldIndex ) )
166 cacheLayerAttrs.append( joinFieldIndex );
170 QgsFeatureIterator fit = cacheLayer->
getFeatures( request );
175 QString key = attrs.at( joinFieldIndex ).toString();
178 QgsAttributes subsetAttrs( subsetIndices.count() );
179 for (
int i = 0; i < subsetIndices.count(); ++i )
180 subsetAttrs[i] = attrs.at( subsetIndices.at( i ) );
185 QgsAttributes attributesCache;
186 for (
int i = 0; i < attrs.size(); i++ )
188 if ( i == joinFieldIndex )
191 QString joinInfoPrefix = joinInfo.
prefix();
192 if ( joinInfoPrefix.isNull() )
193 joinInfoPrefix = QString(
"%1_" ).arg( cacheLayer->
name() );
196 const QString joinFieldName = joinInfoPrefix + cacheLayer->
fields().
names().at( i );
199 int fieldIndex = mLayer->fields().indexFromName( joinFieldName );
203 attributesCache.append( attrs.at( i ) );
220 QVector<int> subsetIndices;
221 for (
int i = 0; i < joinFieldsSubset.count(); ++i )
223 QString joinedFieldName = joinFieldsSubset.at( i );
224 int index = joinLayerFields.
lookupField( joinedFieldName );
227 subsetIndices.append( index );
231 QgsDebugError(
"Join layer subset field not found: " + joinedFieldName );
235 return subsetIndices;
242 QList< QgsVectorLayerJoinInfo>::const_iterator joinIt = mVectorJoins.constBegin();
243 for (
int joinIdx = 0; joinIt != mVectorJoins.constEnd(); ++joinIt, ++joinIdx )
252 QString joinFieldName = joinIt->joinFieldName();
254 QSet<QString> subset;
255 if ( joinIt->hasSubset() )
258 subset = qgis::listToSet( subsetNames );
261 if ( joinIt->prefix().isNull() )
263 prefix = joinLayer->
name() +
'_';
267 prefix = joinIt->prefix();
270 for (
int idx = 0; idx < joinFields.
count(); ++idx )
273 if ( joinIt->hasSubset() && !subset.contains( joinFields.
at( idx ).
name() ) )
278 if ( joinIt->hasSubset() || joinFields.
at( idx ).
name() != joinFieldName )
290 QMutexLocker locker( &mMutex );
291 QList< QgsVectorLayerJoinInfo >::iterator joinIt = mVectorJoins.begin();
292 for ( ; joinIt != mVectorJoins.end(); ++joinIt )
294 if ( joinIt->isUsingMemoryCache() && joinIt->cacheDirty )
295 cacheJoinLayer( *joinIt );
302 QDomElement vectorJoinsElem = document.createElement( u
"vectorjoins"_s );
303 layer_node.appendChild( vectorJoinsElem );
304 QList< QgsVectorLayerJoinInfo >::const_iterator joinIt = mVectorJoins.constBegin();
305 for ( ; joinIt != mVectorJoins.constEnd(); ++joinIt )
310 QDomElement joinElem = document.createElement( u
"join"_s );
312 joinElem.setAttribute( u
"targetFieldName"_s, joinIt->targetFieldName() );
314 joinElem.setAttribute( u
"joinLayerId"_s, joinIt->joinLayerId() );
315 joinElem.setAttribute( u
"joinFieldName"_s, joinIt->joinFieldName() );
317 joinElem.setAttribute( u
"memoryCache"_s, joinIt->isUsingMemoryCache() );
318 joinElem.setAttribute( u
"dynamicForm"_s, joinIt->isDynamicFormEnabled() );
319 joinElem.setAttribute( u
"editable"_s, joinIt->isEditable() );
320 joinElem.setAttribute( u
"upsertOnEdit"_s, joinIt->hasUpsertOnEdit() );
321 joinElem.setAttribute( u
"cascadedDelete"_s, joinIt->hasCascadedDelete() );
323 if ( joinIt->hasSubset() )
325 QDomElement subsetElem = document.createElement( u
"joinFieldsSubset"_s );
328 const auto constSubsetNames = subsetNames;
329 for (
const QString &fieldName : constSubsetNames )
331 QDomElement fieldElem = document.createElement( u
"field"_s );
332 fieldElem.setAttribute( u
"name"_s, fieldName );
333 subsetElem.appendChild( fieldElem );
336 joinElem.appendChild( subsetElem );
339 if ( !joinIt->prefix().isNull() )
341 joinElem.setAttribute( u
"customPrefix"_s, joinIt->prefix() );
342 joinElem.setAttribute( u
"hasCustomPrefix"_s, 1 );
345 vectorJoinsElem.appendChild( joinElem );
351 mVectorJoins.clear();
352 QDomElement vectorJoinsElem = layer_node.firstChildElement( u
"vectorjoins"_s );
353 if ( !vectorJoinsElem.isNull() )
355 QDomNodeList joinList = vectorJoinsElem.elementsByTagName( u
"join"_s );
356 for (
int i = 0; i < joinList.size(); ++i )
358 QDomElement infoElem = joinList.at( i ).toElement();
366 info.
setEditable( infoElem.attribute( u
"editable"_s ).toInt() );
367 info.
setUpsertOnEdit( infoElem.attribute( u
"upsertOnEdit"_s ).toInt() );
370 QDomElement subsetElem = infoElem.firstChildElement( u
"joinFieldsSubset"_s );
371 if ( !subsetElem.isNull() )
373 QStringList *fieldNames =
new QStringList;
374 QDomNodeList fieldNodes = infoElem.elementsByTagName( u
"field"_s );
375 fieldNames->reserve( fieldNodes.count() );
376 for (
int i = 0; i < fieldNodes.count(); ++i )
377 *fieldNames << fieldNodes.at( i ).toElement().attribute( u
"name"_s );
381 if ( infoElem.attribute( u
"hasCustomPrefix"_s ).toInt() )
382 info.
setPrefix( infoElem.attribute( u
"customPrefix"_s ) );
393 bool resolved =
false;
394 for ( QgsVectorJoinList::iterator it = mVectorJoins.begin(); it != mVectorJoins.end(); ++it )
396 if ( it->joinLayer() )
399 if (
QgsVectorLayer *joinedLayer = qobject_cast<QgsVectorLayer *>( project->
mapLayer( it->joinLayerId() ) ) )
401 it->setJoinLayer( joinedLayer );
402 connectJoinedLayer( joinedLayer );
416 int joinIndex = mVectorJoins.indexOf( *info );
417 if ( joinIndex == -1 )
420 for (
int i = 0; i < fields.
count(); ++i )
437 int sourceJoinIndex = originIndex / 1000;
438 sourceFieldIndex = originIndex % 1000;
440 if ( sourceJoinIndex < 0 || sourceJoinIndex >= mVectorJoins.count() )
443 return &( mVectorJoins[sourceJoinIndex] );
448 QList<const QgsVectorLayerJoinInfo *> infos;
450 const auto constMVectorJoins = mVectorJoins;
453 if ( infos.contains( &info ) )
456 if ( info.targetFieldName() == field.
name() )
457 infos.append( &info );
484 return joinedFeature;
504 return targetedFeature;
510 cloned->mVectorJoins = mVectorJoins;
514void QgsVectorLayerJoinBuffer::joinedLayerUpdatedFields()
519 QgsVectorLayer *joinedLayer = qobject_cast<QgsVectorLayer *>( sender() );
520 Q_ASSERT( joinedLayer );
523 for ( QgsVectorJoinList::iterator it = mVectorJoins.begin(); it != mVectorJoins.end(); ++it )
525 if ( joinedLayer == it->joinLayer() )
527 it->cachedAttributes.clear();
528 cacheJoinLayer( *it );
535void QgsVectorLayerJoinBuffer::joinedLayerModified()
537 QgsVectorLayer *joinedLayer = qobject_cast<QgsVectorLayer *>( sender() );
538 Q_ASSERT( joinedLayer );
541 for ( QgsVectorJoinList::iterator it = mVectorJoins.begin(); it != mVectorJoins.end(); ++it )
543 if ( joinedLayer == it->joinLayer() )
545 it->cacheDirty =
true;
550void QgsVectorLayerJoinBuffer::joinedLayerWillBeDeleted()
552 QgsVectorLayer *joinedLayer = qobject_cast<QgsVectorLayer *>( sender() );
553 Q_ASSERT( joinedLayer );
558void QgsVectorLayerJoinBuffer::connectJoinedLayer(
QgsVectorLayer *vl )
576 if ( joinLayer && joinLayer->
isEditable() && info.isEditable() && info.hasUpsertOnEdit() )
580 for (
const QgsFeature &feature : std::as_const( features ) )
582 const QgsFeature joinFeature = info.extractJoinedFeature( feature );
586 const QVariant idFieldValue = feature.
attribute( info.targetFieldName() );
599 if ( existingFeature.
isValid() )
601 if ( info.hasSubset() )
604 const auto constSubsetNames = subsetNames;
605 for (
const QString &field : constSubsetNames )
607 QVariant newValue = joinFeature.
attribute( field );
615 for (
const auto &field : joinFields )
617 QVariant newValue = joinFeature.
attribute( field.name() );
618 int fieldIndex = joinLayer->
fields().
indexOf( field.name() );
626 bool notNullFields =
false;
628 for (
const auto &field : joinFields )
630 if ( field.name() == info.joinFieldName() )
635 notNullFields =
true;
641 joinFeatures << joinFeature;
661 QgsFeature feature = mLayer->getFeature( fid );
684 for (
auto it = newValues.constBegin(); it != newValues.constEnd(); ++it )
686 const int field = it.key();
687 const QVariant newValue = it.value();
690 if ( oldValues.contains( field ) )
691 oldValue = oldValues[field];
709 const auto constFids = fids;
715 if ( info.isEditable() && info.hasCascadedDelete() )
719 info.joinLayer()->deleteFeature( joinFeature.
id(), context );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ Join
Field originates from a joined layer.
Allows managing the auxiliary storage for a vector layer.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
bool isValid() const
Returns the validity of this feature.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
void setName(const QString &name)
Set the field name.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
void layerModified()
Emitted when modifications has been done on layer.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
void resolveReferences(QgsProject *project)
Resolves layer IDs of joined layers using given project's available layers.
QgsFeature targetedFeatureOf(const QgsVectorLayerJoinInfo *info, const QgsFeature &feature) const
Returns the targeted feature corresponding to the joined feature.
bool addJoin(const QgsVectorLayerJoinInfo &joinInfo)
Joins another vector layer to this layer.
void readXml(const QDomNode &layer_node)
Reads joins from project file.
QgsVectorLayerJoinBuffer(QgsVectorLayer *layer=nullptr)
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves mVectorJoins to xml under the layer node.
int joinedFieldsOffset(const QgsVectorLayerJoinInfo *info, const QgsFields &fields)
Find out what is the first index of the join within fields.
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant())
Changes attribute value in joined layers.
bool deleteFeatures(const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context=nullptr) const
Deletes a list of features from joined layers.
QList< const QgsVectorLayerJoinInfo * > joinsWhereFieldIsId(const QgsField &field) const
Returns joins where the field of a target layer is considered as an id.
bool removeJoin(const QString &joinLayerId)
Removes a vector layer join.
bool containsJoins() const
Quick way to test if there is any join at all.
bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues=QgsAttributeMap())
Changes attributes' values in joined layers.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features in joined layers.
QgsVectorLayerJoinBuffer * clone() const
Create a copy of the join buffer.
void joinedFieldsChanged()
Emitted whenever the list of joined fields changes (e.g.
void createJoinCaches()
Calls cacheJoinLayer() for all vector joins.
void updateFields(QgsFields &fields)
Updates field map with joined attributes.
static QVector< int > joinSubsetIndices(QgsVectorLayer *joinLayer, const QStringList &joinFieldsSubset)
Returns a vector of indices for use in join based on field names from the layer.
bool deleteFeature(QgsFeatureId fid, QgsVectorLayer::DeleteContext *context=nullptr) const
Deletes a feature from joined layers.
QgsFeature joinedFeatureOf(const QgsVectorLayerJoinInfo *info, const QgsFeature &feature) const
Returns the joined feature corresponding to the feature.
const QgsVectorJoinList & vectorJoins() const
bool isAuxiliaryJoin(const QgsVectorLayerJoinInfo &info) const
Returns true if the join information is about auxiliary layer, false otherwise.
Defines left outer join from our vector layer to some other vector layer.
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
void setDynamicFormEnabled(bool enabled)
Sets whether the form has to be dynamically updated with joined fields when a feature is being create...
void setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
void setJoinLayerId(const QString &layerId)
Sets ID of the joined layer. It will need to be overwritten by setJoinLayer() to a reference to real ...
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
bool cacheDirty
True if the cached join attributes need to be updated.
bool isEditable() const
Returns whether joined fields may be edited through the form of the target layer.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
void setJoinFieldName(const QString &fieldName)
Sets name of the field of joined layer that will be used for join.
bool isUsingMemoryCache() const
Returns whether values from the joined layer should be cached in memory to speed up lookups.
QString prefix() const
Returns prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
void setTargetFieldName(const QString &fieldName)
Sets name of the field of our layer that will be used for join.
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
QString joinLayerId() const
ID of the joined layer - may be used to resolve reference to the joined layer.
void setPrefix(const QString &prefix)
Sets prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
bool hasSubset(bool blocklisted=true) const
Returns true if blocklisted fields is not empty or if a subset of names has been set.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet).
QHash< QString, QgsAttributes > cachedAttributes
Cache for joined attributes to provide fast lookup (size is 0 if no memory caching).
void setJoinFieldNamesSubset(QStringList *fieldNamesSubset)
Sets the subset of fields to be used from joined layer.
Represents a vector layer which manages a vector based dataset.
bool isEditable() const final
Returns true if the provider is in editing mode.
Q_INVOKABLE bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant(), bool skipDefaultValues=false, QgsVectorLayerToolsContext *context=nullptr)
Changes an attribute value for a feature (but does not immediately commit the changes).
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
void updatedFields()
Emitted whenever the fields available from this layer have been changed.
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) final
Adds a list of features to the sink.
QMap< int, QVariant > QgsAttributeMap
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
#define QgsDebugError(str)
QList< QgsVectorLayerJoinInfo > QgsVectorJoinList
Context for cascade delete features.