QGIS API Documentation  2.99.0-Master (b681b7b)
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 
34 typedef QList<int> QgsAttributeList;
35 typedef QSet<int> QgsAttributeIds;
36 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
37 
38 class QgsFeatureIterator;
39 class QgsTransaction;
40 class QgsFeedback;
41 
42 #include "qgsfeaturerequest.h"
43 
52 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
53 {
54  Q_OBJECT
55 
56  friend class QgsTransaction;
57 
58  public:
59 
60  // If you add to this, please also add to capabilitiesString()
61 
66  {
68  NoCapabilities = 0,
70  AddFeatures = 1,
72  DeleteFeatures = 1 << 1,
74  ChangeAttributeValues = 1 << 2,
76  AddAttributes = 1 << 3,
78  DeleteAttributes = 1 << 4,
80  CreateSpatialIndex = 1 << 6,
82  SelectAtId = 1 << 7,
84  ChangeGeometries = 1 << 8,
86  SelectEncoding = 1 << 13,
88  CreateAttributeIndex = 1 << 12,
90  SimplifyGeometries = 1 << 14,
92  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
94  TransactionSupport = 1 << 16,
96  CircularGeometries = 1 << 17,
97 
101  ChangeFeatures = 1 << 18,
103  RenameAttributes = 1 << 19,
105  FastTruncate = 1 << 20,
106  };
107 
108  Q_DECLARE_FLAGS( Capabilities, Capability )
109 
110 
111  const static int EditingCapabilities = AddFeatures | DeleteFeatures |
112  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
113  RenameAttributes;
114 
119  QgsVectorDataProvider( const QString& uri = QString() );
120 
137  virtual QgsAbstractFeatureSource *featureSource() const = 0;
138 
142  virtual QString storageType() const;
143 
149  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const = 0;
150 
154  virtual QgsWkbTypes::Type wkbType() const = 0;
155 
160  virtual long featureCount() const = 0;
161 
165  virtual QgsFields fields() const = 0;
166 
171  virtual QString dataComment() const;
172 
181  virtual QVariant minimumValue( int index ) const;
182 
191  virtual QVariant maximumValue( int index ) const;
192 
201  virtual void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 ) const;
202 
212  virtual QStringList uniqueStringsMatching( int index, const QString& substring, int limit = -1,
213  QgsFeedback* feedback = nullptr ) const;
214 
225  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
226  int index,
227  const QgsAggregateCalculator::AggregateParameters& parameters,
228  QgsExpressionContext* context,
229  bool& ok ) const;
230 
237  virtual void enumValues( int index, QStringList& enumList ) const { Q_UNUSED( index ); enumList.clear(); }
238 
243  virtual bool addFeatures( QgsFeatureList &flist );
244 
251  virtual bool deleteFeatures( const QgsFeatureIds &id );
252 
260  virtual bool truncate();
261 
267  virtual bool addAttributes( const QList<QgsField> &attributes );
268 
274  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
275 
282  virtual bool renameAttributes( const QgsFieldNameMap& renamedAttributes );
283 
290  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
291 
303  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
304  const QgsGeometryMap &geometry_map );
305 
317  virtual QVariant defaultValue( int fieldIndex ) const;
318 
326  virtual QString defaultValueClause( int fieldIndex ) const;
327 
334  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
335 
343  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant& value = QVariant() ) const;
344 
352  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
353 
358  virtual bool createSpatialIndex();
359 
361  virtual bool createAttributeIndex( int field );
362 
368  virtual Capabilities capabilities() const;
369 
373  QString capabilitiesString() const;
374 
378  virtual void setEncoding( const QString& e );
379 
383  QString encoding() const;
384 
388  int fieldNameIndex( const QString& fieldName ) const;
389 
393  QMap<QString, int> fieldNameMap() const;
394 
398  virtual QgsAttributeList attributeIndexes() const;
399 
403  virtual QgsAttributeList pkAttributeIndexes() const;
404 
408  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
409 
413  bool supportedType( const QgsField &field ) const;
414 
415  struct NativeType
416  {
417  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 )
418  : mTypeDesc( typeDesc )
419  , mTypeName( typeName )
420  , mType( type )
421  , mMinLen( minLen )
422  , mMaxLen( maxLen )
423  , mMinPrec( minPrec )
424  , mMaxPrec( maxPrec )
425  , mSubType( subType )
426  {}
427 
428  QString mTypeDesc;
429  QString mTypeName;
430  QVariant::Type mType;
431  int mMinLen;
432  int mMaxLen;
433  int mMinPrec;
434  int mMaxPrec;
435  QVariant::Type mSubType;
436  };
437 
441  QList< NativeType > nativeTypes() const;
442 
447  virtual bool doesStrictFeatureTypeCheck() const { return true; }
448 
450  static QStringList availableEncodings();
451 
455  bool hasErrors() const;
456 
460  void clearErrors();
461 
465  QStringList errors() const;
466 
471  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
472 
477  virtual bool isDeleteStyleFromDatabaseSupported() const;
478 
479  static QVariant convertValue( QVariant::Type type, const QString& value );
480 
484  virtual QgsTransaction* transaction() const;
485 
493  virtual void forceReload();
494 
498  virtual QSet<QgsMapLayerDependency> dependencies() const;
499 
507  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer* self, const QList<QgsVectorLayer*>& layers ) const;
508 
513  virtual QVariantMap metadata() const { return QVariantMap(); };
514 
520  virtual QString translateMetadataKey( const QString& mdKey ) const { return mdKey; };
521 
528  virtual QString translateMetadataValue( const QString& mdKey, const QVariant& value ) const { Q_UNUSED( mdKey ); return value.toString(); };
529 
530  signals:
531 
537  void raiseError( const QString& msg ) const;
538 
539  protected:
540 
545  void clearMinMaxCache();
546 
550  void fillMinMaxCache() const;
551 
561  void pushError( const QString& msg ) const;
562 
567  QgsGeometry* convertToProviderType( const QgsGeometry& geom ) const;
568 
575  void setNativeTypes( const QList<NativeType>& nativeTypes );
576 
582  QTextCodec* textEncoding() const;
583 
584  private:
585  mutable bool mCacheMinMaxDirty;
586  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
587 
589  QTextCodec* mEncoding = nullptr;
590 
592  QgsAttributeList mAttributesToFetch;
593 
595  QList< NativeType > mNativeTypes;
596 
598  QMap<QString, QVariant::Type> mOldTypeList;
599 
601  mutable QStringList mErrors;
602 
603  static QStringList sEncodings;
604 
608  virtual void setTransaction( QgsTransaction* /*transaction*/ ) {}
609 
610 };
611 
612 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
613 
614 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:358
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:353
static unsigned index
Constraint
Constraints which may be present on a field.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:360
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:38
Utility class for calculating aggregates for a field (or expression) over the features from a vector ...
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:42
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
QSet< int > QgsAttributeIds
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< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:350
This class allows including a set of layers in a database-side transaction, provided the layer data p...
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
QHash< int, QString > QgsAttrPalIndexNameHash
virtual QVariantMap metadata() const
Get metadata, dependent on the provider type, that will be display in the metadata tab of the layer p...