27using namespace Qt::StringLiterals;
33 QStringList leftJoins;
38 columns <<
"t.geometry";
51 QString uid = u
"uid"_s;
56 columns <<
"t.rowid AS " + uid;
61 for (
const auto &f : providerFields )
63 columns <<
"t.\"" + f.name() +
"\"";
70 const QString joinName = u
"j%1"_s.arg( ++joinIdx );
74 const QString prefix = join.prefix().isEmpty() ? joinedLayer->
name() +
"_" : join.prefix();
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() )
79 const QStringList joinFieldNamesSubset { *lJoinFieldNamesSubset };
80 for (
const QString &f : joinFieldNamesSubset )
82 columns << joinName +
".\"" + f +
"\" AS \"" + prefix + f +
"\"";
88 for (
const QgsField &f : joinFields )
90 if ( f.name() == join.joinFieldName() )
92 columns << joinName +
".\"" + f.name() +
"\" AS \"" + prefix + f.name() +
"\"";
97 const QString query =
"SELECT " + columns.join(
", "_L1 ) +
" FROM \"" + layer->
id() +
"\" AS t " + leftJoins.join(
' '_L1 );
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
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.
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