QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsvirtuallayerdefinitionutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvirtuallayerdefinitionutils.cpp
3begin : Jan 2016
4copyright : (C) 2016 Hugo Mercier, Oslandia
5email : 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
19#include "qgsproject.h"
21#include "qgsvectorlayer.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30{
32
33 QStringList leftJoins;
34 QStringList columns;
35
36 // add the geometry column if the layer is spatial
37 if ( layer->isSpatial() )
38 columns << "t.geometry";
39
40 // look for the uid
41 const QgsFields fields = layer->dataProvider()->fields();
42 {
44 if ( pk.size() == 1 )
45 {
46 def.setUid( fields.field( pk[0] ).name() );
47 }
48 else
49 {
50 // find an uid name
51 QString uid = u"uid"_s;
52 while ( fields.lookupField( uid ) != -1 )
53 uid += '_'_L1; // add "_" each time this name already exists
54
55 // add a column
56 columns << "t.rowid AS " + uid;
57 def.setUid( uid );
58 }
59 }
60 const QgsFields providerFields = layer->dataProvider()->fields();
61 for ( const auto &f : providerFields )
62 {
63 columns << "t.\"" + f.name() + "\"";
64 }
65
66 int joinIdx = 0;
67 const auto constVectorJoins = layer->vectorJoins();
68 for ( const QgsVectorLayerJoinInfo &join : constVectorJoins )
69 {
70 const QString joinName = u"j%1"_s.arg( ++joinIdx );
71 QgsVectorLayer *joinedLayer = join.joinLayer();
72 if ( !joinedLayer )
73 continue;
74 const QString prefix = join.prefix().isEmpty() ? joinedLayer->name() + "_" : join.prefix();
75
76 leftJoins << u"LEFT JOIN \"%1\" AS %2 ON t.\"%5\"=%2.\"%3\""_s.arg( joinedLayer->id(), joinName, join.joinFieldName(), join.targetFieldName() );
77 if ( auto *lJoinFieldNamesSubset = join.joinFieldNamesSubset() )
78 {
79 const QStringList joinFieldNamesSubset { *lJoinFieldNamesSubset };
80 for ( const QString &f : joinFieldNamesSubset )
81 {
82 columns << joinName + ".\"" + f + "\" AS \"" + prefix + f + "\"";
83 }
84 }
85 else
86 {
87 const QgsFields joinFields = joinedLayer->fields();
88 for ( const QgsField &f : joinFields )
89 {
90 if ( f.name() == join.joinFieldName() )
91 continue;
92 columns << joinName + ".\"" + f.name() + "\" AS \"" + prefix + f.name() + "\"";
93 }
94 }
95 }
96
97 const QString query = "SELECT " + columns.join( ", "_L1 ) + " FROM \"" + layer->id() + "\" AS t " + leftJoins.join( ' '_L1 );
98 def.setQuery( query );
99
100 return def;
101}
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QString name
Definition qgsfield.h:65
Container of fields for a vector layer.
Definition qgsfields.h:46
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QString name
Definition qgsmaplayer.h:87
QString id
Definition qgsmaplayer.h:86
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
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.
Represents a vector layer which manages a vector based dataset.
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
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
static QgsVirtualLayerDefinition fromJoinedLayer(QgsVectorLayer *joinedLayer)
Gets a virtual layer definition from a vector layer where vector joins are replaced by SQL LEFT JOINs...
Manipulates the definition of a virtual layer.
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
void setQuery(const QString &query)
Sets the SQL query.
QList< int > QgsAttributeList
Definition qgsfield.h:30