Skip to content
Permalink
Browse files

Add API for creating spatial indexes to QgsAbstractDatabaseProviderCo…

…nnection
  • Loading branch information
nyalldawson committed Mar 16, 2020
1 parent 470365a commit 6a4b67f7701b8e531d34f12e31e3e917474aef32
@@ -231,6 +231,7 @@ This information is calculated from the geometry columns types.
SqlLayers,
TableExists,
Spatial,
CreateSpatialIndex,
};

typedef QFlags<QgsAbstractDatabaseProviderConnection::Capability> Capabilities;
@@ -270,7 +271,7 @@ Raises a QgsProviderConnectionException if any errors are encountered.

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 throw( QgsProviderConnectionException );
%Docstring
Creates an empty table with ``name`` in the given ``schema`` (schema is ignored if not supported by the backend).
Creates an empty table with ``name`` in the given ``schema`` (schema is ignored if not supported by the backend).
Raises a QgsProviderConnectionException if any errors are encountered.

:raises :: py:class:`QgsProviderConnectionException`
@@ -298,7 +299,7 @@ Raises a QgsProviderConnectionException if any errors are encountered.

virtual void dropRasterTable( const QString &schema, const QString &name ) const throw( QgsProviderConnectionException );
%Docstring
Drops a raster table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Drops a raster table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Raises a QgsProviderConnectionException if any errors are encountered.

.. note::
@@ -310,7 +311,7 @@ Raises a QgsProviderConnectionException if any errors are encountered.

virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const throw( QgsProviderConnectionException );
%Docstring
Renames a vector or aspatial table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Renames a vector or aspatial table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Raises a QgsProviderConnectionException if any errors are encountered.

.. note::
@@ -322,7 +323,7 @@ Raises a QgsProviderConnectionException if any errors are encountered.

virtual void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const throw( QgsProviderConnectionException );
%Docstring
Renames a raster table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Renames a raster table with given ``schema`` (schema is ignored if not supported by the backend) and ``name``.
Raises a QgsProviderConnectionException if any errors are encountered.

.. note::
@@ -376,7 +377,23 @@ Raises a QgsProviderConnectionException if any errors are encountered.

virtual void vacuum( const QString &schema, const QString &name ) const throw( QgsProviderConnectionException );
%Docstring
Vacuum the database table with given ``schema`` and ``name`` (schema is ignored if not supported by the backend).
Vacuum the database table with given ``schema`` and ``name`` (schema is ignored if not supported by the backend).
Raises a QgsProviderConnectionException if any errors are encountered.

:raises :: py:class:`QgsProviderConnectionException`
%End

struct SpatialIndexOptions
{
QString geometryColumnName;
};

virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const throw( QgsProviderConnectionException );
%Docstring
Creates a spatial index for the database table with given ``schema`` and ``name`` (schema is ignored if not supported by the backend).

The ``options`` argument can be used to provide extra options controlling the spatial index creation.

Raises a QgsProviderConnectionException if any errors are encountered.

:raises :: py:class:`QgsProviderConnectionException`
@@ -132,6 +132,11 @@ void QgsAbstractDatabaseProviderConnection::vacuum( const QString &, const QStri
checkCapability( Capability::Vacuum );
}

void QgsAbstractDatabaseProviderConnection::createSpatialIndex( const QString &, const QString &, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions & ) const
{
checkCapability( Capability::CreateSpatialIndex );
}

QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tables( const QString &, const QgsAbstractDatabaseProviderConnection::TableFlags & ) const
{
checkCapability( Capability::Tables );
@@ -290,6 +290,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
SqlLayers = 1 << 13, //!< Can create vector layers from SQL SELECT queries
TableExists = 1 << 14, //!< Can check if table exists
Spatial = 1 << 15, //!< The connection supports spatial tables
CreateSpatialIndex = 1 << 16, //!< The connection can create spatial indices
};

Q_ENUM( Capability )
@@ -328,7 +329,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
virtual QString tableUri( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );

/**
* Creates an empty table with \a name in the given \a schema (schema is ignored if not supported by the backend).
* Creates an empty table with \a name in the given \a schema (schema is ignored if not supported by the backend).
* Raises a QgsProviderConnectionException if any errors are encountered.
* \throws QgsProviderConnectionException
*/
@@ -350,23 +351,23 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
virtual void dropVectorTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );

/**
* Drops a raster table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Drops a raster table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Raises a QgsProviderConnectionException if any errors are encountered.
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
*/
virtual void dropRasterTable( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );

/**
* Renames a vector or aspatial table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Renames a vector or aspatial table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Raises a QgsProviderConnectionException if any errors are encountered.
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
*/
virtual void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const SIP_THROW( QgsProviderConnectionException );

/**
* Renames a raster table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Renames a raster table with given \a schema (schema is ignored if not supported by the backend) and \a name.
* Raises a QgsProviderConnectionException if any errors are encountered.
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
@@ -405,12 +406,33 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
virtual QList<QList<QVariant>> executeSql( const QString &sql ) const SIP_THROW( QgsProviderConnectionException );

/**
* Vacuum the database table with given \a schema and \a name (schema is ignored if not supported by the backend).
* Vacuum the database table with given \a schema and \a name (schema is ignored if not supported by the backend).
* Raises a QgsProviderConnectionException if any errors are encountered.
* \throws QgsProviderConnectionException
*/
virtual void vacuum( const QString &schema, const QString &name ) const SIP_THROW( QgsProviderConnectionException );

/**
* Contains extra options relating to spatial index creation.
*
* \since QGIS 3.14
*/
struct CORE_EXPORT SpatialIndexOptions
{
//! Specifies the name of the geometry column to create the index for
QString geometryColumnName;
};

/**
* Creates a spatial index for the database table with given \a schema and \a name (schema is ignored if not supported by the backend).
*
* The \a options argument can be used to provide extra options controlling the spatial index creation.
*
* Raises a QgsProviderConnectionException if any errors are encountered.
* \throws QgsProviderConnectionException
*/
virtual void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const SIP_THROW( QgsProviderConnectionException );

/**
* Returns information on the tables in the given schema.
* Raises a QgsProviderConnectionException if any errors are encountered.
@@ -273,6 +273,10 @@ def _test_operations(self, md, conn):
if capabilities & QgsAbstractDatabaseProviderConnection.Vacuum:
conn.vacuum('myNewSchema', 'myNewTable')

# Create spatial index
if capabilities & QgsAbstractDatabaseProviderConnection.CreateSpatialIndex:
conn.createSpatialIndex('myNewSchema', 'myNewTable')

if capabilities & QgsAbstractDatabaseProviderConnection.DropSchema:
# Drop schema (should fail)
with self.assertRaises(QgsProviderConnectionException) as ex:

0 comments on commit 6a4b67f

Please sign in to comment.
You can’t perform that action at this time.