21#include <QRegularExpression>
27 : mFilePath( filePath )
38 const QString columnNameRx( QStringLiteral(
"[a-zA-Z_\\x80-\\xFF][a-zA-Z0-9_\\x80-\\xFF]*" ) );
44 const QList<QPair<QString, QString> > items = QUrlQuery( url ).queryItems( QUrl::FullyEncoded );
45 for (
int i = 0; i < items.size(); i++ )
47 const QString key = items.
at( i ).first;
48 const QString value = items.at( i ).second;
49 if ( key == QLatin1String(
"layer_ref" ) )
53 const int pos = value.indexOf(
':' );
54 QString layerId, vlayerName;
57 layerId = QUrl::fromPercentEncoding( value.toUtf8() );
58 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
62 layerId = QUrl::fromPercentEncoding( value.left( pos ).toUtf8() );
63 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
68 else if ( key == QLatin1String(
"layer" ) )
72 const int pos = value.indexOf(
':' );
75 QString providerKey, source, vlayerName, encoding = QStringLiteral(
"UTF-8" );
77 providerKey = value.left( pos );
78 int pos2 = value.indexOf(
':', pos + 1 );
79 if ( pos2 - pos == 2 )
80 pos2 = value.indexOf(
':', pos + 3 );
83 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
84 const int pos3 = value.indexOf(
':', pos2 + 1 );
87 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
88 encoding = value.mid( pos3 + 1 );
92 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
97 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
98 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
101 def.
addSource( vlayerName, source, providerKey, encoding );
104 else if ( key == QLatin1String(
"geometry" ) )
108 const thread_local QRegularExpression reGeom(
"(" + columnNameRx +
")(?::([a-zA-Z0-9]+):(\\d+))?" );
109 const QRegularExpressionMatch match = reGeom.match( value );
110 if ( match.hasMatch() )
113 if ( match.capturedTexts().size() > 2 )
119 wkbType =
static_cast<Qgis::WkbType>( match.captured( 2 ).toLong() );
126 else if ( key == QLatin1String(
"nogeometry" ) )
130 else if ( key == QLatin1String(
"uid" ) )
134 else if ( key == QLatin1String(
"query" ) )
137 def.
setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
139 else if ( key == QLatin1String(
"field" ) )
142 const thread_local QRegularExpression reField(
"(" + columnNameRx +
"):(int|real|text)" );
143 const QRegularExpressionMatch match = reField.match( value );
144 if ( match.hasMatch() )
146 const QString fieldName( match.captured( 1 ) );
147 const QString fieldType( match.captured( 2 ) );
148 if ( fieldType == QLatin1String(
"int" ) )
152 else if ( fieldType == QLatin1String(
"real" ) )
156 if ( fieldType == QLatin1String(
"text" ) )
162 else if ( key == QLatin1String(
"lazy" ) )
166 else if ( key == QLatin1String(
"subsetstring" ) )
180 url = QUrl::fromLocalFile(
filePath() );
182 QUrlQuery urlQuery( url );
187 if ( l.isReferenced() )
188 urlQuery.addQueryItem( QStringLiteral(
"layer_ref" ), QStringLiteral(
"%1:%2" ).arg( l.reference(), l.name() ) );
195 QString( QUrl::toPercentEncoding( l.name() ) ),
197 QString( QUrl::toPercentEncoding( l.source() ) ) ).toUtf8() ) );
200 if ( !
query().isEmpty() )
202 urlQuery.addQueryItem( QStringLiteral(
"query" ),
query() );
205 if ( !
uid().isEmpty() )
206 urlQuery.addQueryItem( QStringLiteral(
"uid" ),
uid() );
209 urlQuery.addQueryItem( QStringLiteral(
"nogeometry" ), QString() );
215 urlQuery.addQueryItem( QStringLiteral(
"geometry" ),
geometryField() );
218 const auto constFields =
fields();
219 for (
const QgsField &f : constFields )
221 if ( f.type() == QMetaType::Type::Int
222 || f.type() == QMetaType::Type::UInt
223 || f.type() == QMetaType::Type::Bool
224 || f.type() == QMetaType::Type::LongLong )
225 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":int" );
226 else if ( f.type() == QMetaType::Type::Double )
227 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":real" );
228 else if ( f.type() == QMetaType::Type::QString )
229 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":text" );
234 urlQuery.addQueryItem( QStringLiteral(
"lazy" ), QString() );
239 urlQuery.addQueryItem( QStringLiteral(
"subsetstring" ), QUrl::toPercentEncoding(
subsetString() ) );
242 url.setQuery( urlQuery );
249 return QString(
toUrl().toEncoded() );
259 mSourceLayers.append(
SourceLayer( name, source, provider, encoding ) );
267 if ( l.name() == name )
280 if ( l.isReferenced() )
290 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.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
A SourceLayer is either a reference to a live layer in the registry or all the parameters needed to l...
Class to manipulate the definition of a virtual layer.
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
Convert 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.