Skip to content

Commit

Permalink
[gpkg] Allow raster table rename for GDAL >= 3.10 (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
agiudiceandrea authored Jun 20, 2024
1 parent 684a802 commit 924402e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/core/providers/ogr/qgsgeopackageproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,17 @@ void QgsGeoPackageProviderConnection::dropRasterTable( const QString &schema, co
executeGdalSqlPrivate( QStringLiteral( "DROP TABLE %1" ).arg( name ) );
}


void QgsGeoPackageProviderConnection::renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const
void QgsGeoPackageProviderConnection::renameTablePrivate( const QString &schema, const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameVectorTable );
if ( ! schema.isEmpty() )
{
QgsMessageLog::logMessage( QStringLiteral( "Schema is not supported by GPKG, ignoring" ), QStringLiteral( "OGR" ), Qgis::MessageLevel::Info );
}
// TODO: maybe an index?
QString sql( QStringLiteral( "ALTER TABLE %1 RENAME TO %2" )
.arg( QgsSqliteUtils::quotedIdentifier( name ),
QgsSqliteUtils::quotedIdentifier( newName ) ) );
executeGdalSqlPrivate( sql );
// This is also done by GDAL (at least by current version)
sql = QStringLiteral( "UPDATE layer_styles SET f_table_name = %2 WHERE f_table_name = %1" )
.arg( QgsSqliteUtils::quotedString( name ),
QgsSqliteUtils::quotedString( newName ) );
Expand All @@ -132,6 +130,20 @@ void QgsGeoPackageProviderConnection::renameVectorTable( const QString &schema,
}
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,10,0)
void QgsGeoPackageProviderConnection::renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameRasterTable );
renameTablePrivate( schema, name, newName );
}
#endif

void QgsGeoPackageProviderConnection::renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const
{
checkCapability( Capability::RenameVectorTable );
renameTablePrivate( schema, name, newName );
}

void QgsGeoPackageProviderConnection::vacuum( const QString &schema, const QString &name ) const
{
Q_UNUSED( name );
Expand Down Expand Up @@ -308,6 +320,9 @@ void QgsGeoPackageProviderConnection::setDefaultCapabilities()
Capability::Tables,
Capability::CreateVectorTable,
Capability::DropVectorTable,
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,10,0)
Capability::RenameRasterTable,
#endif
Capability::RenameVectorTable,
Capability::Vacuum,
Capability::Spatial,
Expand Down
6 changes: 6 additions & 0 deletions src/core/providers/ogr/qgsgeopackageproviderconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ class QgsGeoPackageProviderConnection : public QgsOgrProviderConnection

// QgsAbstractProviderConnection interface
public:

void store( const QString &name ) const override;
void remove( const QString &name ) const override;
QgsAbstractDatabaseProviderConnection::TableProperty table( const QString &schema, const QString &table, QgsFeedback *feedback = nullptr ) const override;
QString tableUri( const QString &schema, const QString &name ) const override;
void dropRasterTable( const QString &schema, const QString &name ) const override;
void renameVectorTable( const QString &schema, const QString &name, const QString &newName ) const override;
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,10,0)
void renameRasterTable( const QString &schema, const QString &name, const QString &newName ) const override;
#endif
void vacuum( const QString &schema, const QString &name ) const override;
void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const override;
bool spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const override;
Expand All @@ -52,12 +56,14 @@ class QgsGeoPackageProviderConnection : public QgsOgrProviderConnection
QList<QgsLayerMetadataProviderResult> searchLayerMetadata( const QgsMetadataSearchContext &searchContext, const QString &searchString, const QgsRectangle &geographicExtent, QgsFeedback *feedback ) const override;

protected:

QString databaseQueryLogIdentifier() const override;
QString primaryKeyColumnName( const QString &table ) const override;

private:

void setDefaultCapabilities();
void renameTablePrivate( const QString &schema, const QString &name, const QString &newName ) const;

};

Expand Down
20 changes: 19 additions & 1 deletion tests/src/python/test_qgsproviderconnection_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ def test_gpkg_connections(self):
self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.CreateVectorTable))
self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.DropVectorTable))
self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.RenameVectorTable))
self.assertFalse(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.RenameRasterTable))
if int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 10, 0):
self.assertFalse(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.RenameRasterTable))
else:
self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.Capability.RenameRasterTable))

crs = QgsCoordinateReferenceSystem.fromEpsgId(3857)
typ = QgsWkbTypes.Type.LineString
Expand Down Expand Up @@ -370,6 +373,21 @@ def test_searchLayerMetadata_buggy_extent(self):
res = conn.searchLayerMetadata(QgsMetadataSearchContext())
self.assertTrue(res[0].geographicExtent().isEmpty())

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 10, 0), "GDAL 3.10 required")
def test_rename_raster_layer(self):
"""Test renaming a raster layer"""

md = QgsProviderRegistry.instance().providerMetadata(self.providerKey)
conn = md.createConnection(self.uri, {})

tables = conn.tables('', QgsAbstractDatabaseProviderConnection.TableFlag.Raster)
osm = tables[0]
self.assertEqual(osm.tableName(), 'osm')
conn.renameRasterTable('', 'osm', 'osm_new')
tables = conn.tables('', QgsAbstractDatabaseProviderConnection.TableFlag.Raster)
osm = tables[0]
self.assertEqual(osm.tableName(), 'osm_new')


if __name__ == '__main__':
unittest.main()

0 comments on commit 924402e

Please sign in to comment.