QGIS API Documentation  2.99.0-Master (314842d)
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 
276  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
277 
286  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
287 
294  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
295 
307  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
308  const QgsGeometryMap &geometry_map );
309 
321  virtual QVariant defaultValue( int fieldIndex ) const;
322 
330  virtual QString defaultValueClause( int fieldIndex ) const;
331 
338  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
339 
347  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
348 
356  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
357 
362  virtual bool createSpatialIndex();
363 
365  virtual bool createAttributeIndex( int field );
366 
372  virtual Capabilities capabilities() const;
373 
377  QString capabilitiesString() const;
378 
382  virtual void setEncoding( const QString &e );
383 
387  QString encoding() const;
388 
392  int fieldNameIndex( const QString &fieldName ) const;
393 
397  QMap<QString, int> fieldNameMap() const;
398 
402  virtual QgsAttributeList attributeIndexes() const;
403 
407  virtual QgsAttributeList pkAttributeIndexes() const;
408 
412  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
413 
417  bool supportedType( const QgsField &field ) const;
418 
419  struct NativeType
420  {
421  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 )
422  : mTypeDesc( typeDesc )
423  , mTypeName( typeName )
424  , mType( type )
425  , mMinLen( minLen )
426  , mMaxLen( maxLen )
427  , mMinPrec( minPrec )
428  , mMaxPrec( maxPrec )
429  , mSubType( subType )
430  {}
431 
432  QString mTypeDesc;
433  QString mTypeName;
434  QVariant::Type mType;
435  int mMinLen;
436  int mMaxLen;
437  int mMinPrec;
438  int mMaxPrec;
439  QVariant::Type mSubType;
440  };
441 
445  QList< NativeType > nativeTypes() const;
446 
451  virtual bool doesStrictFeatureTypeCheck() const { return true; }
452 
454  static QStringList availableEncodings();
455 
459  bool hasErrors() const;
460 
464  void clearErrors();
465 
469  QStringList errors() const;
470 
475  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
476 
481  virtual bool isDeleteStyleFromDatabaseSupported() const;
482 
483  static QVariant convertValue( QVariant::Type type, const QString &value );
484 
488  virtual QgsTransaction *transaction() const;
489 
497  virtual void forceReload();
498 
502  virtual QSet<QgsMapLayerDependency> dependencies() const;
503 
511  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
512 
517  virtual QVariantMap metadata() const { return QVariantMap(); };
518 
524  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; };
525 
532  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); };
533 
534  signals:
535 
541  void raiseError( const QString &msg ) const;
542 
543  protected:
544 
549  void clearMinMaxCache();
550 
554  void fillMinMaxCache() const;
555 
565  void pushError( const QString &msg ) const;
566 
571  QgsGeometry *convertToProviderType( const QgsGeometry &geom ) const;
572 
579  void setNativeTypes( const QList<NativeType> &nativeTypes );
580 
586  QTextCodec *textEncoding() const;
587 
588  private:
589  mutable bool mCacheMinMaxDirty;
590  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
591 
593  QTextCodec *mEncoding = nullptr;
594 
596  QgsAttributeList mAttributesToFetch;
597 
599  QList< NativeType > mNativeTypes;
600 
602  QMap<QString, QVariant::Type> mOldTypeList;
603 
605  mutable QStringList mErrors;
606 
607  static QStringList sEncodings;
608 
612  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
613 
614 };
615 
616 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
617 
618 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:361
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:356
static unsigned index
Constraint
Constraints which may be present on a field.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:358
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:363
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:353
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...