QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
6  email : [email protected]
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 
18 #include "qgsvectorlayerjoininfo.h"
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 
86 QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blocklisted )
87 {
88  QStringList fieldNames;
89 
90  if ( blocklisted && !info.joinFieldNamesBlockList().isEmpty() )
91  {
92  QStringList *lst = info.joinFieldNamesSubset();
93  if ( lst )
94  {
95  for ( const QString &s : std::as_const( *lst ) )
96  {
97  if ( !info.joinFieldNamesBlockList().contains( s ) )
98  fieldNames.append( s );
99  }
100  }
101  else
102  {
103  if ( auto *lJoinLayer = info.joinLayer() )
104  {
105  const QgsFields fields { lJoinLayer->fields() };
106  for ( const QgsField &f : fields )
107  {
108  if ( !info.joinFieldNamesBlockList().contains( f.name() )
109  && f.name() != info.joinFieldName() )
110  fieldNames.append( f.name() );
111  }
112  }
113  }
114  }
115  else
116  {
117  QStringList *lst = info.joinFieldNamesSubset();
118  if ( lst )
119  {
120  fieldNames = *lst;
121  }
122  }
123 
124  return fieldNames;
125 }
126 
127 bool QgsVectorLayerJoinInfo::hasSubset( bool blocklisted ) const
128 {
129  bool subset = joinFieldNamesSubset();
130 
131  if ( blocklisted )
132  subset |= !joinFieldNamesBlockList().isEmpty();
133 
134  return subset;
135 }
QgsVectorLayerJoinInfo::mEditable
bool mEditable
Definition: qgsvectorlayerjoininfo.h:268
QgsFeature::initAttributes
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:235
QgsVectorLayerJoinInfo::setEditable
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
Definition: qgsvectorlayerjoininfo.cpp:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsVectorLayerJoinInfo::joinFieldNamesSubset
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
Definition: qgsvectorlayerjoininfo.h:236
QgsVectorLayerJoinInfo::joinFieldNamesSubset
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blocklisted=true)
Returns the list of field names to use for joining considering blocklisted fields and subset.
Definition: qgsvectorlayerjoininfo.cpp:86
QgsVectorLayerJoinInfo::setUpsertOnEdit
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
Definition: qgsvectorlayerjoininfo.h:128
field
const QgsField & field
Definition: qgsfield.h:463
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsVectorLayerJoinInfo::prefixedFieldName
QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
Definition: qgsvectorlayerjoininfo.cpp:21
QgsField::name
QString name
Definition: qgsfield.h:60
QgsFeature::fieldNameIndex
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:342
qgsvectorlayerjoininfo.h
QgsVectorLayerJoinInfo::targetFieldName
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
Definition: qgsvectorlayerjoininfo.h:68
QgsVectorLayerJoinInfo::prefix
QString prefix() const
Returns prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
Definition: qgsvectorlayerjoininfo.h:78
QgsVectorLayerJoinInfo::mUpsertOnEdit
bool mUpsertOnEdit
Definition: qgsvectorlayerjoininfo.h:270
QgsFeature::setFields
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
QgsVectorLayerJoinInfo::joinFieldName
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
Definition: qgsvectorlayerjoininfo.h:73
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
QgsVectorLayerJoinInfo
Defines left outer join from our vector layer to some other vector layer. The join is done based on [...
Definition: qgsvectorlayerjoininfo.h:33
QgsVectorLayerJoinInfo::setUsingMemoryCache
void setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
Definition: qgsvectorlayerjoininfo.cpp:38
qgsvectorlayer.h
QgsFeature::setAttribute
bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Definition: qgsfeature.cpp:262
QgsVectorLayerJoinInfo::joinLayer
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet)
Definition: qgsvectorlayerjoininfo.h:58
QgsVectorLayerJoinInfo::joinFieldNamesBlockList
QStringList joinFieldNamesBlockList() const
Returns the list of fields to ignore.
Definition: qgsvectorlayerjoininfo.h:189
QgsVectorLayerJoinInfo::extractJoinedFeature
QgsFeature extractJoinedFeature(const QgsFeature &feature) const
Extract the join feature from the target feature for the current join layer information.
Definition: qgsvectorlayerjoininfo.cpp:62
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:70
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerJoinInfo::isUsingMemoryCache
bool isUsingMemoryCache() const
Returns whether values from the joined layer should be cached in memory to speed up lookups.
Definition: qgsvectorlayerjoininfo.cpp:43
QgsVectorLayerJoinInfo::mMemoryCache
bool mMemoryCache
True if the join is cached in virtual memory.
Definition: qgsvectorlayerjoininfo.h:253
QgsVectorLayerJoinInfo::hasSubset
bool hasSubset(bool blocklisted=true) const
Returns true if blocklisted fields is not empty or if a subset of names has been set.
Definition: qgsvectorlayerjoininfo.cpp:127
QgsVectorLayerJoinInfo::setCascadedDelete
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
Definition: qgsvectorlayerjoininfo.h:142
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50