|
| 1 | +/*************************************************************************** |
| 2 | + qgsvirtuallayerdefinition.cpp |
| 3 | +begin : December 2015 |
| 4 | +copyright : (C) 2015 Hugo Mercier, Oslandia |
| 5 | +email : hugo dot mercier at oslandia dot com |
| 6 | + ***************************************************************************/ |
| 7 | + |
| 8 | +/*************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +#include <QUrl> |
| 18 | +#include <QRegExp> |
| 19 | +#include <QStringList> |
| 20 | + |
| 21 | +#include "qgsvirtuallayerdefinition.h" |
| 22 | + |
| 23 | +QgsVirtualLayerDefinition::QgsVirtualLayerDefinition( const QString& filePath ) : |
| 24 | + mFilePath( filePath ), |
| 25 | + mGeometryWkbType( QgsWKBTypes::Unknown ), |
| 26 | + mGeometrySrid( 0 ) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +QgsVirtualLayerDefinition QgsVirtualLayerDefinition::fromUrl( const QUrl& url ) |
| 31 | +{ |
| 32 | + QgsVirtualLayerDefinition def; |
| 33 | + |
| 34 | + def.setFilePath( url.path() ); |
| 35 | + |
| 36 | + // regexp for column name |
| 37 | + const QString columnNameRx( "[a-zA-Z_\x80-\xFF][a-zA-Z0-9_\x80-\xFF]*" ); |
| 38 | + |
| 39 | + QgsFields fields; |
| 40 | + |
| 41 | + int layerIdx = 0; |
| 42 | + QList<QPair<QString, QString> > items = url.queryItems(); |
| 43 | + for ( int i = 0; i < items.size(); i++ ) |
| 44 | + { |
| 45 | + QString key = items.at( i ).first; |
| 46 | + QString value = items.at( i ).second; |
| 47 | + if ( key == "layer_ref" ) |
| 48 | + { |
| 49 | + layerIdx++; |
| 50 | + // layer id, with optional layer_name |
| 51 | + int pos = value.indexOf( ':' ); |
| 52 | + QString layerId, vlayerName; |
| 53 | + if ( pos == -1 ) |
| 54 | + { |
| 55 | + layerId = value; |
| 56 | + vlayerName = QString( "vtab%1" ).arg( layerIdx ); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + layerId = value.left( pos ); |
| 61 | + vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() ); |
| 62 | + } |
| 63 | + // add the layer to the list |
| 64 | + def.addSource( vlayerName, layerId ); |
| 65 | + } |
| 66 | + else if ( key == "layer" ) |
| 67 | + { |
| 68 | + layerIdx++; |
| 69 | + // syntax: layer=provider:url_encoded_source_URI(:name(:encoding)?)? |
| 70 | + int pos = value.indexOf( ':' ); |
| 71 | + if ( pos != -1 ) |
| 72 | + { |
| 73 | + QString providerKey, source, vlayerName, encoding = "UTF-8"; |
| 74 | + |
| 75 | + providerKey = value.left( pos ); |
| 76 | + int pos2 = value.indexOf( ':', pos + 1 ); |
| 77 | + if ( pos2 != -1 ) |
| 78 | + { |
| 79 | + source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() ); |
| 80 | + int pos3 = value.indexOf( ':', pos2 + 1 ); |
| 81 | + if ( pos3 != -1 ) |
| 82 | + { |
| 83 | + vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() ); |
| 84 | + encoding = value.mid( pos3 + 1 ); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() ); |
| 89 | + } |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() ); |
| 94 | + vlayerName = QString( "vtab%1" ).arg( layerIdx ); |
| 95 | + } |
| 96 | + |
| 97 | + def.addSource( vlayerName, source, providerKey, encoding ); |
| 98 | + } |
| 99 | + } |
| 100 | + else if ( key == "geometry" ) |
| 101 | + { |
| 102 | + // geometry field definition, optional |
| 103 | + // geometry_column(:wkb_type:srid)? |
| 104 | + QRegExp reGeom( "(" + columnNameRx + ")(?::([a-zA-Z0-9]+):(\\d+))?" ); |
| 105 | + int pos = reGeom.indexIn( value ); |
| 106 | + if ( pos >= 0 ) |
| 107 | + { |
| 108 | + def.setGeometryField( reGeom.cap( 1 ) ); |
| 109 | + if ( reGeom.captureCount() > 1 ) |
| 110 | + { |
| 111 | + // not used by the spatialite provider for now ... |
| 112 | + QgsWKBTypes::Type wkbType = QgsWKBTypes::parseType( reGeom.cap( 2 ) ); |
| 113 | + if ( wkbType == QgsWKBTypes::Unknown ) |
| 114 | + { |
| 115 | + wkbType = static_cast<QgsWKBTypes::Type>( reGeom.cap( 2 ).toLong() ); |
| 116 | + } |
| 117 | + def.setGeometryWkbType( wkbType ); |
| 118 | + def.setGeometrySrid( reGeom.cap( 3 ).toLong() ); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + else if ( key == "nogeometry" ) |
| 123 | + { |
| 124 | + def.setGeometryWkbType( QgsWKBTypes::NoGeometry ); |
| 125 | + } |
| 126 | + else if ( key == "uid" ) |
| 127 | + { |
| 128 | + def.setUid( value ); |
| 129 | + } |
| 130 | + else if ( key == "query" ) |
| 131 | + { |
| 132 | + // url encoded query |
| 133 | + def.setQuery( value ); |
| 134 | + } |
| 135 | + else if ( key == "field" ) |
| 136 | + { |
| 137 | + // field_name:type (int, real, text) |
| 138 | + QRegExp reField( "(" + columnNameRx + "):(int|real|text)" ); |
| 139 | + int pos = reField.indexIn( value ); |
| 140 | + if ( pos >= 0 ) |
| 141 | + { |
| 142 | + QString fieldName( reField.cap( 1 ) ); |
| 143 | + QString fieldType( reField.cap( 2 ) ); |
| 144 | + if ( fieldType == "int" ) |
| 145 | + { |
| 146 | + fields.append( QgsField( fieldName, QVariant::Int, fieldType ) ); |
| 147 | + } |
| 148 | + else if ( fieldType == "real" ) |
| 149 | + { |
| 150 | + fields.append( QgsField( fieldName, QVariant::Double, fieldType ) ); |
| 151 | + } |
| 152 | + if ( fieldType == "text" ) |
| 153 | + { |
| 154 | + fields.append( QgsField( fieldName, QVariant::String, fieldType ) ); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + def.setFields( fields ); |
| 160 | + |
| 161 | + return def; |
| 162 | +} |
| 163 | + |
| 164 | +QUrl QgsVirtualLayerDefinition::toUrl() const |
| 165 | +{ |
| 166 | + QUrl url; |
| 167 | + url.setPath( filePath() ); |
| 168 | + |
| 169 | + foreach ( const QgsVirtualLayerDefinition::SourceLayer& l, sourceLayers() ) |
| 170 | + { |
| 171 | + if ( l.isReferenced() ) |
| 172 | + url.addQueryItem( "layer_ref", QString( "%1:%2" ).arg( l.reference() ).arg( l.name() ) ); |
| 173 | + else |
| 174 | + url.addQueryItem( "layer", QString( "%1:%4:%2:%3" ) // the order is important, since the 4th argument may contain '%2' as well |
| 175 | + .arg( l.provider() ) |
| 176 | + .arg( QString( QUrl::toPercentEncoding( l.name() ) ) ) |
| 177 | + .arg( l.encoding() ) |
| 178 | + .arg( QString( QUrl::toPercentEncoding( l.source() ) ) ) ); |
| 179 | + } |
| 180 | + |
| 181 | + if ( !query().isEmpty() ) |
| 182 | + { |
| 183 | + url.addQueryItem( "query", query() ); |
| 184 | + } |
| 185 | + |
| 186 | + if ( !uid().isEmpty() ) |
| 187 | + url.addQueryItem( "uid", uid() ); |
| 188 | + |
| 189 | + if ( geometryWkbType() == QgsWKBTypes::NoGeometry ) |
| 190 | + url.addQueryItem( "nogeometry", "" ); |
| 191 | + else if ( !geometryField().isEmpty() ) |
| 192 | + { |
| 193 | + if ( hasDefinedGeometry() ) |
| 194 | + url.addQueryItem( "geometry", QString( "%1:%2:%3" ).arg( geometryField() ). arg( geometryWkbType() ).arg( geometrySrid() ).toUtf8() ); |
| 195 | + else |
| 196 | + url.addQueryItem( "geometry", geometryField() ); |
| 197 | + } |
| 198 | + |
| 199 | + for ( int i = 0; i < fields().count(); i++ ) |
| 200 | + { |
| 201 | + const QgsField& f = fields()[i]; |
| 202 | + if ( f.type() == QVariant::Int ) |
| 203 | + url.addQueryItem( "field", f.name() + ":int" ); |
| 204 | + else if ( f.type() == QVariant::Double ) |
| 205 | + url.addQueryItem( "field", f.name() + ":real" ); |
| 206 | + else if ( f.type() == QVariant::String ) |
| 207 | + url.addQueryItem( "field", f.name() + ":text" ); |
| 208 | + } |
| 209 | + |
| 210 | + return url; |
| 211 | +} |
| 212 | + |
| 213 | +QString QgsVirtualLayerDefinition::toString() const |
| 214 | +{ |
| 215 | + return QString( toUrl().toEncoded() ); |
| 216 | +} |
| 217 | + |
| 218 | +void QgsVirtualLayerDefinition::addSource( const QString& name, const QString ref ) |
| 219 | +{ |
| 220 | + mSourceLayers.append( SourceLayer( name, ref ) ); |
| 221 | +} |
| 222 | + |
| 223 | +void QgsVirtualLayerDefinition::addSource( const QString& name, const QString source, const QString& provider, const QString& encoding ) |
| 224 | +{ |
| 225 | + mSourceLayers.append( SourceLayer( name, source, provider, encoding ) ); |
| 226 | +} |
| 227 | + |
| 228 | +bool QgsVirtualLayerDefinition::hasSourceLayer( QString name ) const |
| 229 | +{ |
| 230 | + foreach ( const QgsVirtualLayerDefinition::SourceLayer& l, sourceLayers() ) |
| 231 | + { |
| 232 | + if ( l.name() == name ) |
| 233 | + { |
| 234 | + return true; |
| 235 | + } |
| 236 | + } |
| 237 | + return false; |
| 238 | +} |
| 239 | + |
| 240 | +bool QgsVirtualLayerDefinition::hasReferencedLayers() const |
| 241 | +{ |
| 242 | + foreach ( const QgsVirtualLayerDefinition::SourceLayer& l, sourceLayers() ) |
| 243 | + { |
| 244 | + if ( l.isReferenced() ) |
| 245 | + { |
| 246 | + return true; |
| 247 | + } |
| 248 | + } |
| 249 | + return false; |
| 250 | +} |
0 commit comments