Skip to content

Commit

Permalink
Add API for creating spatial indexes to QgsAbstractDatabaseProviderCo…
Browse files Browse the repository at this point in the history
…nnection
  • Loading branch information
nyalldawson committed Mar 17, 2020
1 parent 470365a commit 6a4b67f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
Expand Up @@ -231,6 +231,7 @@ This information is calculated from the geometry columns types.
SqlLayers,
TableExists,
Spatial,
CreateSpatialIndex,
};

typedef QFlags<QgsAbstractDatabaseProviderConnection::Capability> Capabilities;
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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::
Expand All @@ -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::
Expand All @@ -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::
Expand Down Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsabstractdatabaseproviderconnection.cpp
Expand Up @@ -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 );
Expand Down
32 changes: 27 additions & 5 deletions src/core/qgsabstractdatabaseproviderconnection.h
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsproviderconnection_base.py
Expand Up @@ -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:
Expand Down

0 comments on commit 6a4b67f

Please sign in to comment.