58 static QMetaEnum metaEnum = QMetaEnum::fromType<QgsAbstractDatabaseProviderConnection::Capability>();
59 const QString capName { metaEnum.valueToKey( capability ) };
76 const QMap<QString, QVariant> *
83 Q_UNUSED( overwrite );
91 checkCapability( Capability::RenameVectorTable );
96 checkCapability( Capability::RenameRasterTable );
101 checkCapability( Capability::DropVectorTable );
106 checkCapability( Capability::TableExists );
107 const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables {
tables( schema ) };
108 for (
const auto &t : constTables )
110 if ( t.tableName() == name )
120 checkCapability( Capability::DropRasterTable );
125 checkCapability( Capability::CreateSchema );
130 checkCapability( Capability::DropSchema );
135 checkCapability( Capability::RenameSchema );
146 checkCapability( Capability::ExecuteSql );
153 checkCapability( Capability::Vacuum );
158 checkCapability( Capability::CreateSpatialIndex );
163 checkCapability( Capability::DeleteSpatialIndex );
168 checkCapability( Capability::SpatialIndexExists );
174 checkCapability( Capability::DeleteField );
178 std::unique_ptr<QgsVectorLayer> vl { std::make_unique<QgsVectorLayer>(
tableUri( schema, tableName ), QStringLiteral(
"temp_layer" ),
mProviderKey, options ) };
179 if ( ! vl->isValid() )
182 .arg( tableName, schema ) );
184 if ( vl->fields().lookupField( fieldName ) == -1 )
187 .arg( fieldName, tableName, schema ) );
190 if ( ! vl->dataProvider()->deleteAttributes( { vl->fields().lookupField( fieldName ) } ) )
193 .arg( fieldName, tableName, schema ) );
199 checkCapability( Capability::AddField );
203 std::unique_ptr<QgsVectorLayer> vl( std::make_unique<QgsVectorLayer>(
tableUri( schema, tableName ), QStringLiteral(
"temp_layer" ),
mProviderKey, options ) );
204 if ( ! vl->isValid() )
207 .arg( tableName, schema ) );
209 if ( vl->fields().lookupField(
field.
name() ) != -1 )
212 .arg(
field.
name(), tableName, schema ) );
215 if ( ! vl->dataProvider()->addAttributes( { field } ) )
218 .arg(
field.
name(), tableName, schema ) );
224 checkCapability( Capability::Tables );
225 return QList<QgsAbstractDatabaseProviderConnection::TableProperty>();
231 checkCapability( Capability::Tables );
232 const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables {
tables( schema ) };
233 for (
const auto &t : constTables )
235 if ( t.tableName() == name )
241 .arg( name, schema ) );
246 return tables( schema,
static_cast<QgsAbstractDatabaseProviderConnection::TableFlags
>( flags ) );
252 checkCapability( Capability::Schemas );
253 return QStringList();
270 for (
const auto &t : std::as_const( mGeometryColumnTypes ) )
277 mGeometryColumnTypes.push_back( toAdd );
282 return mGeometryColumnTypes;
302 QString n = mTableName;
303 if ( mGeometryColumnCount > 1 ) n +=
'.' + mGeometryColumn;
311 Q_ASSERT( index >= 0 && index < mGeometryColumnTypes.size() );
313 property.mGeometryColumnTypes << mGeometryColumnTypes[ index ];
314 property.mSchema = mSchema;
315 property.mTableName = mTableName;
316 property.mGeometryColumn = mGeometryColumn;
317 property.mPkColumns = mPkColumns;
318 property.mGeometryColumnCount = mGeometryColumnCount;
319 property.mFlags = mFlags;
320 property.mComment = mComment;
321 property.mInfo = mInfo;
327 mFlags.setFlag( flag );
342 return mSchema == other.mSchema &&
343 mTableName == other.mTableName &&
344 mGeometryColumn == other.mGeometryColumn &&
345 mGeometryColumnCount == other.mGeometryColumnCount &&
346 mPkColumns == other.mPkColumns &&
347 mFlags == other.mFlags &&
348 mComment == other.mComment &&
349 mInfo == other.mInfo;
355 mGeometryColumnTypes = columnTypes;
361 return mGeometryColumnCount;
366 mGeometryColumnCount = geometryColumnCount;
401 QList<QgsCoordinateReferenceSystem> crss;
402 for (
const auto &t : std::as_const( mGeometryColumnTypes ) )
404 crss.push_back( t.crs );
416 mPkColumns = pkColumns;
421 return mGeometryColumn;
426 mGeometryColumn = geometryColumn;
450 QList<QList<QVariant> > rows;
452 while ( mResultIterator &&
453 mResultIterator->hasNextRow() &&
456 const QVariantList row( mResultIterator->nextRow() );
463 rows.push_back( row );
471 if ( ! mResultIterator )
473 return QList<QVariant>();
475 return mResultIterator->nextRow();
481 if ( ! mResultIterator )
485 return mResultIterator->fetchedRowCount();
491 if ( ! mResultIterator )
495 return mResultIterator->hasNextRow();
498 void QgsAbstractDatabaseProviderConnection::QueryResult::appendColumn(
const QString &columnName )
500 mColumns.push_back( columnName );
504 QgsAbstractDatabaseProviderConnection::QueryResult::QueryResult( std::shared_ptr<QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator> iterator )
505 : mResultIterator( iterator )
509 QVariantList QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::nextRow()
511 QMutexLocker lock( &mMutex );
512 const QVariantList row = nextRowPrivate();
513 if ( ! row.isEmpty() )
520 bool QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::hasNextRow()
const
522 QMutexLocker lock( &mMutex );
523 return hasNextRowPrivate();
526 qlonglong QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::fetchedRowCount()
528 QMutexLocker lock( &mMutex );
529 return mFetchedRowCount;
virtual void vacuum(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Vacuum the database table with given schema and name (schema is ignored if not supported by the backe...
virtual QueryResult execSql(const QString &sql, QgsFeedback *feedback=nullptr) const SIP_THROW(QgsProviderConnectionException)
Executes raw sql and returns the (possibly empty) query results, optionally feedback can be provided.
Capabilities mCapabilities
QString providerKey() const
Returns the provider key.
virtual bool tableExists(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Checks whether a table name exists in the given schema.
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)
Creates an empty table with name in the given schema (schema is ignored if not supported by the backe...
virtual void deleteSpatialIndex(const QString &schema, const QString &name, const QString &geometryColumn) const SIP_THROW(QgsProviderConnectionException)
Deletes the existing spatial index for the database table with given schema, name and geometryColumn ...
Capability
The Capability enum represents the operations supported by the connection.
virtual void deleteField(const QString &fieldName, const QString &schema, const QString &tableName, bool force=false) const SIP_THROW(QgsProviderConnectionException)
Deletes the field with the specified name.
GeometryColumnCapabilities mGeometryColumnCapabilities
virtual void createSchema(const QString &name) const SIP_THROW(QgsProviderConnectionException)
Creates a new schema with the specified name.
virtual GeometryColumnCapabilities geometryColumnCapabilities()
Returns connection geomerty column capabilities (Z, M, SinglePart, Curves)
virtual void dropSchema(const QString &name, bool force=false) const SIP_THROW(QgsProviderConnectionException)
Drops an entire schema with the specified name.
virtual void addField(const QgsField &field, const QString &schema, const QString &tableName) const SIP_THROW(QgsProviderConnectionException)
Adds a field Raises a QgsProviderConnectionException if any errors are encountered.
virtual QList< QList< QVariant > > executeSql(const QString &sql, QgsFeedback *feedback=nullptr) const SIP_THROW(QgsProviderConnectionException)
Executes raw sql and returns the (possibly empty) list of results in a multi-dimensional array,...
virtual void renameRasterTable(const QString &schema, const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a raster table with given schema (schema is ignored if not supported by the backend) and name...
QList< QgsAbstractDatabaseProviderConnection::TableProperty > tablesInt(const QString &schema=QString(), const int flags=0) const SIP_THROW(QgsProviderConnectionException)
Returns information on the tables in the given schema.
virtual void renameVectorTable(const QString &schema, const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a vector or aspatial table with given schema (schema is ignored if not supported by the backe...
virtual bool spatialIndexExists(const QString &schema, const QString &name, const QString &geometryColumn) const SIP_THROW(QgsProviderConnectionException)
Determines whether a spatial index exists for the database table with given schema,...
virtual void renameSchema(const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a schema with the specified name.
virtual QgsAbstractDatabaseProviderConnection::TableProperty table(const QString &schema, const QString &table) const SIP_THROW(QgsProviderConnectionException)
Returns information on a table in the given schema.
virtual QStringList schemas() const SIP_THROW(QgsProviderConnectionException)
Returns information about the existing schemas.
virtual QString tableUri(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Returns the URI string for the given table and schema.
virtual QgsFields fields(const QString &schema, const QString &table) const SIP_THROW(QgsProviderConnectionException)
Returns the fields of a table and schema.
virtual void createSpatialIndex(const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options=QgsAbstractDatabaseProviderConnection::SpatialIndexOptions()) const SIP_THROW(QgsProviderConnectionException)
Creates a spatial index for the database table with given schema and name (schema is ignored if not s...
virtual void dropVectorTable(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Drops a vector (or aspatial) table with given schema (schema is ignored if not supported by the backe...
QgsAbstractDatabaseProviderConnection(const QString &name)
Creates a new connection with name by reading its configuration from the settings.
virtual void dropRasterTable(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Drops a raster table with given schema (schema is ignored if not supported by the backend) and name.
TableFlag
Flags for table properties.
virtual QList< QgsAbstractDatabaseProviderConnection::TableProperty > tables(const QString &schema=QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags=QgsAbstractDatabaseProviderConnection::TableFlags()) const
Returns information on the tables in the given schema.
Capabilities capabilities() const
Returns connection capabilities.
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.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
Custom exception class for provider connection related exceptions.
Represents a vector layer which manages a vector based data sets.
Type
The WKB type describes the number of dimensions a geometry has.
static int coordDimensions(Type type) SIP_HOLDGIL
Returns the coordinate dimension of the geometry type as an integer.
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.
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
The TableProperty class represents a database table or view.
void setTableName(const QString &name)
Sets the table name to name.
QString geometryColumn() const
Returns the geometry column name.
void addGeometryColumnType(const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs)
Appends the geometry column type with the given srid to the geometry column types list.
bool operator==(const QgsAbstractDatabaseProviderConnection::TableProperty &other) const
void setPrimaryKeyColumns(const QStringList &primaryKeyColumns)
Sets the primary key column names to primaryKeyColumns.
void setFlag(const TableFlag &flag)
Sets a flag.
QString defaultName() const
Returns the default name for the table entry.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.
void setGeometryColumn(const QString &geometryColumn)
Sets the geometry column name to geometryColumn.
TableProperty at(int index) const
Returns the table property corresponding to the geometry type at the given index.
int maxCoordinateDimensions() const
Returns the maximum coordinate dimensions of the geometries of a vector table.
QString tableName() const
Returns the table name.
int geometryColumnCount() const
Returns the number of geometry columns in the original table this entry refers to.
QList< QgsCoordinateReferenceSystem > crsList() const
Returns the list of CRSs supported by the geometry column.
void setSchema(const QString &schema)
Sets the schema.
QVariantMap info() const
Returns additional information about the table.
QString comment() const
Returns the table comment.
void setInfo(const QVariantMap &info)
Sets additional information about the table to info.
QStringList primaryKeyColumns() const
Returns the list of primary key column names.
void setGeometryColumnTypes(const QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > &geometryColumnTypes)
Sets the geometry column types to geometryColumnTypes.
void setComment(const QString &comment)
Sets the table comment.
QString schema() const
Returns the schema or an empty string for backends that do not support a schema.
TableFlags flags() const
Returns the table flags.
void setGeometryColumnCount(int geometryColumnCount)
Sets the geometryColumnCount.
void setFlags(const TableFlags &flags)
Sets the table flags.
Setting options for loading vector layers.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.