1 /*************************************************************************** 2 qgsvirtuallayerprovider.cpp Virtual layer data provider 3 begin : Jan, 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 #ifndef QGSVIRTUAL_LAYER_PROVIDER_H 18 #define QGSVIRTUAL_LAYER_PROVIDER_H 19 20 #include "qgsvectordataprovider.h" 21 22 #include "qgscoordinatereferencesystem.h" 23 #include "qgsvirtuallayerdefinition.h" 24 #include "qgsvirtuallayersqlitehelper.h" 25 26 #include "qgsprovidermetadata.h" 27 #ifdef HAVE_GUI 28 #include "qgsproviderguimetadata.h" 29 #endif 30 31 class QgsVirtualLayerFeatureIterator; 32 33 class QgsVirtualLayerProvider final: public QgsVectorDataProvider 34 { 35 Q_OBJECT 36 public: 37 38 /** 39 * Constructor of the vector provider 40 * \param uri uniform resource locator (URI) for a dataset 41 * \param options generic data provider options 42 */ 43 explicit QgsVirtualLayerProvider( QString const &uri, const ProviderOptions &coordinateTransformContext, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() ); 44 45 QgsAbstractFeatureSource *featureSource() const override; 46 QString storageType() const override; 47 QgsCoordinateReferenceSystem crs() const override; 48 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override; 49 QgsWkbTypes::Type wkbType() const override; 50 long featureCount() const override; 51 QgsRectangle extent() const override; 52 QString subsetString() const override; 53 bool setSubsetString( const QString &subset, bool updateFeatureCount = true ) override; supportsSubsetString()54 bool supportsSubsetString() const override { return true; } 55 QgsFields fields() const override; 56 bool isValid() const override; 57 QgsVectorDataProvider::Capabilities capabilities() const override; 58 QString name() const override; 59 QString description() const override; 60 QgsAttributeList pkAttributeIndexes() const override; 61 QSet<QgsMapLayerDependency> dependencies() const override; 62 bool cancelReload() override; 63 64 private: 65 66 // file on disk 67 QString mPath; 68 69 QgsScopedSqlite mSqlite; 70 71 // underlying vector layers 72 struct SourceLayer 73 { 74 SourceLayer() = default; 75 SourceLayer( QgsVectorLayer *l, const QString &n = QString() ) layerSourceLayer76 : layer( l ) 77 , name( n ) 78 {} 79 SourceLayer( const QString &p, const QString &s, const QString &n, const QString &e = QStringLiteral( "UTF-8" ) ) nameSourceLayer80 : name( n ) 81 , source( s ) 82 , provider( p ) 83 , encoding( e ) 84 {} 85 // non-null if it refers to a live layer 86 QgsVectorLayer *layer = nullptr; 87 QString name; 88 // non-empty if it is an embedded layer 89 QString source; 90 QString provider; 91 QString encoding; 92 }; 93 typedef QVector<SourceLayer> SourceLayers; 94 SourceLayers mLayers; 95 96 97 bool mValid = true; 98 99 QString mTableName; 100 101 QgsCoordinateReferenceSystem mCrs; 102 103 QgsVirtualLayerDefinition mDefinition; 104 105 QString mSubset; 106 107 void resetSqlite(); 108 109 mutable bool mCachedStatistics = false; 110 mutable qint64 mFeatureCount = 0; 111 mutable QgsRectangle mExtent; 112 113 void updateStatistics() const; 114 115 bool openIt(); 116 bool createIt(); 117 bool loadSourceLayers(); 118 void createVirtualTable( QgsVectorLayer *vlayer, const QString &name ); 119 120 /** 121 * Opens or creates file 122 */ 123 void reloadProviderData() override; 124 125 friend class QgsVirtualLayerFeatureSource; 126 127 private slots: 128 void invalidateStatistics(); 129 130 }; 131 132 class QgsVirtualLayerProviderMetadata final: public QgsProviderMetadata 133 { 134 public: 135 QgsVirtualLayerProviderMetadata(); 136 QgsVirtualLayerProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() ) override; 137 }; 138 139 #ifdef HAVE_GUI 140 class QgsVirtualLayerProviderGuiMetadata final: public QgsProviderGuiMetadata 141 { 142 public: 143 QgsVirtualLayerProviderGuiMetadata(); 144 QList<QgsSourceSelectProvider *> sourceSelectProviders() override; 145 }; 146 #endif 147 148 // clazy:excludeall=qstring-allocations 149 150 #endif 151