QGIS API Documentation  3.20.0-Odense (decaadbb31)
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_ENUM( TableFlag )
67  Q_DECLARE_FLAGS( TableFlags, TableFlag )
68  Q_FLAG( TableFlags )
69 
70 
82  struct CORE_EXPORT QueryResult
83  {
84 
88  QStringList columns() const;
89 
97  QList<QList<QVariant> > rows( QgsFeedback *feedback = nullptr );
98 
105  bool hasNextRow() const;
106 
113  QList<QVariant> nextRow() const;
114 
120  qlonglong fetchedRowCount( ) const;
121 
122 
123 #ifdef SIP_RUN
124  // Python iterator
125  QueryResult *__iter__();
126  % MethodCode
127  sipRes = sipCpp;
128  % End
129 
130  SIP_PYOBJECT __next__();
131  % MethodCode
132  QList<QVariant> result;
133  Py_BEGIN_ALLOW_THREADS
134  result = sipCpp->nextRow( );
135  Py_END_ALLOW_THREADS
136  if ( ! result.isEmpty() )
137  {
138  const sipTypeDef *qvariantlist_type = sipFindType( "QList<QVariant>" );
139  sipRes = sipConvertFromNewType( new QList<QVariant>( result ), qvariantlist_type, Py_None );
140  }
141  else
142  {
143  PyErr_SetString( PyExc_StopIteration, "" );
144  }
145  % End
146 #endif
147 
149 
155  struct CORE_EXPORT QueryResultIterator SIP_SKIP
156  {
157  QVariantList nextRow();
158  bool hasNextRow() const;
159  qlonglong fetchedRowCount();
160  virtual ~QueryResultIterator() = default;
161 
162  private:
163 
164  virtual QVariantList nextRowPrivate() = 0;
165  virtual bool hasNextRowPrivate() const = 0;
166  mutable qlonglong mFetchedRowCount = 0;
167  mutable QMutex mMutex;
168 
169  };
170 
175  void appendColumn( const QString &columnName ) SIP_SKIP;
176 
181  QueryResult( std::shared_ptr<QueryResultIterator> iterator ) SIP_SKIP;
182 
187  QueryResult( ) = default SIP_SKIP;
188 
190 
191  private:
192 
193  mutable std::shared_ptr<QueryResultIterator> mResultIterator;
194  QStringList mColumns;
195 
196  };
197 
198 
210  struct CORE_EXPORT TableProperty
211  {
212 
213 #ifdef SIP_RUN
214  SIP_PYOBJECT __repr__();
215  % MethodCode
216  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
217  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
218  % End
219 #endif
220 
225  struct CORE_EXPORT GeometryColumnType
226  {
227 #ifdef SIP_RUN
228  SIP_PYOBJECT __repr__();
229  % MethodCode
230  QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
231  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
232  % End
233 #endif
236 
237  inline bool operator==( const GeometryColumnType &other ) const
238  {
239  return this->crs == other.crs && this->wkbType == other.wkbType;
240  }
241  };
242 
243  public:
244 
249  QString tableName() const;
250 
255  void setTableName( const QString &name );
256 
260  void addGeometryColumnType( const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs );
261 
266  QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
267 
271  void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
272 
280  QString defaultName() const;
281 
286  TableProperty at( int index ) const;
287 
291  QString schema() const;
292 
296  void setSchema( const QString &schema );
297 
301  QString geometryColumn() const;
302 
306  void setGeometryColumn( const QString &geometryColumn );
307 
311  QStringList primaryKeyColumns() const;
312 
316  void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
317 
321  QList<QgsCoordinateReferenceSystem> crsList() const;
322 
326  TableFlags flags() const;
327 
331  void setFlags( const TableFlags &flags );
332 
336  QString comment() const;
337 
341  void setComment( const QString &comment );
342 
349  QVariantMap info() const;
350 
357  void setInfo( const QVariantMap &info );
358 
364  int geometryColumnCount() const;
365 
369  void setGeometryColumnCount( int geometryColumnCount );
370 
374  void setFlag( const TableFlag &flag );
375 
381  int maxCoordinateDimensions() const;
382 
384 
385  private:
386 
388  QList<GeometryColumnType> mGeometryColumnTypes;
390  QString mSchema;
392  QString mTableName;
394  QString mGeometryColumn;
396  int mGeometryColumnCount;
398  QStringList mPkColumns;
399  TableFlags mFlags;
400  QString mComment;
402  QVariantMap mInfo;
403  };
404 
409  {
410  CreateVectorTable = 1 << 1,
411  DropRasterTable = 1 << 2,
412  DropVectorTable = 1 << 3,
413  RenameVectorTable = 1 << 4,
414  RenameRasterTable = 1 << 5,
415  CreateSchema = 1 << 6,
416  DropSchema = 1 << 7,
417  RenameSchema = 1 << 8,
418  ExecuteSql = 1 << 9,
419  Vacuum = 1 << 10,
420  Tables = 1 << 11,
421  Schemas = 1 << 12,
422  SqlLayers = 1 << 13,
423  TableExists = 1 << 14,
424  Spatial = 1 << 15,
425  CreateSpatialIndex = 1 << 16,
426  SpatialIndexExists = 1 << 17,
427  DeleteSpatialIndex = 1 << 18,
428  DeleteField = 1 << 19,
429  DeleteFieldCascade = 1 << 20,
430  AddField = 1 << 21,
431  };
432  Q_ENUM( Capability )
433  Q_DECLARE_FLAGS( Capabilities, Capability )
434  Q_FLAG( Capabilities )
435 
436 
441  {
442  Z = 1 << 1,
443  M = 1 << 2,
444  SinglePart = 1 << 3,
445  Curves = 1 << 4
446  };
447 
448  Q_ENUM( GeometryColumnCapability )
449  Q_DECLARE_FLAGS( GeometryColumnCapabilities, GeometryColumnCapability )
450  Q_FLAG( GeometryColumnCapabilities )
451 
452 
456  QgsAbstractDatabaseProviderConnection( const QString &name );
457 
463  QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
464 
465 
466  // Public interface
467 
471  Capabilities capabilities() const;
472 
477  virtual GeometryColumnCapabilities geometryColumnCapabilities();
478 
479  // Operations interface
480 
487  virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
488 
494  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 );
495 
501  virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
502 
509  virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
510 
517  virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
518 
525  virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
526 
533  virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
534 
539  virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
540 
549  virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
550 
562  virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
563 
574  virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
575 
576 
583  virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
584 
591  virtual QList<QList<QVariant>> executeSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
592 
600  virtual QueryResult execSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
601 
607  virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
608 
614  struct CORE_EXPORT SpatialIndexOptions
615  {
618  };
619 
629  virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
630 
639  virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
640 
649  virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
650 
659  virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = QgsAbstractDatabaseProviderConnection::TableFlags() ) const SIP_SKIP;
660 
668  virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
669 
677  QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
678 
679 
680  // TODO: return more schema information and not just the name
681 
687  virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
688 
697  virtual QgsFields fields( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
698 
704  virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const SIP_THROW( QgsProviderConnectionException ) = 0;
705 
710  QString providerKey() const;
711 
712  protected:
713 
715 
720  void checkCapability( Capability capability ) const;
722 
723  Capabilities mCapabilities = Capabilities() SIP_SKIP;
724  GeometryColumnCapabilities mGeometryColumnCapabilities = GeometryColumnCapabilities() SIP_SKIP;
725  QString mProviderKey;
726 
727 };
728 
730 
731 #endif // QGSABSTRACTDATABASEPROVIDERCONNECTION_H
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
GeometryColumnCapability
The GeometryColumnCapability enum represents the geomery column features supported by the connection.
Capability
The Capability enum represents the operations supported by the connection.
The QgsAbstractProviderConnection provides an interface for data provider connections.
This class represents a coordinate reference system (CRS).
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
This is the base class for vector data providers.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
#define str(x)
Definition: qgis.cpp:37
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsField & field
Definition: qgsfield.h:463
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.
const QgsCoordinateReferenceSystem & crs
The QueryResult class represents the result of a query executed by execSql()
QList< QList< QVariant > > rows(QgsFeedback *feedback=nullptr)
Returns the result rows by calling the iterator internally and fetching all the rows,...
bool hasNextRow() const
Returns true if there are more rows to fetch.
qlonglong fetchedRowCount() const
Returns the number of fetched rows.
QList< QVariant > nextRow() const
Returns the next result row or an empty row if there are no rows left.
QStringList columns() const
Returns the column names.
Contains extra options relating to spatial index creation.
QString geometryColumnName
Specifies the name of the geometry column to create the index for.
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
The TableProperty class represents a database table or view.