21#include <QRegularExpression>
27using namespace Qt::StringLiterals;
40 const QString columnNameRx( u
"[a-zA-Z_\\x80-\\xFF][a-zA-Z0-9_\\x80-\\xFF]*"_s );
46 const QList<QPair<QString, QString> > items = QUrlQuery( url ).queryItems( QUrl::FullyEncoded );
47 for (
int i = 0; i < items.size(); i++ )
49 const QString key = items.at( i ).first;
50 const QString value = items.at( i ).second;
51 if ( key ==
"layer_ref"_L1 )
55 const int pos = value.indexOf(
':' );
56 QString layerId, vlayerName;
59 layerId = QUrl::fromPercentEncoding( value.toUtf8() );
60 vlayerName = u
"vtab%1"_s.arg( layerIdx );
64 layerId = QUrl::fromPercentEncoding( value.left( pos ).toUtf8() );
65 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
70 else if ( key ==
"layer"_L1 )
74 const int pos = value.indexOf(
':' );
77 QString providerKey, source, vlayerName, encoding = u
"UTF-8"_s;
79 providerKey = value.left( pos );
80 int pos2 = value.indexOf(
':', pos + 1 );
81 if ( pos2 - pos == 2 )
82 pos2 = value.indexOf(
':', pos + 3 );
85 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
86 const int pos3 = value.indexOf(
':', pos2 + 1 );
89 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
90 encoding = value.mid( pos3 + 1 );
94 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
99 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
100 vlayerName = u
"vtab%1"_s.arg( layerIdx );
103 def.
addSource( vlayerName, source, providerKey, encoding );
106 else if ( key ==
"geometry"_L1 )
110 const thread_local QRegularExpression reGeom(
"(" + columnNameRx +
")(?::([a-zA-Z0-9]+):(\\d+))?" );
111 const QRegularExpressionMatch match = reGeom.match( value );
112 if ( match.hasMatch() )
115 if ( match.capturedTexts().size() > 2 )
121 wkbType =
static_cast<Qgis::WkbType>( match.captured( 2 ).toLong() );
128 else if ( key ==
"nogeometry"_L1 )
132 else if ( key ==
"uid"_L1 )
136 else if ( key ==
"query"_L1 )
139 def.
setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
141 else if ( key ==
"field"_L1 )
144 const thread_local QRegularExpression reField(
"(" + columnNameRx +
"):(int|real|text)" );
145 const QRegularExpressionMatch match = reField.match( value );
146 if ( match.hasMatch() )
148 const QString fieldName( match.captured( 1 ) );
149 const QString fieldType( match.captured( 2 ) );
150 if ( fieldType ==
"int"_L1 )
152 fields.append(
QgsField( fieldName, QMetaType::Type::LongLong, fieldType ) );
154 else if ( fieldType ==
"real"_L1 )
156 fields.append(
QgsField( fieldName, QMetaType::Type::Double, fieldType ) );
158 if ( fieldType ==
"text"_L1 )
160 fields.append(
QgsField( fieldName, QMetaType::Type::QString, fieldType ) );
164 else if ( key ==
"lazy"_L1 )
168 else if ( key ==
"subsetstring"_L1 )
182 url = QUrl::fromLocalFile(
filePath() );
184 QUrlQuery urlQuery( url );
189 if ( l.isReferenced() )
190 urlQuery.addQueryItem( u
"layer_ref"_s, u
"%1:%2"_s.arg( l.reference(), l.name() ) );
194 urlQuery.addQueryItem(
198 .arg( l.provider(), QString( QUrl::toPercentEncoding( l.name() ) ), l.encoding(), QString( QUrl::toPercentEncoding( l.source() ) ) )
204 if ( !
query().isEmpty() )
206 urlQuery.addQueryItem( u
"query"_s,
query() );
209 if ( !
uid().isEmpty() )
210 urlQuery.addQueryItem( u
"uid"_s,
uid() );
213 urlQuery.addQueryItem( u
"nogeometry"_s, QString() );
222 const auto constFields =
fields();
223 for (
const QgsField &f : constFields )
225 if ( f.type() == QMetaType::Type::Int || f.type() == QMetaType::Type::UInt || f.type() == QMetaType::Type::Bool || f.type() == QMetaType::Type::LongLong )
226 urlQuery.addQueryItem( u
"field"_s, f.name() +
":int" );
227 else if ( f.type() == QMetaType::Type::Double )
228 urlQuery.addQueryItem( u
"field"_s, f.name() +
":real" );
229 else if ( f.type() == QMetaType::Type::QString )
230 urlQuery.addQueryItem( u
"field"_s, f.name() +
":text" );
235 urlQuery.addQueryItem( u
"lazy"_s, QString() );
240 urlQuery.addQueryItem( u
"subsetstring"_s, QUrl::toPercentEncoding(
subsetString() ) );
243 url.setQuery( urlQuery );
250 return QString(
toUrl().toEncoded() );
260 mSourceLayers.append(
SourceLayer( name, source, provider, encoding ) );
268 if ( l.name() == name )
281 if ( l.isReferenced() )
291 return mSubsetString;
WkbType
The WKB type describes the number of dimensions a geometry has.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
Either a reference to a live layer in the registry or all the parameters needed to load it (provider ...
QString geometryField() const
Gets the name of the geometry field. Empty if no geometry field.
QString query() const
Gets the SQL query.
Qgis::WkbType geometryWkbType() const
Gets the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for u...
QgsFields fields() const
Gets field definitions.
long geometrySrid() const
Gets the SRID of the geometry.
bool hasSourceLayer(const QString &name) const
Convenience method to test if a given source layer is part of the definition.
QgsVirtualLayerDefinition(const QString &filePath="")
Constructor with an optional file path.
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown).
void setSubsetString(const QString &subsetString)
Sets the subsetString.
void setLazy(bool lazy)
Sets the lazy mode.
bool hasReferencedLayers() const
Convenience method to test whether the definition has referenced (live) layers.
void setFilePath(const QString &filePath)
Sets the file path.
QString subsetString() const
Returns the subset string.
QUrl toUrl() const
Convert the definition into a QUrl.
void setGeometrySrid(long srid)
Sets the SRID of the geometry.
void addSource(const QString &name, const QString &ref)
Add a live layer source layer.
void setGeometryField(const QString &geometryField)
Sets the name of the geometry field.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Gets access to the source layers.
QString uid() const
Gets the name of the field with unique identifiers.
QString filePath() const
Gets the file path. May be empty.
static QgsVirtualLayerDefinition fromUrl(const QUrl &url)
Constructor to build a definition from a QUrl The path part of the URL is extracted as well as the fo...
QString toString() const
Converts the definition into a QString that can be read by the virtual layer provider.
void setFields(const QgsFields &fields)
Sets field definitions.
void setGeometryWkbType(Qgis::WkbType t)
Sets the type of the geometry.
void setQuery(const QString &query)
Sets the SQL query.
bool isLazy() const
Returns the lazy mode.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
QString fromEncodedComponent_helper(const QByteArray &ba)
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.