QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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"
25
26#include <QObject>
27
28class QgsFeedback;
29class QgsFieldDomain;
30class QgsWeakRelation;
32
33
48{
49
50 Q_GADGET
51
52 public:
53
61 {
62 Aspatial = 1 << 1,
63 Vector = 1 << 2,
64 Raster = 1 << 3,
65 View = 1 << 4,
66 MaterializedView = 1 << 5,
67 Foreign = 1 << 6,
68 };
69
70 Q_ENUM( TableFlag )
71 Q_DECLARE_FLAGS( TableFlags, TableFlag )
72 Q_FLAG( TableFlags )
73
74
86 struct CORE_EXPORT QueryResult
87 {
88
92 QStringList columns() const;
93
101 QList<QList<QVariant> > rows( QgsFeedback *feedback = nullptr );
102
109 bool hasNextRow() const;
110
117 QList<QVariant> nextRow() const;
118
124 long long fetchedRowCount( ) const;
125
131 long long rowCount( ) const;
132
133
134#ifdef SIP_RUN
135 // Python iterator
136 QueryResult *__iter__();
137 % MethodCode
138 sipRes = sipCpp;
139 % End
140
141 SIP_PYOBJECT __next__();
142 % MethodCode
143 QList<QVariant> result;
144 Py_BEGIN_ALLOW_THREADS
145 result = sipCpp->nextRow( );
146 Py_END_ALLOW_THREADS
147 if ( ! result.isEmpty() )
148 {
149 const sipTypeDef *qvariantlist_type = sipFindType( "QList<QVariant>" );
150 sipRes = sipConvertFromNewType( new QList<QVariant>( result ), qvariantlist_type, Py_None );
151 }
152 else
153 {
154 PyErr_SetString( PyExc_StopIteration, "" );
155 }
156 % End
157#endif
158
160
166 struct CORE_EXPORT QueryResultIterator SIP_SKIP
167 {
169 QVariantList nextRow();
170
172 bool hasNextRow() const;
173
175 long long fetchedRowCount();
176
178 long long rowCount();
179
180 virtual ~QueryResultIterator() = default;
181
182 private:
183
184 virtual QVariantList nextRowPrivate() = 0;
185 virtual bool hasNextRowPrivate() const = 0;
186 virtual long long rowCountPrivate() const = 0;
187
188 mutable qlonglong mFetchedRowCount = 0;
189 mutable QMutex mMutex;
190
191 };
192
198 void appendColumn( const QString &columnName ) SIP_SKIP;
199
205 QueryResult( std::shared_ptr<QueryResultIterator> iterator ) SIP_SKIP;
206
211 QueryResult( ) = default SIP_SKIP;
212
216 double queryExecutionTime( ) const;
217
221 void setQueryExecutionTime( double queryExecutionTime );
222
224
225 private:
226
227 mutable std::shared_ptr<QueryResultIterator> mResultIterator;
228 QStringList mColumns;
230 double mQueryExecutionTime = 0;
231
232 };
233
240 struct CORE_EXPORT SqlVectorLayerOptions
241 {
243 QString sql;
245 QString filter;
247 QString layerName;
249 QStringList primaryKeyColumns;
253 bool disableSelectAtId = false;
254
255 };
256
268 struct CORE_EXPORT TableProperty
269 {
270
271#ifdef SIP_RUN
272 SIP_PYOBJECT __repr__();
273 % MethodCode
274 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
275 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
276 % End
277#endif
278
283 struct CORE_EXPORT GeometryColumnType
284 {
285#ifdef SIP_RUN
286 SIP_PYOBJECT __repr__();
287 % MethodCode
288 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
289 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
290 % End
291#endif
294
295 // TODO c++20 - replace with = default
296 inline bool operator==( const GeometryColumnType &other ) const
297 {
298 return this->crs == other.crs && this->wkbType == other.wkbType;
299 }
300 };
301
302 public:
303
309 QString tableName() const;
310
316 void setTableName( const QString &name );
317
321 void addGeometryColumnType( const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs );
322
328 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
329
333 void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
334
342 QString defaultName() const;
343
348 TableProperty at( int index ) const;
349
353 QString schema() const;
354
358 void setSchema( const QString &schema );
359
363 QString geometryColumn() const;
364
368 void setGeometryColumn( const QString &geometryColumn );
369
373 QStringList primaryKeyColumns() const;
374
378 void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
379
383 QList<QgsCoordinateReferenceSystem> crsList() const;
384
388 TableFlags flags() const;
389
393 void setFlags( const TableFlags &flags );
394
398 QString comment() const;
399
403 void setComment( const QString &comment );
404
411 QVariantMap info() const;
412
419 void setInfo( const QVariantMap &info );
420
426 int geometryColumnCount() const;
427
431 void setGeometryColumnCount( int geometryColumnCount );
432
436 void setFlag( const TableFlag &flag );
437
444 int maxCoordinateDimensions() const;
445
447
448 private:
449
451 QList<GeometryColumnType> mGeometryColumnTypes;
453 QString mSchema;
455 QString mTableName;
457 QString mGeometryColumn;
459 int mGeometryColumnCount;
461 QStringList mPkColumns;
462 TableFlags mFlags;
463 QString mComment;
465 QVariantMap mInfo;
466 };
467
473 struct CORE_EXPORT SpatialIndexOptions
474 {
477 };
478
483 {
484 CreateVectorTable = 1 << 1,
485 DropRasterTable = 1 << 2,
486 DropVectorTable = 1 << 3,
487 RenameVectorTable = 1 << 4,
488 RenameRasterTable = 1 << 5,
489 CreateSchema = 1 << 6,
490 DropSchema = 1 << 7,
491 RenameSchema = 1 << 8,
492 ExecuteSql = 1 << 9,
493 Vacuum = 1 << 10,
494 Tables = 1 << 11,
495 Schemas = 1 << 12,
496 SqlLayers = 1 << 13,
497 TableExists = 1 << 14,
498 Spatial = 1 << 15,
499 CreateSpatialIndex = 1 << 16,
500 SpatialIndexExists = 1 << 17,
501 DeleteSpatialIndex = 1 << 18,
502 DeleteField = 1 << 19,
503 DeleteFieldCascade = 1 << 20,
504 AddField = 1 << 21,
505 ListFieldDomains = 1 << 22,
506 RetrieveFieldDomain = 1 << 23,
507 SetFieldDomain = 1 << 24,
508 AddFieldDomain = 1 << 25,
509 RenameField = 1 << 26,
510 RetrieveRelationships = 1 << 27,
511 };
512 Q_ENUM( Capability )
513 Q_DECLARE_FLAGS( Capabilities, Capability )
514 Q_FLAG( Capabilities )
515
516
522 {
523 Z = 1 << 1,
524 M = 1 << 2,
525 SinglePart = 1 << 3,
526 Curves = 1 << 4,
527 SinglePoint = 1 << 5,
528 SingleLineString = 1 << 6,
529 SinglePolygon = 1 << 7,
530 };
531 // TODO QGIS 4.0 -- remove SinglePart
532
533 Q_ENUM( GeometryColumnCapability )
534 Q_DECLARE_FLAGS( GeometryColumnCapabilities, GeometryColumnCapability )
535 Q_FLAG( GeometryColumnCapabilities )
536
537
542 QgsAbstractDatabaseProviderConnection( const QString &name );
543
550 QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
551
552
553 // Public interface
554
558 Capabilities capabilities() const;
559
565 virtual GeometryColumnCapabilities geometryColumnCapabilities();
566
571 virtual Qgis::SqlLayerDefinitionCapabilities sqlLayerDefinitionCapabilities();
572
573 // Operations interface
574
581 virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
582
588 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 );
589
595 virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
596
603 virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
604
611 virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
612
619 virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
620
627 virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
628
634 virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
635
644 virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
645
657 virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
658
669 virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
670
684 virtual void renameField( const QString &schema, const QString &tableName, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
685
692 virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
693
700 virtual QList<QList<QVariant>> executeSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
701
707 virtual QgsVectorLayer *createSqlVectorLayer( const SqlVectorLayerOptions &options ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
708
716 virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource ) SIP_THROW( QgsProviderConnectionException );
717
725 virtual QueryResult execSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
726
732 virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
733
742 virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
743
751 virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
752
760 virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
761
770 virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = QgsAbstractDatabaseProviderConnection::TableFlags() ) const SIP_SKIP;
771
779 virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
780
788 QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
789
790
791 // TODO: return more schema information and not just the name
792
798 virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
799
810 virtual QgsFields fields( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
811
818 virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const SIP_THROW( QgsProviderConnectionException ) = 0;
819
825 QString providerKey() const;
826
836 virtual QMultiMap<Qgis::SqlKeywordCategory, QStringList> sqlDictionary();
837
848 virtual QStringList fieldDomainNames() const SIP_THROW( QgsProviderConnectionException );
849
855 virtual QList< Qgis::FieldDomainType > supportedFieldDomainTypes() const;
856
869 virtual QgsFieldDomain *fieldDomain( const QString &name ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
870
882 virtual void setFieldDomainName( const QString &fieldName, const QString &schema, const QString &tableName, const QString &domainName ) const SIP_THROW( QgsProviderConnectionException );
883
893 virtual void addFieldDomain( const QgsFieldDomain &domain, const QString &schema ) const SIP_THROW( QgsProviderConnectionException );
894
907 virtual QList< QgsWeakRelation > relationships( const QString &schema = QString(), const QString &tableName = QString() ) const SIP_THROW( QgsProviderConnectionException );
908
916 virtual QgsProviderSqlQueryBuilder *queryBuilder() const SIP_FACTORY;
917
938 virtual QList<QgsLayerMetadataProviderResult> searchLayerMetadata( const QgsMetadataSearchContext &searchContext, const QString &searchString = QString(), const QgsRectangle &geographicExtent = QgsRectangle(), QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException, QgsNotSupportedException );
939
940 protected:
941
943
949 void checkCapability( Capability capability ) const;
951
952 Capabilities mCapabilities = Capabilities() SIP_SKIP;
953 GeometryColumnCapabilities mGeometryColumnCapabilities = GeometryColumnCapabilities() SIP_SKIP;
954 Qgis::SqlLayerDefinitionCapabilities mSqlLayerDefinitionCapabilities = Qgis::SqlLayerDefinitionCapabilities() SIP_SKIP;
955 QString mProviderKey;
956
957};
958
960
961#endif // QGSABSTRACTDATABASEPROVIDERCONNECTION_H
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
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
Base class for field domains.
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
Result record of layer metadata provider search.
Custom exception class which is raised when an operation is not supported.
Definition: qgsexception.h:118
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
Provides an interface for provider-specific creation of SQL queries.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
The QgsWeakRelation class represent a QgsRelation with possibly unresolved layers or unmatched fields...
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_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
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,...
long long fetchedRowCount() const
Returns the number of fetched rows.
bool hasNextRow() const
Returns true if there are more rows to fetch.
QList< QVariant > nextRow() const
Returns the next result row or an empty row if there are no rows left.
long long rowCount() const
Returns the number of rows returned by a SELECT query or Qgis::FeatureCountState::UnknownCount if unk...
QStringList columns() const
Returns the column names.
The SpatialIndexOptions contains extra options relating to spatial index creation.
QString geometryColumnName
Specifies the name of the geometry column to create the index for.
The SqlVectorLayerOptions stores all information required to create a SQL (query) layer.
QString sql
The SQL expression that defines the SQL (query) layer.
QString filter
Additional subset string (provider-side filter), not all data providers support this feature: check s...
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
The TableProperty class represents a database table or view.