QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
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:237
QgsFields fields
Definition: qgsfeature.h:66
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:317
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:210
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:170
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:302
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QString name
Definition: qgsfield.h:60
Container of fields for a vector layer.
Definition: qgsfields.h:45
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
Defines left outer join from our vector layer to some other vector 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.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet)
bool hasSubset(bool blocklisted=true) const
Returns true if blocklisted fields is not empty or if a subset of names has been set.
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
const QgsField & field
Definition: qgsfield.h:463