QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
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 
25 #include <QObject>
26 
41 {
42 
43  Q_GADGET
44 
45  public:
46 
53  enum TableFlag
54  {
55  Aspatial = 1 << 1,
56  Vector = 1 << 2,
57  Raster = 1 << 3,
58  View = 1 << 4,
59  MaterializedView = 1 << 5,
60  Foreign = 1 << 6,
61  };
62 
63  Q_ENUMS( TableFlag )
64  Q_DECLARE_FLAGS( TableFlags, TableFlag )
65  Q_FLAG( TableFlags )
66 
67 
78  struct CORE_EXPORT TableProperty
79  {
80 
81 #ifdef SIP_RUN
82  SIP_PYOBJECT __repr__();
83  % MethodCode
84  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
85  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
86  % End
87 #endif
88 
93  struct CORE_EXPORT GeometryColumnType
94  {
95 #ifdef SIP_RUN
96  SIP_PYOBJECT __repr__();
97  % MethodCode
98  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
99  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
100  % End
101 #endif
104 
105  inline bool operator==( const GeometryColumnType &other ) const
106  {
107  return this->crs == other.crs && this->wkbType == other.wkbType;
108  }
109  };
110 
111  public:
112 
117  QString tableName() const;
118 
123  void setTableName( const QString &name );
124 
128  void addGeometryColumnType( const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs );
129 
134  QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
135 
139  void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
140 
148  QString defaultName() const;
149 
154  TableProperty at( int index ) const;
155 
159  QString schema() const;
160 
164  void setSchema( const QString &schema );
165 
169  QString geometryColumn() const;
170 
174  void setGeometryColumn( const QString &geometryColumn );
175 
179  QStringList primaryKeyColumns() const;
180 
184  void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
185 
189  QList<QgsCoordinateReferenceSystem> crsList() const;
190 
194  TableFlags flags() const;
195 
199  void setFlags( const TableFlags &flags );
200 
204  QString comment() const;
205 
209  void setComment( const QString &comment );
210 
217  QVariantMap info() const;
218 
225  void setInfo( const QVariantMap &info );
226 
232  int geometryColumnCount() const;
233 
237  void setGeometryColumnCount( int geometryColumnCount );
238 
242  void setFlag( const TableFlag &flag );
243 
249  int maxCoordinateDimensions() const;
250 
252 
253  private:
254 
256  QList<GeometryColumnType> mGeometryColumnTypes;
258  QString mSchema;
260  QString mTableName;
262  QString mGeometryColumn;
264  int mGeometryColumnCount;
266  QStringList mPkColumns;
267  TableFlags mFlags;
268  QString mComment;
270  QVariantMap mInfo;
271  };
272 
277  {
278  CreateVectorTable = 1 << 1,
279  DropRasterTable = 1 << 2,
280  DropVectorTable = 1 << 3,
281  RenameVectorTable = 1 << 4,
282  RenameRasterTable = 1 << 5,
283  CreateSchema = 1 << 6,
284  DropSchema = 1 << 7,
285  RenameSchema = 1 << 8,
286  ExecuteSql = 1 << 9,
287  Vacuum = 1 << 10,
288  Tables = 1 << 11,
289  Schemas = 1 << 12,
290  SqlLayers = 1 << 13,
291  TableExists = 1 << 14,
292  Spatial = 1 << 15,
293  CreateSpatialIndex = 1 << 16,
294  SpatialIndexExists = 1 << 17,
295  DeleteSpatialIndex = 1 << 18,
296  DeleteField = 1 << 19,
297  CreateField = 1 << 20,
298  };
299 
300  Q_ENUM( Capability )
301  Q_DECLARE_FLAGS( Capabilities, Capability )
302  Q_FLAG( Capabilities )
303 
304 
308  QgsAbstractDatabaseProviderConnection( const QString &name );
309 
315  QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
316 
317 
318  // Public interface
319 
323  Capabilities capabilities() const;
324 
325  // Operations interface
326 
333  virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
334 
340  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 );
341 
347  virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
348 
355  virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
356 
363  virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
364 
371  virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
372 
379  virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
380 
385  virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
386 
395  virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
396 
403  virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
404 
410  virtual QList<QList<QVariant>> executeSql( const QString &sql ) const SIP_THROW( QgsProviderConnectionException );
411 
417  virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
418 
424  struct CORE_EXPORT SpatialIndexOptions
425  {
428  };
429 
439  virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
440 
449  virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
450 
459  virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
460 
469  virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = nullptr ) const SIP_SKIP;
470 
478  virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table ) const;
479 
487  QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
488 
489 
490  // TODO: return more schema information and not just the name
491 
497  virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
498 
507  virtual QgsFields fields( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
508 
513  QString providerKey() const;
514 
515  protected:
516 
518 
523  void checkCapability( Capability capability ) const;
525 
526  Capabilities mCapabilities = nullptr SIP_SKIP;
527  QString mProviderKey;
528 
529 };
530 
531 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractDatabaseProviderConnection::Capabilities )
532 
533 #endif // QGSABSTRACTDATABASEPROVIDERCONNECTION_H
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Capability
The Capability enum represent the operations supported by the connection.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfields.h:44
const QgsCoordinateReferenceSystem & crs
The QgsAbstractProviderConnection provides an interface for data provider connections.
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
The TableProperty class represents a database table or view.
#define SIP_SKIP
Definition: qgis_sip.h:126
QString geometryColumnName
Specifies the name of the geometry column to create the index for.
Contains extra options relating to spatial index creation.
This class represents a coordinate reference system (CRS).
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
#define SIP_THROW(name)
Definition: qgis_sip.h:184
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81