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 ) );
170 url = QUrl::fromLocalFile(
filePath() );
175 url.addQueryItem( QStringLiteral(
"layer_ref" ), QStringLiteral(
"%1:%2" ).arg( l.
reference(), l.
name() ) );
177 url.addEncodedQueryItem(
"layer", QStringLiteral(
"%1:%4:%2:%3" )
179 QString( QUrl::toPercentEncoding( l.
name() ) ),
181 QString( QUrl::toPercentEncoding( l.
source() ) ) ).toUtf8() );
184 if ( !
query().isEmpty() )
186 url.addQueryItem( QStringLiteral(
"query" ),
query() );
189 if ( !
uid().isEmpty() )
190 url.addQueryItem( QStringLiteral(
"uid" ),
uid() );
193 url.addQueryItem( QStringLiteral(
"nogeometry" ), QLatin1String(
"" ) );
199 url.addQueryItem( QStringLiteral(
"geometry" ),
geometryField() );
204 if ( f.
type() == QVariant::Int )
205 url.addQueryItem( QStringLiteral(
"field" ), f.
name() +
":int" );
206 else if ( f.
type() == QVariant::Double )
207 url.addQueryItem( QStringLiteral(
"field" ), f.
name() +
":real" );
208 else if ( f.
type() == QVariant::String )
209 url.addQueryItem( QStringLiteral(
"field" ), f.
name() +
":text" );
217 return QString(
toUrl().toEncoded() );
227 mSourceLayers.append(
SourceLayer( name, source, provider, encoding ) );
234 if ( l.
name() == name )
bool isReferenced() const
Is it a live layer or not ?
bool hasReferencedLayers() const
Convenience method to test whether the definition has referenced (live) layers.
void setFields(const QgsFields &fields)
Set field definitions.
QString name() const
Name of the layer.
QString uid() const
Get the name of the field with unique identifiers.
QString encoding() const
Optional encoding for the provider.
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...
QString source() const
The source url used by the provider to build the layer.
void addSource(const QString &name, const QString &ref)
Add a live layer source layer.
void setFilePath(const QString &filePath)
Set the file path.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
QUrl toUrl() const
Convert the definition into a QUrl.
QString filePath() const
Get the file path. May be empty.
Type
The WKB type describes the number of dimensions a geometry has.
QString reference() const
The reference (id) of the live layer.
void setQuery(const QString &query)
Set the SQL query.
void setGeometryField(const QString &geometryField)
Set the name of the geometry field.
QString geometryField() const
Get the name of the geometry field. Empty if no geometry field.
QgsFields fields() const
Get field definitions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append 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
Get access to the source layers.
void setGeometryWkbType(QgsWkbTypes::Type t)
Set 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)
Set the name of the field with unique identifiers.
void setGeometrySrid(long srid)
Set the SRID of the geometry.
QString provider() const
Provider key.
QString query() const
Get the SQL query.
QgsVirtualLayerDefinition(const QString &filePath="")
Constructor with an optional file path.
QgsWkbTypes::Type geometryWkbType() const
Get the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for un...
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
Get the SRID of the geometry.