QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsabstractdatabaseproviderconnection.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsabstractdatabaseproviderconnection.h - QgsAbstractDatabaseProviderConnection
3 
4  ---------------------
5  begin : 2.8.2019
6  copyright : (C) 2019 by Alessandro Pasotti
7  email : elpaso at itopen dot it
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 #ifndef QGSABSTRACTDATABASEPROVIDERCONNECTION_H
17 #define QGSABSTRACTDATABASEPROVIDERCONNECTION_H
18 
21 #include "qgis_core.h"
22 #include "qgsfields.h"
23 #include "qgsexception.h"
24 #include "qgsvectordataprovider.h"
25 
26 #include <QObject>
27 
28 class QgsFeedback;
29 
44 {
45 
46  Q_GADGET
47 
48  public:
49 
56  enum TableFlag
57  {
58  Aspatial = 1 << 1,
59  Vector = 1 << 2,
60  Raster = 1 << 3,
61  View = 1 << 4,
62  MaterializedView = 1 << 5,
63  Foreign = 1 << 6,
64  };
65 
66  Q_ENUMS( TableFlag )
67  Q_DECLARE_FLAGS( TableFlags, TableFlag )
68  Q_FLAG( TableFlags )
69 
70 
81  struct CORE_EXPORT TableProperty
82  {
83 
84 #ifdef SIP_RUN
85  SIP_PYOBJECT __repr__();
86  % MethodCode
87  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
88  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
89  % End
90 #endif
91 
96  struct CORE_EXPORT GeometryColumnType
97  {
98 #ifdef SIP_RUN
99  SIP_PYOBJECT __repr__();
100  % MethodCode
101  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
102  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
103  % End
104 #endif
107 
108  inline bool operator==( const GeometryColumnType &other ) const
109  {
110  return this->crs == other.crs && this->wkbType == other.wkbType;
111  }
112  };
113 
114  public:
115 
120  QString tableName() const;
121 
126  void setTableName( const QString &name );
127 
131  void addGeometryColumnType( const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs );
132 
137  QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
138 
142  void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
143 
151  QString defaultName() const;
152 
157  TableProperty at( int index ) const;
158 
162  QString schema() const;
163 
167  void setSchema( const QString &schema );
168 
172  QString geometryColumn() const;
173 
177  void setGeometryColumn( const QString &geometryColumn );
178 
182  QStringList primaryKeyColumns() const;
183 
187  void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
188 
192  QList<QgsCoordinateReferenceSystem> crsList() const;
193 
197  TableFlags flags() const;
198 
202  void setFlags( const TableFlags &flags );
203 
207  QString comment() const;
208 
212  void setComment( const QString &comment );
213 
220  QVariantMap info() const;
221 
228  void setInfo( const QVariantMap &info );
229 
235  int geometryColumnCount() const;
236 
240  void setGeometryColumnCount( int geometryColumnCount );
241 
245  void setFlag( const TableFlag &flag );
246 
252  int maxCoordinateDimensions() const;
253 
255 
256  private:
257 
259  QList<GeometryColumnType> mGeometryColumnTypes;
261  QString mSchema;
263  QString mTableName;
265  QString mGeometryColumn;
267  int mGeometryColumnCount;
269  QStringList mPkColumns;
270  TableFlags mFlags;
271  QString mComment;
273  QVariantMap mInfo;
274  };
275 
280  {
281  CreateVectorTable = 1 << 1,
282  DropRasterTable = 1 << 2,
283  DropVectorTable = 1 << 3,
284  RenameVectorTable = 1 << 4,
285  RenameRasterTable = 1 << 5,
286  CreateSchema = 1 << 6,
287  DropSchema = 1 << 7,
288  RenameSchema = 1 << 8,
289  ExecuteSql = 1 << 9,
290  Vacuum = 1 << 10,
291  Tables = 1 << 11,
292  Schemas = 1 << 12,
293  SqlLayers = 1 << 13,
294  TableExists = 1 << 14,
295  Spatial = 1 << 15,
296  CreateSpatialIndex = 1 << 16,
297  SpatialIndexExists = 1 << 17,
298  DeleteSpatialIndex = 1 << 18,
299  DeleteField = 1 << 19,
300  DeleteFieldCascade = 1 << 20,
301  AddField = 1 << 21,
302  };
303  Q_ENUM( Capability )
304  Q_DECLARE_FLAGS( Capabilities, Capability )
305  Q_FLAG( Capabilities )
306 
307 
312  {
313  Z = 1 << 1,
314  M = 1 << 2,
315  SinglePart = 1 << 3,
316  Curves = 1 << 4
317  };
318 
319  Q_ENUM( GeometryColumnCapability )
320  Q_DECLARE_FLAGS( GeometryColumnCapabilities, GeometryColumnCapability )
321  Q_FLAG( GeometryColumnCapabilities )
322 
323 
327  QgsAbstractDatabaseProviderConnection( const QString &name );
328 
334  QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
335 
336 
337  // Public interface
338 
342  Capabilities capabilities() const;
343 
348  virtual GeometryColumnCapabilities geometryColumnCapabilities();
349 
350  // Operations interface
351 
358  virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
359 
365  virtual void createVectorTable( const QString &schema, const QString &name, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, const QMap<QString, QVariant> *options ) const SIP_THROW( QgsProviderConnectionException );
366 
372  virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
373 
380  virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
381 
388  virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
389 
396  virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
397 
404  virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
405 
410  virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
411 
420  virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
421 
433  virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
434 
445  virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
446 
447 
454  virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
455 
461  virtual QList<QList<QVariant>> executeSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
462 
468  virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
469 
475  struct CORE_EXPORT SpatialIndexOptions
476  {
479  };
480 
490  virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
491 
500  virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
501 
510  virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
511 
520  virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = QgsAbstractDatabaseProviderConnection::TableFlags() ) const SIP_SKIP;
521 
529  virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
530 
538  QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
539 
540 
541  // TODO: return more schema information and not just the name
542 
548  virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
549 
558  virtual QgsFields fields( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
559 
565  virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const SIP_THROW( QgsProviderConnectionException ) = 0;
566 
571  QString providerKey() const;
572 
573  protected:
574 
576 
581  void checkCapability( Capability capability ) const;
583 
584  Capabilities mCapabilities = Capabilities() SIP_SKIP;
585  GeometryColumnCapabilities mGeometryColumnCapabilities = GeometryColumnCapabilities() SIP_SKIP;
586  QString mProviderKey;
587 
588 };
589 
591 
592 #endif // QGSABSTRACTDATABASEPROVIDERCONNECTION_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgsfields.h
QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType::wkbType
QgsWkbTypes::Type wkbType
Definition: qgsabstractdatabaseproviderconnection.h:105
QgsWkbTypes::displayString
static QString displayString(Type type) SIP_HOLDGIL
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
Definition: qgswkbtypes.cpp:145
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:400
QgsAbstractDatabaseProviderConnection::GeometryColumnCapability
GeometryColumnCapability
The GeometryColumnCapability enum represents the geomery column features supported by the connection.
Definition: qgsabstractdatabaseproviderconnection.h:312
QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType::crs
QgsCoordinateReferenceSystem crs
Definition: qgsabstractdatabaseproviderconnection.h:106
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
field
const QgsField & field
Definition: qgsfield.h:456
QgsAbstractDatabaseProviderConnection::TableProperty
The TableProperty class represents a database table or view.
Definition: qgsabstractdatabaseproviderconnection.h:82
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:189
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType::operator==
bool operator==(const GeometryColumnType &other) const
Definition: qgsabstractdatabaseproviderconnection.h:108
qgsvectordataprovider.h
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsProviderConnectionException
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsAbstractDatabaseProviderConnection::SpatialIndexOptions
Contains extra options relating to spatial index creation.
Definition: qgsabstractdatabaseproviderconnection.h:476
QgsAbstractDatabaseProviderConnection::Capability
Capability
The Capability enum represents the operations supported by the connection.
Definition: qgsabstractdatabaseproviderconnection.h:280
QgsAbstractDatabaseProviderConnection::TableFlag
TableFlag
Flags for table properties.
Definition: qgsabstractdatabaseproviderconnection.h:57
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:59
qgsexception.h
QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
Definition: qgsabstractdatabaseproviderconnection.h:97
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:45
QgsAbstractDatabaseProviderConnection::SpatialIndexOptions::geometryColumnName
QString geometryColumnName
Specifies the name of the geometry column to create the index for.
Definition: qgsabstractdatabaseproviderconnection.h:478
qgscoordinatereferencesystem.h
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44
qgsabstractproviderconnection.h
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50