QGIS API Documentation  2.99.0-Master (3450a9f)
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.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 
35 typedef QList<int> QgsAttributeList;
36 typedef QSet<int> QgsAttributeIds;
37 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
38 
39 class QgsFeatureIterator;
40 class QgsTransaction;
41 class QgsFeedback;
42 
43 #include "qgsfeaturerequest.h"
44 
53 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink
54 {
55  Q_OBJECT
56 
57  friend class QgsTransaction;
58 
59  public:
60 
61  // If you add to this, please also add to capabilitiesString()
62 
67  {
69  NoCapabilities = 0,
71  AddFeatures = 1,
73  DeleteFeatures = 1 << 1,
75  ChangeAttributeValues = 1 << 2,
77  AddAttributes = 1 << 3,
79  DeleteAttributes = 1 << 4,
81  CreateSpatialIndex = 1 << 6,
83  SelectAtId = 1 << 7,
85  ChangeGeometries = 1 << 8,
87  SelectEncoding = 1 << 13,
89  CreateAttributeIndex = 1 << 12,
91  SimplifyGeometries = 1 << 14,
93  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
95  TransactionSupport = 1 << 16,
97  CircularGeometries = 1 << 17,
98 
102  ChangeFeatures = 1 << 18,
104  RenameAttributes = 1 << 19,
106  FastTruncate = 1 << 20,
107  };
108 
109  Q_DECLARE_FLAGS( Capabilities, Capability )
110 
111 
112  const static int EditingCapabilities = AddFeatures | DeleteFeatures |
113  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
114  RenameAttributes;
115 
120  {
122  Uncounted = -2,
124  UnknownCount = -1,
125  };
126 
131  QgsVectorDataProvider( const QString &uri = QString() );
132 
149  virtual QgsAbstractFeatureSource *featureSource() const = 0;
150 
154  virtual QString storageType() const;
155 
161  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const = 0;
162 
166  virtual QgsWkbTypes::Type wkbType() const = 0;
167 
172  virtual long featureCount() const = 0;
173 
177  virtual QgsFields fields() const = 0;
178 
183  virtual QString dataComment() const;
184 
193  virtual QVariant minimumValue( int index ) const;
194 
203  virtual QVariant maximumValue( int index ) const;
204 
213  virtual void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 ) const;
214 
224  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
225  QgsFeedback *feedback = nullptr ) const;
226 
237  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
238  int index,
240  QgsExpressionContext *context,
241  bool &ok ) const;
242 
249  virtual void enumValues( int index, QStringList &enumList ) const { Q_UNUSED( index ); enumList.clear(); }
250 
251  virtual bool addFeatures( QgsFeatureList &flist ) override;
252 
259  virtual bool deleteFeatures( const QgsFeatureIds &id );
260 
268  virtual bool truncate();
269 
275  virtual bool addAttributes( const QList<QgsField> &attributes );
276 
284  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
285 
294  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
295 
302  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
303 
315  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
316  const QgsGeometryMap &geometry_map );
317 
329  virtual QVariant defaultValue( int fieldIndex ) const;
330 
338  virtual QString defaultValueClause( int fieldIndex ) const;
339 
346  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
347 
355  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
356 
364  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
365 
370  virtual bool createSpatialIndex();
371 
373  virtual bool createAttributeIndex( int field );
374 
380  virtual Capabilities capabilities() const;
381 
385  QString capabilitiesString() const;
386 
390  virtual void setEncoding( const QString &e );
391 
395  QString encoding() const;
396 
400  int fieldNameIndex( const QString &fieldName ) const;
401 
405  QMap<QString, int> fieldNameMap() const;
406 
410  virtual QgsAttributeList attributeIndexes() const;
411 
415  virtual QgsAttributeList pkAttributeIndexes() const;
416 
420  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
421 
425  bool supportedType( const QgsField &field ) const;
426 
427  struct NativeType
428  {
429  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 )
430  : mTypeDesc( typeDesc )
431  , mTypeName( typeName )
432  , mType( type )
433  , mMinLen( minLen )
434  , mMaxLen( maxLen )
435  , mMinPrec( minPrec )
436  , mMaxPrec( maxPrec )
437  , mSubType( subType )
438  {}
439 
440  QString mTypeDesc;
441  QString mTypeName;
442  QVariant::Type mType;
443  int mMinLen;
444  int mMaxLen;
445  int mMinPrec;
446  int mMaxPrec;
447  QVariant::Type mSubType;
448  };
449 
453  QList< NativeType > nativeTypes() const;
454 
459  virtual bool doesStrictFeatureTypeCheck() const { return true; }
460 
462  static QStringList availableEncodings();
463 
467  bool hasErrors() const;
468 
472  void clearErrors();
473 
477  QStringList errors() const;
478 
483  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
484 
489  virtual bool isDeleteStyleFromDatabaseSupported() const;
490 
491  static QVariant convertValue( QVariant::Type type, const QString &value );
492 
496  virtual QgsTransaction *transaction() const;
497 
505  virtual void forceReload();
506 
510  virtual QSet<QgsMapLayerDependency> dependencies() const;
511 
519  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
520 
525  virtual QVariantMap metadata() const { return QVariantMap(); }
526 
532  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
533 
540  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
541 
542  signals:
543 
549  void raiseError( const QString &msg ) const;
550 
551  protected:
552 
557  void clearMinMaxCache();
558 
562  void fillMinMaxCache() const;
563 
573  void pushError( const QString &msg ) const;
574 
579  QgsGeometry *convertToProviderType( const QgsGeometry &geom ) const;
580 
587  void setNativeTypes( const QList<NativeType> &nativeTypes );
588 
594  QTextCodec *textEncoding() const;
595 
596  private:
597  mutable bool mCacheMinMaxDirty;
598  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
599 
601  QTextCodec *mEncoding = nullptr;
602 
604  QgsAttributeList mAttributesToFetch;
605 
607  QList< NativeType > mNativeTypes;
608 
610  QMap<QString, QVariant::Type> mOldTypeList;
611 
613  mutable QStringList mErrors;
614 
615  static QStringList sEncodings;
616 
620  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
621 
622 };
623 
624 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
625 
626 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:510
Constraint
Constraints which may be present on a field.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:517
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:522
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:39
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
Abstract base class for spatial data provider implementations.
QList< int > QgsAttributeList
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Get the translated metadata value.
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:43
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
QSet< int > QgsAttributeIds
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:65
virtual bool addFeatures(QgsFeatureList &features)=0
Adds a list of features to the sink.
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)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual QString translateMetadataKey(const QString &mdKey) const
Get the translated metadata key.
Capability
enumeration with capabilities that providers might implement
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
Base class that can be used for any class that is capable of returning features.
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:45
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:503
This class allows including a set of layers in a database-side transaction, provided the layer data p...
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
QHash< int, QString > QgsAttrPalIndexNameHash
Aggregate
Available aggregates to calculate.
A bundle of parameters controlling aggregate calculation.
FeatureCountState
Enumeration of feature count states.
virtual QVariantMap metadata() const
Get metadata, dependent on the provider type, that will be display in the metadata tab of the layer p...