21#include <QRegularExpression>
27using namespace Qt::StringLiterals;
41 const QString columnNameRx( u
"[a-zA-Z_\\x80-\\xFF][a-zA-Z0-9_\\x80-\\xFF]*"_s );
47 const QList<QPair<QString, QString> > items = QUrlQuery( url ).queryItems( QUrl::FullyEncoded );
48 for (
int i = 0; i < items.size(); i++ )
50 const QString key = items.at( i ).first;
51 const QString value = items.at( i ).second;
52 if ( key ==
"layer_ref"_L1 )
56 const int pos = value.indexOf(
':' );
57 QString layerId, vlayerName;
60 layerId = QUrl::fromPercentEncoding( value.toUtf8() );
61 vlayerName = u
"vtab%1"_s.arg( layerIdx );
65 layerId = QUrl::fromPercentEncoding( value.left( pos ).toUtf8() );
66 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
71 else if ( key ==
"layer"_L1 )
75 const int pos = value.indexOf(
':' );
78 QString providerKey, source, vlayerName, encoding = u
"UTF-8"_s;
80 providerKey = value.left( pos );
81 int pos2 = value.indexOf(
':', pos + 1 );
82 if ( pos2 - pos == 2 )
83 pos2 = value.indexOf(
':', pos + 3 );
86 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
87 const int pos3 = value.indexOf(
':', pos2 + 1 );
90 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
91 encoding = value.mid( pos3 + 1 );
95 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
100 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
101 vlayerName = u
"vtab%1"_s.arg( layerIdx );
104 def.
addSource( vlayerName, source, providerKey, encoding );
107 else if ( key ==
"geometry"_L1 )
111 const thread_local QRegularExpression reGeom(
"(" + columnNameRx +
")(?::([a-zA-Z0-9]+):(\\d+))?" );
112 const QRegularExpressionMatch match = reGeom.match( value );
113 if ( match.hasMatch() )
116 if ( match.capturedTexts().size() > 2 )
122 wkbType =
static_cast<Qgis::WkbType>( match.captured( 2 ).toLong() );
129 else if ( key ==
"nogeometry"_L1 )
133 else if ( key ==
"uid"_L1 )
137 else if ( key ==
"query"_L1 )
140 def.
setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
142 else if ( key ==
"field"_L1 )
145 const thread_local QRegularExpression reField(
"(" + columnNameRx +
"):(int|real|text)" );
146 const QRegularExpressionMatch match = reField.match( value );
147 if ( match.hasMatch() )
149 const QString fieldName( match.captured( 1 ) );
150 const QString fieldType( match.captured( 2 ) );
151 if ( fieldType ==
"int"_L1 )
153 fields.append(
QgsField( fieldName, QMetaType::Type::LongLong, fieldType ) );
155 else if ( fieldType ==
"real"_L1 )
157 fields.append(
QgsField( fieldName, QMetaType::Type::Double, fieldType ) );
159 if ( fieldType ==
"text"_L1 )
161 fields.append(
QgsField( fieldName, QMetaType::Type::QString, fieldType ) );
165 else if ( key ==
"lazy"_L1 )
169 else if ( key ==
"subsetstring"_L1 )
183 url = QUrl::fromLocalFile(
filePath() );
185 QUrlQuery urlQuery( url );
190 if ( l.isReferenced() )
191 urlQuery.addQueryItem( u
"layer_ref"_s, u
"%1:%2"_s.arg( l.reference(), l.name() ) );
198 QString( QUrl::toPercentEncoding( l.name() ) ),
200 QString( QUrl::toPercentEncoding( l.source() ) ) ).toUtf8() ) );
203 if ( !
query().isEmpty() )
205 urlQuery.addQueryItem( u
"query"_s,
query() );
208 if ( !
uid().isEmpty() )
209 urlQuery.addQueryItem( u
"uid"_s,
uid() );
212 urlQuery.addQueryItem( u
"nogeometry"_s, QString() );
221 const auto constFields =
fields();
222 for (
const QgsField &f : constFields )
224 if ( f.type() == QMetaType::Type::Int
225 || f.type() == QMetaType::Type::UInt
226 || f.type() == QMetaType::Type::Bool
227 || f.type() == QMetaType::Type::LongLong )
228 urlQuery.addQueryItem( u
"field"_s, f.name() +
":int" );
229 else if ( f.type() == QMetaType::Type::Double )
230 urlQuery.addQueryItem( u
"field"_s, f.name() +
":real" );
231 else if ( f.type() == QMetaType::Type::QString )
232 urlQuery.addQueryItem( u
"field"_s, f.name() +
":text" );
237 urlQuery.addQueryItem( u
"lazy"_s, QString() );
242 urlQuery.addQueryItem( u
"subsetstring"_s, QUrl::toPercentEncoding(
subsetString() ) );
245 url.setQuery( urlQuery );
252 return QString(
toUrl().toEncoded() );
262 mSourceLayers.append(
SourceLayer( name, source, provider, encoding ) );
270 if ( l.name() == name )
283 if ( l.isReferenced() )
293 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.