QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsvectordataprovider.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectordataprovider.h - DataProvider Interface for vector layers
3  --------------------------------------
4  Date : 23-Sep-2004
5  Copyright : (C) 2004 by Marco Hugentobler
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSVECTORDATAPROVIDER_H
16 #define QGSVECTORDATAPROVIDER_H
17 
18 class QTextCodec;
19 
20 #include "qgis_core.h"
21 #include <QList>
22 #include <QSet>
23 #include <QMap>
24 #include <QHash>
25 
26 //QGIS Includes
27 #include "qgis_sip.h"
28 #include "qgsdataprovider.h"
29 #include "qgsfeature.h"
30 #include "qgsaggregatecalculator.h"
31 #include "qgsmaplayerdependency.h"
32 #include "qgsrelation.h"
33 #include "qgsfeaturesink.h"
34 #include "qgsfeaturesource.h"
35 #include "qgsfeaturerequest.h"
37 
38 typedef QList<int> QgsAttributeList SIP_SKIP;
39 typedef QSet<int> QgsAttributeIds SIP_SKIP;
40 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
41 
42 class QgsFeatureIterator;
43 class QgsTransaction;
44 class QgsFeedback;
45 class QgsFeatureRenderer;
47 
48 
58 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
59 {
60  Q_OBJECT
61 
62  friend class QgsTransaction;
64 
65  public:
66 
67  // If you add to this, please also add to capabilitiesString()
68 
73  {
74  NoCapabilities = 0,
75  AddFeatures = 1,
76  DeleteFeatures = 1 << 1,
77  ChangeAttributeValues = 1 << 2,
78  AddAttributes = 1 << 3,
79  DeleteAttributes = 1 << 4,
80  CreateSpatialIndex = 1 << 6,
81  SelectAtId = 1 << 7,
82  ChangeGeometries = 1 << 8,
83  SelectEncoding = 1 << 13,
84  CreateAttributeIndex = 1 << 12,
85  SimplifyGeometries = 1 << 14,
86  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
87  TransactionSupport = 1 << 16,
88  CircularGeometries = 1 << 17,
89  ChangeFeatures = 1 << 18,
92  RenameAttributes = 1 << 19,
93  FastTruncate = 1 << 20,
94  ReadLayerMetadata = 1 << 21,
95  WriteLayerMetadata = 1 << 22,
96  CancelSupport = 1 << 23,
97  CreateRenderer = 1 << 24,
98  CreateLabeling = 1 << 25,
99  };
100 
101  Q_DECLARE_FLAGS( Capabilities, Capability )
102 
103 
104  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
105  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
106  RenameAttributes;
107 
112  {
114  Uncounted = -2,
116  UnknownCount = -1,
117  };
118 
126  QgsVectorDataProvider( const QString &uri = QString(), const QgsDataProvider::ProviderOptions &providerOptions = QgsDataProvider::ProviderOptions() );
127 
144  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
145 
149  virtual QString storageType() const;
150 
156  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
157 
161  QgsWkbTypes::Type wkbType() const override = 0;
162 
167  long featureCount() const override = 0;
168 
174  virtual bool empty() const;
175 
187 
191  QgsFields fields() const override = 0;
192 
193  QgsCoordinateReferenceSystem sourceCrs() const override;
194  QgsRectangle sourceExtent() const override;
195  QString sourceName() const override { return QString(); }
196 
201  virtual QString dataComment() const override;
202 
211  QVariant minimumValue( int index ) const override;
212 
221  QVariant maximumValue( int index ) const override;
222 
232  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
233  QgsFeedback *feedback = nullptr ) const;
234 
247  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
248  int index,
250  QgsExpressionContext *context,
251  bool &ok,
252  QgsFeatureIds *fids = nullptr ) const;
253 
260  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }
261 
262  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
263 
270  virtual bool deleteFeatures( const QgsFeatureIds &id );
271 
279  virtual bool truncate();
280 
287  virtual bool cancelReload();
288 
294  virtual bool addAttributes( const QList<QgsField> &attributes );
295 
303  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
304 
313  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
314 
323  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
324 
336  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
337  const QgsGeometryMap &geometry_map );
338 
350  virtual QVariant defaultValue( int fieldIndex ) const;
351 
359  virtual QString defaultValueClause( int fieldIndex ) const;
360 
367  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
368 
376  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
377 
385  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
386 
391  virtual bool createSpatialIndex();
392 
394  virtual bool createAttributeIndex( int field );
395 
402  virtual QgsVectorDataProvider::Capabilities capabilities() const;
403 
407  QString capabilitiesString() const;
408 
421  virtual void setEncoding( const QString &e );
422 
428  QString encoding() const;
429 
433  int fieldNameIndex( const QString &fieldName ) const;
434 
438  QMap<QString, int> fieldNameMap() const;
439 
443  virtual QgsAttributeList attributeIndexes() const;
444 
448  virtual QgsAttributeList pkAttributeIndexes() const;
449 
453  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
454 
458  bool supportedType( const QgsField &field ) const;
459 
460  struct NativeType
461  {
462  NativeType( const QString &typeDesc, const QString &typeName, QVariant::Type type, int minLen = 0, int maxLen = 0, int minPrec = 0, int maxPrec = 0, QVariant::Type subType = QVariant::Invalid )
463  : mTypeDesc( typeDesc )
464  , mTypeName( typeName )
465  , mType( type )
466  , mMinLen( minLen )
467  , mMaxLen( maxLen )
468  , mMinPrec( minPrec )
469  , mMaxPrec( maxPrec )
470  , mSubType( subType )
471  {}
472 
473  QString mTypeDesc;
474  QString mTypeName;
475  QVariant::Type mType;
476  int mMinLen;
477  int mMaxLen;
478  int mMinPrec;
479  int mMaxPrec;
480  QVariant::Type mSubType;
481  };
482 
486  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
487 
492  virtual bool doesStrictFeatureTypeCheck() const { return true; }
493 
495  static QStringList availableEncodings();
496 
500  bool hasErrors() const;
501 
505  void clearErrors();
506 
510  QStringList errors() const;
511 
516  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
517 
522  virtual bool isDeleteStyleFromDatabaseSupported() const;
523 
539  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
540 
556  virtual QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
557 
558  static QVariant convertValue( QVariant::Type type, const QString &value );
559 
563  virtual QgsTransaction *transaction() const;
564 
568  Q_DECL_DEPRECATED virtual void forceReload() SIP_DEPRECATED { reloadData(); }
569 
573  virtual QSet<QgsMapLayerDependency> dependencies() const;
574 
582  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
583 
588  virtual QVariantMap metadata() const { return QVariantMap(); }
589 
595  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
596 
603  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ) return value.toString(); }
604 
612  virtual bool hasMetadata() const { return true; }
613 
620  virtual void handlePostCloneOperations( QgsVectorDataProvider *source );
621 
624 
625  signals:
626 
632  void raiseError( const QString &msg ) const;
633 
634  protected:
635 
640  void clearMinMaxCache();
641 
645  void fillMinMaxCache() const;
646 
656  void pushError( const QString &msg ) const;
657 
662  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
663 
670  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
671 
677  QTextCodec *textEncoding() const;
678 
679  private:
680  mutable bool mCacheMinMaxDirty = true;
681  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
682 
684  QTextCodec *mEncoding = nullptr;
685 
687  QgsAttributeList mAttributesToFetch;
688 
690  QList< NativeType > mNativeTypes;
691 
693  mutable QStringList mErrors;
694 
695  std::unique_ptr< QgsVectorDataProviderTemporalCapabilities > mTemporalCapabilities;
696 
697  static QStringList sEncodings;
698 
702  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
703 };
704 
705 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
706 
707 #endif
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
qgsvectordataprovidertemporalcapabilities.h
QgsAttributeIds
QSet< int > QgsAttributeIds
Definition: qgsvectordataprovider.h:39
QgsFeatureSource::minimumValue
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:52
QgsVectorDataProvider::NativeType::mMaxPrec
int mMaxPrec
Definition: qgsvectordataprovider.h:479
QgsFeatureSource::sourceCrs
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
qgsfeaturerequest.h
QgsVectorDataProvider::NativeType::mMinPrec
int mMinPrec
Definition: qgsvectordataprovider.h:478
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:104
QgsDataProvider
Definition: qgsdataprovider.h:41
QgsVectorDataProvider::FeatureCountState
FeatureCountState
Enumeration of feature count states.
Definition: qgsvectordataprovider.h:111
QgsVectorDataProvider::metadata
virtual QVariantMap metadata() const
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...
Definition: qgsvectordataprovider.h:588
qgsmaplayerdependency.h
QgsAggregateCalculator::AggregateParameters
A bundle of parameters controlling aggregate calculation.
Definition: qgsaggregatecalculator.h:91
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsVectorDataProvider::enumValues
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
Definition: qgsvectordataprovider.h:260
QgsFields
Definition: qgsfields.h:44
QgsVectorDataProvider::NativeType::mMinLen
int mMinLen
Definition: qgsvectordataprovider.h:476
qgsfeaturesink.h
qgsfeature.h
QgsFeatureSource
Definition: qgsfeaturesource.h:37
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsVectorDataProvider::doesStrictFeatureTypeCheck
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
Definition: qgsvectordataprovider.h:492
QgsTransaction
Definition: qgstransaction.h:56
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:558
QgsFieldConstraints::Constraint
Constraint
Constraints which may be present on a field.
Definition: qgsfieldconstraints.h:43
QgsVectorDataProvider::NativeType
Definition: qgsvectordataprovider.h:460
QgsVectorDataProvider::hasMetadata
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
Definition: qgsvectordataprovider.h:612
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
Definition: qgsrectangle.h:41
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
qgsaggregatecalculator.h
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsFeatureSource::hasFeatures
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
Definition: qgsfeaturesource.cpp:26
QgsVectorDataProvider::Capability
Capability
enumeration with capabilities that providers might implement
Definition: qgsvectordataprovider.h:72
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsVectorDataProvider::translateMetadataValue
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Gets the translated metadata value.
Definition: qgsvectordataprovider.h:603
QgsFeatureSource::maximumValue
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:75
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsDataProvider::reloadData
virtual void reloadData()
Reloads the data from the source by calling reloadProviderData() implemented by providers with data c...
Definition: qgsdataprovider.cpp:38
QgsVectorDataProvider::NativeType::mTypeDesc
QString mTypeDesc
Definition: qgsvectordataprovider.h:473
QgsFeedback
Definition: qgsfeedback.h:43
QgsVectorDataProvider::NativeType::mMaxLen
int mMaxLen
Definition: qgsvectordataprovider.h:477
qgis_sip.h
QgsAbstractFeatureSource
Definition: qgsfeaturerequest.h:742
QgsFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
QgsVectorDataProvider::NativeType::NativeType
NativeType(const QString &typeDesc, const QString &typeName, QVariant::Type type, int minLen=0, int maxLen=0, int minPrec=0, int maxPrec=0, QVariant::Type subType=QVariant::Invalid)
Definition: qgsvectordataprovider.h:462
QgsVectorDataProvider::sourceName
QString sourceName() const override
Returns a friendly display name for the source.
Definition: qgsvectordataprovider.h:195
qgsfeaturesource.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsVectorDataProvider::NativeType::mTypeName
QString mTypeName
Definition: qgsvectordataprovider.h:474
QgsVectorDataProvider::NativeType::mSubType
QVariant::Type mSubType
Definition: qgsvectordataprovider.h:480
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:109
QgsFeatureSource::sourceExtent
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
Definition: qgsfeaturesource.cpp:98
qgsrelation.h
QgsVectorDataProvider::translateMetadataKey
virtual QString translateMetadataKey(const QString &mdKey) const
Gets the translated metadata key.
Definition: qgsvectordataprovider.h:595
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsFeatureSource::fields
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
QgsAbstractVectorLayerLabeling
Definition: qgsvectorlayerlabeling.h:41
QgsDataProvider::temporalCapabilities
virtual QgsDataProviderTemporalCapabilities * temporalCapabilities()
Returns the provider's temporal capabilities.
Definition: qgsdataprovider.cpp:28
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
QgsFeatureSource::featureCount
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsFeatureSource::FeatureAvailability
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Definition: qgsfeaturesource.h:50
QgsVectorDataProvider::NativeType::mType
QVariant::Type mType
Definition: qgsvectordataprovider.h:475
QgsAggregateCalculator::Aggregate
Aggregate
Available aggregates to calculate.
Definition: qgsaggregatecalculator.h:64
QgsFeatureRenderer
Definition: qgsrenderer.h:102
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:36
QgsFeatureSource::wkbType
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
QgsVectorDataProviderTemporalCapabilities
Definition: qgsvectordataprovidertemporalcapabilities.h:37
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFieldNameMap
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
qgsdataprovider.h
QgsAttrPalIndexNameHash
QHash< int, QString > QgsAttrPalIndexNameHash
Definition: qgsvectordataprovider.h:40
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:567
QgsVectorDataProvider
Definition: qgsvectordataprovider.h:58
QgsVectorLayerEditBuffer::deleteFeatures
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
Definition: qgsvectorlayereditbuffer.cpp:182
QgsDataProvider::dataComment
virtual QString dataComment() const
Returns a short comment for the data that this provider is providing access to (e....
Definition: qgsdataprovider.h:162
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsVectorLayerEditBuffer::changeAttributeValues
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
Definition: qgsvectorlayereditbuffer.cpp:220
attributeIndexes
const QgsAttributeList & attributeIndexes
Definition: qgswfsgetfeature.cpp:107
QgsFeatureSink
Definition: qgsfeaturesink.h:33
QgsVectorLayerEditBuffer::addFeatures
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
Definition: qgsvectorlayereditbuffer.cpp:136
QgsField
Definition: qgsfield.h:49