From c327bd3577a17d5567eab0d63aa1516bd74e3508 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Thu, 20 Jun 2024 10:12:27 +0200 Subject: [PATCH] [gpkg] Allow raster table rename for GDAL >= 3.10 Related to #57754 --- .../ogr/qgsgeopackageproviderconnection.cpp | 23 +++++++++++++++---- .../ogr/qgsgeopackageproviderconnection.h | 6 +++++ .../test_qgsproviderconnection_ogr_gpkg.py | 20 +++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/core/providers/ogr/qgsgeopackageproviderconnection.cpp b/src/core/providers/ogr/qgsgeopackageproviderconnection.cpp index 9352077be460..2e665c5575dd 100644 --- a/src/core/providers/ogr/qgsgeopackageproviderconnection.cpp +++ b/src/core/providers/ogr/qgsgeopackageproviderconnection.cpp @@ -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 ) ); @@ -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 ); @@ -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, diff --git a/src/core/providers/ogr/qgsgeopackageproviderconnection.h b/src/core/providers/ogr/qgsgeopackageproviderconnection.h index 1bfb484d5177..e78ffa5d5eda 100644 --- a/src/core/providers/ogr/qgsgeopackageproviderconnection.h +++ b/src/core/providers/ogr/qgsgeopackageproviderconnection.h @@ -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; @@ -52,12 +56,14 @@ class QgsGeoPackageProviderConnection : public QgsOgrProviderConnection QList 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; }; diff --git a/tests/src/python/test_qgsproviderconnection_ogr_gpkg.py b/tests/src/python/test_qgsproviderconnection_ogr_gpkg.py index 2dc3e0238e51..0f5d4ed277fc 100644 --- a/tests/src/python/test_qgsproviderconnection_ogr_gpkg.py +++ b/tests/src/python/test_qgsproviderconnection_ogr_gpkg.py @@ -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 @@ -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()