QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsvectorlayerjoininfo.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerjoininfo.cpp
3 --------------------------
4 begin : Jun 29, 2017
5 copyright : (C) 2017 by Paul Blottiere
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvectorlayer.h"
20
22{
23 QString name;
24
25 if ( auto *lJoinLayer = joinLayer() )
26 {
27 if ( prefix().isNull() )
28 name = lJoinLayer->name() + '_';
29 else
30 name = prefix();
31
32 name += f.name();
33 }
34
35 return name;
36}
37
39{
40 mMemoryCache = enabled;
41}
42
44{
45 if ( mUpsertOnEdit )
46 return false;
47
48 return mMemoryCache;
49}
50
52{
53 mEditable = enabled;
54
55 if ( ! mEditable )
56 {
57 setCascadedDelete( false );
58 setUpsertOnEdit( false );
59 }
60}
61
63{
64 QgsFeature joinFeature;
65
66 if ( auto *lJoinLayer = joinLayer() )
67 {
68 const QVariant idFieldValue = feature.attribute( targetFieldName() );
69 joinFeature.initAttributes( lJoinLayer->fields().count() );
70 joinFeature.setFields( lJoinLayer->fields() );
71 joinFeature.setAttribute( joinFieldName(), idFieldValue );
72
73 const QgsFields joinFields = joinFeature.fields();
74 for ( const auto &field : joinFields )
75 {
76 const QString prefixedName = prefixedFieldName( field );
77
78 if ( feature.fieldNameIndex( prefixedName ) != -1 )
79 joinFeature.setAttribute( field.name(), feature.attribute( prefixedName ) );
80 }
81 }
82
83 return joinFeature;
84}
85
86QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blocklisted )
87{
88 return joinFieldNamesSubset( info, info.joinLayer() ? info.joinLayer()->fields() : QgsFields(), blocklisted );
89}
90
91QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, const QgsFields &joinLayerFields, bool blocklisted )
92{
93 QStringList fieldNames;
94
95 if ( blocklisted && !info.joinFieldNamesBlockList().isEmpty() )
96 {
97 QStringList *lst = info.joinFieldNamesSubset();
98 if ( lst )
99 {
100 for ( const QString &s : std::as_const( *lst ) )
101 {
102 if ( !info.joinFieldNamesBlockList().contains( s ) )
103 fieldNames.append( s );
104 }
105 }
106 else
107 {
108 for ( const QgsField &f : joinLayerFields )
109 {
110 if ( !info.joinFieldNamesBlockList().contains( f.name() )
111 && f.name() != info.joinFieldName() )
112 fieldNames.append( f.name() );
113 }
114 }
115 }
116 else
117 {
118 QStringList *lst = info.joinFieldNamesSubset();
119 if ( lst )
120 {
121 fieldNames = *lst;
122 }
123 }
124
125 return fieldNames;
126}
127
128bool QgsVectorLayerJoinInfo::hasSubset( bool blocklisted ) const
129{
130 bool subset = joinFieldNamesSubset();
131
132 if ( blocklisted )
133 subset |= !joinFieldNamesBlockList().isEmpty();
134
135 return subset;
136}
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Definition: qgsfeature.cpp:262
QgsFields fields
Definition: qgsfeature.h:66
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:350
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:235
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
Definition: qgsfeature.cpp:195
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:335
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
QString name
Definition: qgsfield.h:62
Container of fields for a vector layer.
Definition: qgsfields.h:45
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 setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
QgsFeature extractJoinedFeature(const QgsFeature &feature) const
Extract the join feature from the target feature for the current join layer information.
bool mMemoryCache
True if the join is cached in virtual memory.
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
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.
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blocklisted=true)
Returns the list of field names to use for joining considering blocklisted fields and subset.
QStringList joinFieldNamesBlockList() const
Returns the list of fields to ignore.
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.
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)
QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
QgsFields fields() const FINAL
Returns the list of fields of this layer.