QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
26{
28
29 QStringList leftJoins;
30 QStringList columns;
31
32 // add the geometry column if the layer is spatial
33 if ( layer->isSpatial() )
34 columns << "t.geometry";
35
36 // look for the uid
37 const QgsFields fields = layer->dataProvider()->fields();
38 {
40 if ( pk.size() == 1 )
41 {
42 def.setUid( fields.field( pk[0] ).name() );
43 }
44 else
45 {
46 // find an uid name
47 QString uid = QStringLiteral( "uid" );
48 while ( fields.lookupField( uid ) != -1 )
49 uid += QLatin1Char( '_' ); // add "_" each time this name already exists
50
51 // add a column
52 columns << "t.rowid AS " + uid;
53 def.setUid( uid );
54 }
55 }
56 const QgsFields providerFields = layer->dataProvider()->fields();
57 for ( const auto &f : providerFields )
58 {
59 columns << "t.\"" + f.name() + "\"";
60 }
61
62 int joinIdx = 0;
63 const auto constVectorJoins = layer->vectorJoins();
64 for ( const QgsVectorLayerJoinInfo &join : constVectorJoins )
65 {
66 const QString joinName = QStringLiteral( "j%1" ).arg( ++joinIdx );
67 QgsVectorLayer *joinedLayer = join.joinLayer();
68 if ( !joinedLayer )
69 continue;
70 const QString prefix = join.prefix().isEmpty() ? joinedLayer->name() + "_" : join.prefix();
71
72 leftJoins << QStringLiteral( "LEFT JOIN \"%1\" AS %2 ON t.\"%5\"=%2.\"%3\"" ).arg( joinedLayer->id(), joinName, join.joinFieldName(), join.targetFieldName() );
73 if ( auto *lJoinFieldNamesSubset = join.joinFieldNamesSubset() )
74 {
75 const QStringList joinFieldNamesSubset { *lJoinFieldNamesSubset };
76 for ( const QString &f : joinFieldNamesSubset )
77 {
78 columns << joinName + ".\"" + f + "\" AS \"" + prefix + f + "\"";
79 }
80 }
81 else
82 {
83 const QgsFields joinFields = joinedLayer->fields();
84 for ( const QgsField &f : joinFields )
85 {
86 if ( f.name() == join.joinFieldName() )
87 continue;
88 columns << joinName + ".\"" + f.name() + "\" AS \"" + prefix + f.name() + "\"";
89 }
90 }
91 }
92
93 const QString query = "SELECT " + columns.join( QLatin1String( ", " ) ) + " FROM \"" + layer->id() + "\" AS t " + leftJoins.join( QLatin1Char( ' ' ) );
94 def.setQuery( query );
95
96 return def;
97}
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QString name
Definition qgsfield.h:63
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:84
QString id
Definition qgsmaplayer.h:83
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:28