QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 IncludeSystemTables = 1 << 7,
69 };
70
71 Q_ENUM( TableFlag )
72 Q_DECLARE_FLAGS( TableFlags, TableFlag )
73 Q_FLAG( TableFlags )
74
87 struct CORE_EXPORT QueryResult
88 {
89
93 QStringList columns() const;
94
102 QList<QList<QVariant> > rows( QgsFeedback *feedback = nullptr );
103
110 bool hasNextRow() const;
111
118 QList<QVariant> nextRow() const;
119
125 long long fetchedRowCount( ) const;
126
132 long long rowCount( ) const;
133
134
135#ifdef SIP_RUN
136 // Python iterator
137 QueryResult *__iter__();
138 % MethodCode
139 sipRes = sipCpp;
140 % End
141
142 SIP_PYOBJECT __next__();
143 % MethodCode
144 QList<QVariant> result;
145 Py_BEGIN_ALLOW_THREADS
146 result = sipCpp->nextRow( );
147 Py_END_ALLOW_THREADS
148 if ( ! result.isEmpty() )
149 {
150 const sipTypeDef *qvariantlist_type = sipFindType( "QList<QVariant>" );
151 sipRes = sipConvertFromNewType( new QList<QVariant>( result ), qvariantlist_type, Py_None );
152 }
153 else
154 {
155 PyErr_SetString( PyExc_StopIteration, "" );
156 }
157 % End
158#endif
159
161
167 struct CORE_EXPORT QueryResultIterator SIP_SKIP
168 {
170 QVariantList nextRow();
171
173 bool hasNextRow() const;
174
176 long long fetchedRowCount();
177
179 long long rowCount();
180
181 virtual ~QueryResultIterator() = default;
182
183 private:
184
185 virtual QVariantList nextRowPrivate() = 0;
186 virtual bool hasNextRowPrivate() const = 0;
187 virtual long long rowCountPrivate() const = 0;
188
189 mutable qlonglong mFetchedRowCount = 0;
190 mutable QMutex mMutex;
191
192 };
193
199 void appendColumn( const QString &columnName ) SIP_SKIP;
200
206 QueryResult( std::shared_ptr<QueryResultIterator> iterator ) SIP_SKIP;
207
212 QueryResult( ) = default SIP_SKIP;
213
217 double queryExecutionTime( ) const;
218
222 void setQueryExecutionTime( double queryExecutionTime );
223
225
226 private:
227
228 mutable std::shared_ptr<QueryResultIterator> mResultIterator;
229 QStringList mColumns;
231 double mQueryExecutionTime = 0;
232
233 };
234
241 struct CORE_EXPORT SqlVectorLayerOptions
242 {
244 QString sql;
246 QString filter;
248 QString layerName;
250 QStringList primaryKeyColumns;
254 bool disableSelectAtId = false;
255
256 };
257
269 struct CORE_EXPORT TableProperty
270 {
271
272#ifdef SIP_RUN
273 SIP_PYOBJECT __repr__();
274 % MethodCode
275 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty: '%1'>" ).arg( sipCpp->tableName() );
276 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
277 % End
278#endif
279
284 struct CORE_EXPORT GeometryColumnType
285 {
286#ifdef SIP_RUN
287 SIP_PYOBJECT __repr__();
288 % MethodCode
289 QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType: '%1, %2'>" ).arg( QgsWkbTypes::displayString( sipCpp->wkbType ), sipCpp->crs.authid() );
290 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
291 % End
292#endif
295
296 // TODO c++20 - replace with = default
297 inline bool operator==( const GeometryColumnType &other ) const
298 {
299 return this->crs == other.crs && this->wkbType == other.wkbType;
300 }
301 };
302
303 public:
304
310 QString tableName() const;
311
317 void setTableName( const QString &name );
318
322 void addGeometryColumnType( Qgis::WkbType type, const QgsCoordinateReferenceSystem &crs );
323
329 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> geometryColumnTypes() const;
330
334 void setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &geometryColumnTypes );
335
343 QString defaultName() const;
344
349 TableProperty at( int index ) const;
350
354 QString schema() const;
355
359 void setSchema( const QString &schema );
360
364 QString geometryColumn() const;
365
369 void setGeometryColumn( const QString &geometryColumn );
370
374 QStringList primaryKeyColumns() const;
375
379 void setPrimaryKeyColumns( const QStringList &primaryKeyColumns );
380
384 QList<QgsCoordinateReferenceSystem> crsList() const;
385
389 TableFlags flags() const;
390
394 void setFlags( const TableFlags &flags );
395
399 QString comment() const;
400
404 void setComment( const QString &comment );
405
412 QVariantMap info() const;
413
420 void setInfo( const QVariantMap &info );
421
427 int geometryColumnCount() const;
428
432 void setGeometryColumnCount( int geometryColumnCount );
433
437 void setFlag( const TableFlag &flag );
438
445 int maxCoordinateDimensions() const;
446
448
449 private:
450
452 QList<GeometryColumnType> mGeometryColumnTypes;
454 QString mSchema;
456 QString mTableName;
458 QString mGeometryColumn;
460 int mGeometryColumnCount;
462 QStringList mPkColumns;
463 TableFlags mFlags;
464 QString mComment;
466 QVariantMap mInfo;
467 };
468
474 struct CORE_EXPORT SpatialIndexOptions
475 {
478 };
479
486 {
487 CreateVectorTable = 1 << 1,
488 DropRasterTable = 1 << 2,
489 DropVectorTable = 1 << 3,
490 RenameVectorTable = 1 << 4,
491 RenameRasterTable = 1 << 5,
492 CreateSchema = 1 << 6,
493 DropSchema = 1 << 7,
494 RenameSchema = 1 << 8,
495 ExecuteSql = 1 << 9,
496 Vacuum = 1 << 10,
497 Tables = 1 << 11,
498 Schemas = 1 << 12,
499 SqlLayers = 1 << 13,
500 TableExists = 1 << 14,
501 Spatial = 1 << 15,
502 CreateSpatialIndex = 1 << 16,
503 SpatialIndexExists = 1 << 17,
504 DeleteSpatialIndex = 1 << 18,
505 DeleteField = 1 << 19,
506 DeleteFieldCascade = 1 << 20,
507 AddField = 1 << 21,
508 ListFieldDomains = 1 << 22,
509 RetrieveFieldDomain = 1 << 23,
510 SetFieldDomain = 1 << 24,
511 AddFieldDomain = 1 << 25,
512 RenameField = 1 << 26,
513 RetrieveRelationships = 1 << 27,
514 AddRelationship = 1 << 28,
515 UpdateRelationship = 1 << 29,
516 DeleteRelationship = 1 << 30,
517 };
518 Q_ENUM( Capability )
519 Q_DECLARE_FLAGS( Capabilities, Capability )
520 Q_FLAG( Capabilities )
521
528 {
529 Z = 1 << 1,
530 M = 1 << 2,
531 SinglePart = 1 << 3,
532 Curves = 1 << 4,
533 SinglePoint = 1 << 5,
534 SingleLineString = 1 << 6,
535 SinglePolygon = 1 << 7,
536 PolyhedralSurfaces = 1 << 8,
537 };
538 // TODO QGIS 4.0 -- remove SinglePart
539
540 Q_ENUM( GeometryColumnCapability )
543
549 QgsAbstractDatabaseProviderConnection( const QString &name );
550
557 QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration );
558
559
560 // Public interface
561
567 Capabilities capabilities() const;
568
575 Qgis::DatabaseProviderConnectionCapabilities2 capabilities2() const;
576
582 virtual GeometryColumnCapabilities geometryColumnCapabilities();
583
588 virtual Qgis::SqlLayerDefinitionCapabilities sqlLayerDefinitionCapabilities();
589
590 // Operations interface
591
598 virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
599
605 virtual void createVectorTable( const QString &schema, const QString &name, const QgsFields &fields, Qgis::WkbType wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, const QMap<QString, QVariant> *options ) const SIP_THROW( QgsProviderConnectionException );
606
612 virtual bool tableExists( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
613
620 virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
621
628 virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
629
636 virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
637
644 virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
645
651 virtual void createSchema( const QString &name ) const SIP_THROW( QgsProviderConnectionException );
652
661 virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
662
674 virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
675
686 virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
687
701 virtual void renameField( const QString &schema, const QString &tableName, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
702
709 virtual void renameSchema( const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );
710
717 virtual QList<QList<QVariant>> executeSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
718
724 virtual QgsVectorLayer *createSqlVectorLayer( const SqlVectorLayerOptions &options ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
725
733 virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource ) SIP_THROW( QgsProviderConnectionException );
734
742 virtual QueryResult execSql( const QString &sql, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
743
749 virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );
750
759 virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );
760
768 virtual bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
769
777 virtual void deleteSpatialIndex( const QString &schema, const QString &name, const QString &geometryColumn ) const SIP_THROW( QgsProviderConnectionException );
778
788 virtual QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags = QgsAbstractDatabaseProviderConnection::TableFlags(), QgsFeedback *feedback = nullptr ) const SIP_SKIP;
789
799 virtual QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
800
808 QList<QgsAbstractDatabaseProviderConnection::TableProperty> tablesInt( const QString &schema = QString(), const int flags = 0 ) const SIP_THROW( QgsProviderConnectionException ) SIP_PYNAME( tables );
809
810
811 // TODO: return more schema information and not just the name
812
818 virtual QStringList schemas() const SIP_THROW( QgsProviderConnectionException );
819
832 virtual QgsFields fields( const QString &schema, const QString &table, QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException );
833
840 virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const SIP_THROW( QgsProviderConnectionException ) = 0;
841
847 QString providerKey() const;
848
858 virtual QMultiMap<Qgis::SqlKeywordCategory, QStringList> sqlDictionary();
859
866 virtual QSet< QString > illegalFieldNames() const;
867
878 virtual QStringList fieldDomainNames() const SIP_THROW( QgsProviderConnectionException );
879
885 virtual QList< Qgis::FieldDomainType > supportedFieldDomainTypes() const;
886
899 virtual QgsFieldDomain *fieldDomain( const QString &name ) const SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;
900
912 virtual void setFieldDomainName( const QString &fieldName, const QString &schema, const QString &tableName, const QString &domainName ) const SIP_THROW( QgsProviderConnectionException );
913
923 virtual void addFieldDomain( const QgsFieldDomain &domain, const QString &schema ) const SIP_THROW( QgsProviderConnectionException );
924
936 virtual void setFieldAlias( const QString &fieldName, const QString &schema, const QString &tableName, const QString &alias ) const SIP_THROW( QgsProviderConnectionException );
937
949 virtual void setFieldComment( const QString &fieldName, const QString &schema, const QString &tableName, const QString &comment ) const SIP_THROW( QgsProviderConnectionException );
950
956#ifndef SIP_RUN
957 virtual QList< Qgis::RelationshipCardinality > supportedRelationshipCardinalities() const;
958#else
959 SIP_PYOBJECT supportedRelationshipCardinalities() const SIP_TYPEHINT( List[Qgis.RelationshipCardinality] );
960 % MethodCode
961 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
962
963 const QList< Qgis::RelationshipCardinality > cppRes = sipCpp->supportedRelationshipCardinalities();
964
965 PyObject *l = PyList_New( cppRes.size() );
966
967 if ( !l )
968 sipIsErr = 1;
969 else
970 {
971 for ( int i = 0; i < cppRes.size(); ++i )
972 {
973 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
974 sipType_Qgis_RelationshipCardinality );
975
976 if ( !eobj )
977 {
978 sipIsErr = 1;
979 }
980
981 PyList_SetItem( l, i, eobj );
982 }
983
984 if ( !sipIsErr )
985 {
986 sipRes = l;
987 }
988 else
989 {
990 Py_DECREF( l );
991 }
992 }
993 % End
994#endif
995
1001#ifndef SIP_RUN
1002 virtual QList< Qgis::RelationshipStrength > supportedRelationshipStrengths() const;
1003#else
1004 SIP_PYOBJECT supportedRelationshipStrengths() const SIP_TYPEHINT( List[Qgis.RelationshipStrength] );
1005 % MethodCode
1006 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
1007
1008 const QList< Qgis::RelationshipStrength > cppRes = sipCpp->supportedRelationshipStrengths();
1009
1010 PyObject *l = PyList_New( cppRes.size() );
1011
1012 if ( !l )
1013 sipIsErr = 1;
1014 else
1015 {
1016 for ( int i = 0; i < cppRes.size(); ++i )
1017 {
1018 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
1019 sipType_Qgis_RelationshipStrength );
1020
1021 if ( !eobj )
1022 {
1023 sipIsErr = 1;
1024 }
1025
1026 PyList_SetItem( l, i, eobj );
1027 }
1028
1029 if ( !sipIsErr )
1030 {
1031 sipRes = l;
1032 }
1033 else
1034 {
1035 Py_DECREF( l );
1036 }
1037 }
1038 % End
1039#endif
1040
1046 virtual Qgis::RelationshipCapabilities supportedRelationshipCapabilities() const;
1047
1058 virtual QStringList relatedTableTypes() const;
1059
1072 virtual QList< QgsWeakRelation > relationships( const QString &schema = QString(), const QString &tableName = QString() ) const SIP_THROW( QgsProviderConnectionException );
1073
1080 virtual void addRelationship( const QgsWeakRelation &relationship ) const SIP_THROW( QgsProviderConnectionException );
1081
1088 virtual void updateRelationship( const QgsWeakRelation &relationship ) const SIP_THROW( QgsProviderConnectionException );
1089
1096 virtual void deleteRelationship( const QgsWeakRelation &relationship ) const SIP_THROW( QgsProviderConnectionException );
1097
1105 virtual QgsProviderSqlQueryBuilder *queryBuilder() const SIP_FACTORY;
1106
1127 virtual QList<QgsLayerMetadataProviderResult> searchLayerMetadata( const QgsMetadataSearchContext &searchContext, const QString &searchString = QString(), const QgsRectangle &geographicExtent = QgsRectangle(), QgsFeedback *feedback = nullptr ) const SIP_THROW( QgsProviderConnectionException, QgsNotSupportedException );
1128
1129 protected:
1130
1132
1138 void checkCapability( Capability capability ) const;
1139
1145 void checkCapability( Qgis::DatabaseProviderConnectionCapability2 capability ) const;
1147
1149 Qgis::DatabaseProviderConnectionCapabilities2 mCapabilities2 = Qgis::DatabaseProviderConnectionCapabilities2() SIP_SKIP;
1150
1152 Qgis::SqlLayerDefinitionCapabilities mSqlLayerDefinitionCapabilities = Qgis::SqlLayerDefinitionCapabilities() SIP_SKIP;
1153 QString mProviderKey;
1154 QSet<QString> mIllegalFieldNames SIP_SKIP;
1155
1156};
1157
1159
1160#endif // QGSABSTRACTDATABASEPROVIDERCONNECTION_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
QFlags< RelationshipCapability > RelationshipCapabilities
Relationship capabilities.
Definition qgis.h:4138
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
GeometryColumnCapability
The GeometryColumnCapability enum represents the geometry column features supported by the connection...
QFlags< GeometryColumnCapability > GeometryColumnCapabilities
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:44
Base class for field domains.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
Result record of layer metadata provider search.
Custom exception class which is raised when an operation is not supported.
Custom exception class for provider connection related exceptions.
Provides an interface for provider-specific creation of SQL queries.
A rectangle specified with double values.
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...
static QString displayString(Qgis::WkbType type)
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:39
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:232
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271
#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:203
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
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.