QGIS API Documentation  3.6.0-Noosa (5873452)
qgsvirtuallayerdefinitionutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvirtuallayerdefinitionutils.cpp
3 begin : Jan 2016
4 copyright : (C) 2016 Hugo Mercier, Oslandia
5 email : hugo dot mercier at oslandia dot com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgsvectorlayer.h"
19 #include "qgsvectorlayerjoininfo.h"
20 #include "qgsvectordataprovider.h"
21 #include "qgsproject.h"
23 
25 {
27 
28  QStringList leftJoins;
29  QStringList columns;
30 
31  // add the geometry column if the layer is spatial
32  if ( layer->isSpatial() )
33  columns << "t.geometry";
34 
35  // look for the uid
36  QgsFields fields = layer->dataProvider()->fields();
37  {
39  if ( pk.size() == 1 )
40  {
41  def.setUid( fields.field( pk[0] ).name() );
42  }
43  else
44  {
45  // find an uid name
46  QString uid = QStringLiteral( "uid" );
47  while ( fields.lookupField( uid ) != -1 )
48  uid += QLatin1String( "_" ); // add "_" each time this name already exists
49 
50  // add a column
51  columns << "t.rowid AS " + uid;
52  def.setUid( uid );
53  }
54  }
55  const QgsFields providerFields = layer->dataProvider()->fields();
56  for ( const auto &f : providerFields )
57  {
58  columns << "t.\"" + f.name() + "\"";
59  }
60 
61  int joinIdx = 0;
62  Q_FOREACH ( const QgsVectorLayerJoinInfo &join, layer->vectorJoins() )
63  {
64  QString joinName = QStringLiteral( "j%1" ).arg( ++joinIdx );
65  QgsVectorLayer *joinedLayer = join.joinLayer();
66  if ( !joinedLayer )
67  continue;
68  QString prefix = join.prefix().isEmpty() ? joinedLayer->name() + "_" : join.prefix();
69 
70  leftJoins << QStringLiteral( "LEFT JOIN \"%1\" AS %2 ON t.\"%5\"=%2.\"%3\"" ).arg( joinedLayer->id(), joinName, join.joinFieldName(), join.targetFieldName() );
71  if ( join.joinFieldNamesSubset() )
72  {
73  Q_FOREACH ( const QString &f, *join.joinFieldNamesSubset() )
74  {
75  columns << joinName + ".\"" + f + "\" AS \"" + prefix + f + "\"";
76  }
77  }
78  else
79  {
80  const QgsFields joinFields = joinedLayer->fields();
81  for ( const QgsField &f : joinFields )
82  {
83  if ( f.name() == join.joinFieldName() )
84  continue;
85  columns << joinName + ".\"" + f.name() + "\" AS \"" + prefix + f.name() + "\"";
86  }
87  }
88  }
89 
90  QString query = "SELECT " + columns.join( QStringLiteral( ", " ) ) + " FROM \"" + layer->id() + "\" AS t " + leftJoins.join( QStringLiteral( " " ) );
91  def.setQuery( query );
92 
93  return def;
94 }
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:324
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
QString name
Definition: qgsfield.h:58
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
Container of fields for a vector layer.
Definition: qgsfields.h:42
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null if the reference was set by layer ID and not resolved yet) ...
void setQuery(const QString &query)
Sets the SQL query.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
Defines left outer join from our vector layer to some other vector layer.
static QgsVirtualLayerDefinition fromJoinedLayer(QgsVectorLayer *joinedLayer)
Gets a virtual layer definition from a vector layer where vector joins are replaced by SQL LEFT JOINs...
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blacklisted=true)
Returns the list of field names to use for joining considering blacklisted fields and subset...
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
QString name
Definition: qgsmaplayer.h:68
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer&#39;s data provider, it may be null.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
Represents a vector layer which manages a vector based data sets.
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:168
Class to manipulate the definition of a virtual layer.
QString prefix() const
Returns prefix of fields from the joined layer. If null, joined layer&#39;s name will be used...