19 #include <QStringList> 26 : mFilePath( filePath )
37 const QString columnNameRx( QStringLiteral(
"[a-zA-Z_\x80-\xFF][a-zA-Z0-9_\x80-\xFF]*" ) );
42 QList<QPair<QByteArray, QByteArray> > items = url.encodedQueryItems();
43 for (
int i = 0; i < items.size(); i++ )
45 QString key = items.
at( i ).first;
46 QString value = items.at( i ).second;
47 if ( key == QLatin1String(
"layer_ref" ) )
51 int pos = value.indexOf(
':' );
52 QString layerId, vlayerName;
56 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
60 layerId = value.left( pos );
61 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
66 else if ( key == QLatin1String(
"layer" ) )
70 int pos = value.indexOf(
':' );
73 QString providerKey, source, vlayerName, encoding = QStringLiteral(
"UTF-8" );
75 providerKey = value.left( pos );
76 int pos2 = value.indexOf(
':', pos + 1 );
77 if ( pos2 - pos == 2 )
78 pos2 = value.indexOf(
':', pos + 3 );
81 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
82 int pos3 = value.indexOf(
':', pos2 + 1 );
85 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
86 encoding = value.mid( pos3 + 1 );
90 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
95 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
96 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
99 def.
addSource( vlayerName, source, providerKey, encoding );
102 else if ( key == QLatin1String(
"geometry" ) )
106 QRegExp reGeom(
"(" + columnNameRx +
")(?::([a-zA-Z0-9]+):(\\d+))?" );
107 int pos = reGeom.indexIn( value );
111 if ( reGeom.captureCount() > 1 )
124 else if ( key == QLatin1String(
"nogeometry" ) )
128 else if ( key == QLatin1String(
"uid" ) )
132 else if ( key == QLatin1String(
"query" ) )
135 def.
setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
137 else if ( key == QLatin1String(
"field" ) )
140 QRegExp reField(
"(" + columnNameRx +
"):(int|real|text)" );
141 int pos = reField.indexIn( value );
144 QString fieldName( reField.cap( 1 ) );
145 QString fieldType( reField.cap( 2 ) );
146 if ( fieldType == QLatin1String(
"int" ) )
150 else if ( fieldType == QLatin1String(
"real" ) )
152 fields.
append(
QgsField( fieldName, QVariant::Double, fieldType ) );
154 if ( fieldType == QLatin1String(
"text" ) )
156 fields.
append(
QgsField( fieldName, QVariant::String, fieldType ) );
160 else if ( key == QLatin1String(
"lazy" ) )
174 url = QUrl::fromLocalFile(
filePath() );
179 if ( l.isReferenced() )
180 url.addQueryItem( QStringLiteral(
"layer_ref" ), QStringLiteral(
"%1:%2" ).arg( l.reference(), l.name() ) );
182 url.addEncodedQueryItem(
"layer", QStringLiteral(
"%1:%4:%2:%3" )
184 QString( QUrl::toPercentEncoding( l.name() ) ),
186 QString( QUrl::toPercentEncoding( l.source() ) ) ).toUtf8() );
189 if ( !
query().isEmpty() )
191 url.addQueryItem( QStringLiteral(
"query" ),
query() );
194 if ( !
uid().isEmpty() )
195 url.addQueryItem( QStringLiteral(
"uid" ),
uid() );
198 url.addQueryItem( QStringLiteral(
"nogeometry" ), QString() );
204 url.addQueryItem( QStringLiteral(
"geometry" ),
geometryField() );
207 const auto constFields =
fields();
208 for (
const QgsField &f : constFields )
210 if ( f.type() == QVariant::Int )
211 url.addQueryItem( QStringLiteral(
"field" ), f.name() +
":int" );
212 else if ( f.type() == QVariant::Double )
213 url.addQueryItem( QStringLiteral(
"field" ), f.name() +
":real" );
214 else if ( f.type() == QVariant::String )
215 url.addQueryItem( QStringLiteral(
"field" ), f.name() +
":text" );
220 url.addQueryItem( QStringLiteral(
"lazy" ), QString() );
228 return QString(
toUrl().toEncoded() );
238 mSourceLayers.append(
SourceLayer( name, source, provider, encoding ) );
246 if ( l.name() == name )
259 if ( l.isReferenced() )
bool hasReferencedLayers() const
Convenience method to test whether the definition has referenced (live) layers.
void setFields(const QgsFields &fields)
Sets field definitions.
QString uid() const
Gets the name of the field with unique identifiers.
Container of fields for a vector layer.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
QString toString() const
Convert into a QString that can be read by the virtual layer provider.
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...
void addSource(const QString &name, const QString &ref)
Add a live layer source layer.
void setFilePath(const QString &filePath)
Sets the file path.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QUrl toUrl() const
Convert the definition into a QUrl.
QString filePath() const
Gets the file path. May be empty.
Type
The WKB type describes the number of dimensions a geometry has.
void setQuery(const QString &query)
Sets the SQL query.
void setGeometryField(const QString &geometryField)
Sets the name of the geometry field.
bool isLazy() const
Returns the lazy mode.
QString geometryField() const
Gets the name of the geometry field. Empty if no geometry field.
QgsFields fields() const
Gets field definitions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
Encapsulate a field in an attribute table or data source.
bool hasSourceLayer(const QString &name) const
Convenience method to test if a given source layer is part of the definition.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Gets access to the source layers.
void setGeometryWkbType(QgsWkbTypes::Type t)
Sets the type of the geometry.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown) ...
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
void setGeometrySrid(long srid)
Sets the SRID of the geometry.
QString query() const
Gets the SQL query.
void setLazy(bool lazy)
Sets the lazy mode.
QgsVirtualLayerDefinition(const QString &filePath="")
Constructor with an optional file path.
QgsWkbTypes::Type geometryWkbType() const
Gets the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for u...
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.
long geometrySrid() const
Gets the SRID of the geometry.