QGIS API Documentation 3.27.0-Master (f261cc1f8b)
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"
24
25#include <QObject>
26
27class QgsFeedback;
28class QgsFieldDomain;
29class QgsWeakRelation;
30
31
46{
47
48 Q_GADGET
49
50 public:
51
59 {
60 Aspatial = 1 << 1,
61 Vector = 1 << 2,
62 Raster = 1 << 3,
63 View = 1 << 4,
64 MaterializedView = 1 << 5,
65 Foreign = 1 << 6,
66 };
67
68 Q_ENUM( TableFlag )
69 Q_DECLARE_FLAGS( TableFlags, TableFlag )
70 Q_FLAG( TableFlags )
71
72
84 struct CORE_EXPORT QueryResult
85 {
86
90 QStringList columns() const;
91
99 QList<QList<QVariant> > rows( QgsFeedback *feedback = nullptr );
100
107 bool hasNextRow() const;
108
115 QList<QVariant> nextRow() const;
116
122 long long fetchedRowCount( ) const;
123
129 long long rowCount( ) const;
130
131
132#ifdef SIP_RUN
133 // Python iterator
134 QueryResult *__iter__();
135 % MethodCode
136 sipRes = sipCpp;
137 % End
138
139 SIP_PYOBJECT __next__();
140 % MethodCode
141 QList<QVariant> result;
142 Py_BEGIN_ALLOW_THREADS
143 result = sipCpp->nextRow( );
144 Py_END_ALLOW_THREADS
145 if ( ! result.isEmpty() )
146 {
147 const sipTypeDef *qvariantlist_type = sipFindType( "QList<QVariant>" );
148 sipRes = sipConvertFromNewType( new QList<QVariant>( result ), qvariantlist_type, Py_None );
149 }
150 else
151 {
152 PyErr_SetString( PyExc_StopIteration, "" );
153 }
154 % End
155#endif
156
158
164 struct CORE_EXPORT QueryResultIterator SIP_SKIP
165 {
167 QVariantList nextRow();
168
170 bool hasNextRow() const;
171
173 long long fetchedRowCount();
174
176 long long rowCount();
177
178 virtual ~QueryResultIterator() = default;
179
180 private:
181
182 virtual QVariantList nextRowPrivate() = 0;
183 virtual bool hasNextRowPrivate() const = 0;
184 virtual long long rowCountPrivate() const = 0;
185
186 mutable qlonglong mFetchedRowCount = 0;
187 mutable QMutex mMutex;
188
189 };
190
196 void appendColumn( const QString &columnName ) SIP_SKIP;
197
203 QueryResult( std::shared_ptr<QueryResultIterator> iterator ) SIP_SKIP;
204
209 QueryResult( ) = default SIP_SKIP;
210
214 double queryExecutionTime( ) const;
215
219 void setQueryExecutionTime( double queryExecutionTime );
220
222
223 private:
224
225 mutable std::shared_ptr<QueryResultIterator> mResultIterator;
226 QStringList mColumns;
228 double mQueryExecutionTime = 0;
229
230 };
231
238 struct CORE_EXPORT SqlVectorLayerOptions
239 {
241 QString sql;
243 QString filter;
245 QString layerName;
247 QStringList primaryKeyColumns;
251 bool disableSelectAtId = false;
252
253 };
254
266 struct CORE_EXPORT TableProperty
267 {
268
269#ifdef SIP_RUN
270 SIP_PYOBJECT __repr__();
271 % MethodCode
272 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
273 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
274 % End
275#endif
276
281 struct CORE_EXPORT GeometryColumnType
282 {
283#ifdef SIP_RUN
284 SIP_PYOBJECT __repr__();
285 % MethodCode
286 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
287 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
288 % End
289#endif
292
293 // TODO c++20 - replace with = default
294 inline bool operator==( const GeometryColumnType &other ) const
295 {
296 return this->crs == other.crs && this->wkbType == other.wkbType;
297 }
298 };
299
300 public:
301
307 QString tableName() const;
308
314 void setTableName( const QString &name );
315
319 void addGeometryColumnType( const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs );
320
326 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
327
331 void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
332
340 QString defaultName() const;
341
346 TableProperty at( int index ) const;
347
351 QString schema() const;
352
356 void setSchema( const QString &schema );
357
361 QString geometryColumn() const;
362
366 void setGeometryColumn( const QString &geometryColumn );
367
371 QStringList primaryKeyColumns() const;
372
376 void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
377
381 QList<QgsCoordinateReferenceSystem> crsList() const;
382
386 TableFlags flags() const;
387
391 void setFlags( const TableFlags &flags );
392
396 QString comment() const;
397
401 void setComment( const QString &comment );
402
409 QVariantMap info() const;
410
417 void setInfo( const QVariantMap &info );
418
424 int geometryColumnCount() const;
425
429 void setGeometryColumnCount( int geometryColumnCount );
430
434 void setFlag( const TableFlag &flag );
435
442 int maxCoordinateDimensions() const;
443
445
446 private:
447
449 QList<GeometryColumnType> mGeometryColumnTypes;
451 QString mSchema;
453 QString mTableName;
455 QString mGeometryColumn;
457 int mGeometryColumnCount;
459 QStringList mPkColumns;
460 TableFlags mFlags;
461 QString mComment;
463 QVariantMap mInfo;
464 };
465
471 struct CORE_EXPORT SpatialIndexOptions
472 {
475 };
476
481 {
482 CreateVectorTable = 1 << 1,
483 DropRasterTable = 1 << 2,
484 DropVectorTable = 1 << 3,
485 RenameVectorTable = 1 << 4,
486 RenameRasterTable = 1 << 5,
487 CreateSchema = 1 << 6,
488 DropSchema = 1 << 7,
489 RenameSchema = 1 << 8,
490 ExecuteSql = 1 << 9,
491 Vacuum = 1 << 10,
492 Tables = 1 << 11,
493 Schemas = 1 << 12,
494 SqlLayers = 1 << 13,
495 TableExists = 1 << 14,
496 Spatial = 1 << 15,
497 CreateSpatialIndex = 1 << 16,
498 SpatialIndexExists = 1 << 17,
499 DeleteSpatialIndex = 1 << 18,
500 DeleteField = 1 << 19,
501 DeleteFieldCascade = 1 << 20,
502 AddField = 1 << 21,
503 ListFieldDomains = 1 << 22,
504 RetrieveFieldDomain = 1 << 23,
505 SetFieldDomain = 1 << 24,
506 AddFieldDomain = 1 << 25,
507 RenameField = 1 << 26,
508 RetrieveRelationships = 1 << 27,
509 };
510 Q_ENUM( Capability )
511 Q_DECLARE_FLAGS( Capabilities, Capability )
512 Q_FLAG( Capabilities )
513
514
520 {
521 Z = 1 << 1,
522 M = 1 << 2,
523 SinglePart = 1 << 3,
524 Curves = 1 << 4,
525 SinglePoint = 1 << 5,
526 SingleLineString = 1 << 6,
527 SinglePolygon = 1 << 7,
528 };
529 // TODO QGIS 4.0 -- remove SinglePart
530
531 Q_ENUM( GeometryColumnCapability )
532 Q_DECLARE_FLAGS( GeometryColumnCapabilities, GeometryColumnCapability )
533 Q_FLAG( GeometryColumnCapabilities )
534
535
540 QgsAbstractDatabaseProviderConnection( const QString &name );
541
548 QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
549
550
551 // Public interface
552
556 Capabilities capabilities() const;
557
563 virtual GeometryColumnCapabilities geometryColumnCapabilities();
564
569 virtual Qgis::SqlLayerDefinitionCapabilities sqlLayerDefinitionCapabilities();
570
571 // Operations interface
572
579 virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
580
586 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 );
587
593 virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
594
601 virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
602
609 virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
610
617 virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
618
625 virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
626
632 virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
633
642 virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
643
655 virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
656
667 virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
668
682 virtual void renameField( const QString &schema, const QString &tableName, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
683
690 virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
691
698 virtual QList<QList<QVariant>> executeSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
699
705 virtual QgsVectorLayer *createSqlVectorLayer( const SqlVectorLayerOptions &options ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
706
714 virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource ) SIP_THROW( QgsProviderConnectionException );
715
723 virtual QueryResult execSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
724
730 virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
731
740 virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
741
749 virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
750
758 virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
759
768 virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = QgsAbstractDatabaseProviderConnection::TableFlags() ) const SIP_SKIP;
769
777 virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
778
786 QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
787
788
789 // TODO: return more schema information and not just the name
790
796 virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
797
808 virtual QgsFields fields( const QString &schema, const QString &table ) const SIP_THROW( QgsProviderConnectionException );
809
816 virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const SIP_THROW( QgsProviderConnectionException ) = 0;
817
823 QString providerKey() const;
824
834 virtual QMultiMap<Qgis::SqlKeywordCategory, QStringList> sqlDictionary();
835
846 virtual QStringList fieldDomainNames() const SIP_THROW( QgsProviderConnectionException );
847
853 virtual QList< Qgis::FieldDomainType > supportedFieldDomainTypes() const;
854
867 virtual QgsFieldDomain *fieldDomain( const QString &name ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
868
880 virtual void setFieldDomainName( const QString &fieldName, const QString &schema, const QString &tableName, const QString &domainName ) const SIP_THROW( QgsProviderConnectionException );
881
891 virtual void addFieldDomain( const QgsFieldDomain &domain, const QString &schema ) const SIP_THROW( QgsProviderConnectionException );
892
905 virtual QList< QgsWeakRelation > relationships( const QString &schema = QString(), const QString &tableName = QString() ) const SIP_THROW( QgsProviderConnectionException );
906
907 protected:
908
910
916 void checkCapability( Capability capability ) const;
918
919 Capabilities mCapabilities = Capabilities() SIP_SKIP;
920 GeometryColumnCapabilities mGeometryColumnCapabilities = GeometryColumnCapabilities() SIP_SKIP;
921 Qgis::SqlLayerDefinitionCapabilities mSqlLayerDefinitionCapabilities = Qgis::SqlLayerDefinitionCapabilities() SIP_SKIP;
922 QString mProviderKey;
923
924};
925
927
928#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
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
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_THROW(name)
Definition: qgis_sip.h:198
#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
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.