Skip to content

Commit 61d361d

Browse files
committedFeb 12, 2019
GPKG: Rename styles when layers are renamed
Partially fixes #21227 TODO: - DB manager - Other providers
1 parent 287a3b0 commit 61d361d

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed
 

‎src/providers/ogr/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,26 @@ INCLUDE_DIRECTORIES(SYSTEM
6464

6565

6666
ADD_LIBRARY(ogrprovider MODULE ${OGR_SRCS} ${OGR_MOC_SRCS})
67+
ADD_LIBRARY(ogrprovider_a STATIC ${OGR_SRCS} ${OGR_MOC_SRCS})
6768

6869
TARGET_LINK_LIBRARIES(ogrprovider
6970
qgis_core
7071
)
7172

73+
TARGET_LINK_LIBRARIES(ogrprovider_a
74+
qgis_core
75+
)
7276

7377
IF (WITH_GUI)
7478
TARGET_LINK_LIBRARIES (ogrprovider
7579
qgis_gui
7680
)
81+
TARGET_LINK_LIBRARIES (ogrprovider_a
82+
qgis_gui
83+
)
7784
ENDIF ()
7885

86+
7987
IF (MSVC)
8088
#needed for linking to gdal which needs odbc
8189
SET(TARGET_LINK_LIBRARIES ${TARGET_LINK_LIBRARIE} odbc32 odbccp32)

‎src/providers/ogr/qgsgeopackagedataitems.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "qgstaskmanager.h"
4242
#include "qgsproviderregistry.h"
4343
#include "qgsproxyprogresstask.h"
44+
#include "qgssqliteutils.h"
4445

4546

4647
QGISEXTERN bool deleteLayer( const QString &uri, const QString &errCause );
@@ -887,12 +888,23 @@ bool QgsGeoPackageVectorLayerItem::rename( const QString &name )
887888
GDALDatasetH hDS = GDALOpenEx( filePath.toUtf8().constData(), GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr );
888889
if ( hDS )
889890
{
890-
QString sql( QStringLiteral( "ALTER TABLE \"%1\" RENAME TO \"%2\"" ).arg( oldName, name ) );
891+
QString sql( QStringLiteral( "ALTER TABLE %1 RENAME TO %2" )
892+
.arg( QgsSqliteUtils::quotedIdentifier( oldName ),
893+
QgsSqliteUtils::quotedIdentifier( name ) ) );
891894
OGRLayerH ogrLayer( GDALDatasetExecuteSQL( hDS, sql.toUtf8().constData(), nullptr, nullptr ) );
892895
if ( ogrLayer )
893896
GDALDatasetReleaseResultSet( hDS, ogrLayer );
894-
GDALClose( hDS );
895897
errCause = CPLGetLastErrorMsg( );
898+
if ( errCause.isEmpty() )
899+
{
900+
sql = QStringLiteral( "UPDATE layer_styles SET f_table_name = %2 WHERE f_table_name = %1" )
901+
.arg( QgsSqliteUtils::quotedString( oldName ),
902+
QgsSqliteUtils::quotedString( name ) );
903+
ogrLayer = GDALDatasetExecuteSQL( hDS, sql.toUtf8().constData(), nullptr, nullptr );
904+
if ( ogrLayer )
905+
GDALDatasetReleaseResultSet( hDS, ogrLayer );
906+
}
907+
GDALClose( hDS );
896908
}
897909
else
898910
{

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6574,4 +6574,3 @@ QGISEXTERN QgsTransaction *createTransaction( const QString &connString )
65746574
return new QgsOgrTransaction( connString, ds );
65756575
}
65766576

6577-

‎tests/src/providers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
1717
${CMAKE_SOURCE_DIR}/src/providers/postgres
1818
${CMAKE_SOURCE_DIR}/src/providers/arcgisrest
1919
${CMAKE_SOURCE_DIR}/src/providers/mdal
20+
${CMAKE_SOURCE_DIR}/src/providers/ogr
2021
${CMAKE_SOURCE_DIR}/src/test
2122
${CMAKE_BINARY_DIR}/src/core
2223
)
@@ -74,6 +75,7 @@ ADD_QGIS_TEST(gdalprovidertest testqgsgdalprovider.cpp)
7475

7576
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
7677
ADD_QGIS_TEST(ogrprovidertest testqgsogrprovider.cpp)
78+
TARGET_LINK_LIBRARIES(qgis_ogrprovidertest ogrprovider_a)
7779

7880
ADD_QGIS_TEST(wmscapabilitiestest
7981
testqgswmscapabilities.cpp)

‎tests/src/providers/testqgsogrprovider.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <qgsproviderregistry.h>
2525
#include <qgsvectorlayer.h>
2626
#include <qgsnetworkaccessmanager.h>
27+
#include <qgsgeopackagedataitems.h>
28+
#include <qgsdataitem.h>
2729

2830
#include <QObject>
2931

@@ -47,6 +49,8 @@ class TestQgsOgrProvider : public QObject
4749
void setupProxy();
4850
void decodeUri();
4951
void testThread();
52+
//! Test GPKG data items rename
53+
void testGpkgDataItemRename();
5054

5155
private:
5256
QString mTestDataDir;
@@ -213,6 +217,37 @@ void TestQgsOgrProvider::testThread()
213217

214218
}
215219

220+
void TestQgsOgrProvider::testGpkgDataItemRename()
221+
{
222+
QTemporaryFile f( QStringLiteral( "qgis-XXXXXX.gpkg" ) );
223+
f.open();
224+
f.close();
225+
QString fileName { f.fileName( ) };
226+
f.remove();
227+
QVERIFY( QFile::copy( QStringLiteral( "%1/provider/bug_21227-rename-styles.gpkg" ).arg( mTestDataDir ), fileName ) );
228+
QgsGeoPackageVectorLayerItem item( nullptr,
229+
QStringLiteral( "Layer 1" ),
230+
QStringLiteral( "gpkg:/%1|layername=layer 1" )
231+
.arg( fileName ),
232+
QStringLiteral( "%1|layername=layer 1" ).arg( fileName ),
233+
QgsLayerItem::LayerType::TableLayer );
234+
item.rename( "layer 3" );
235+
// Check that the style is still available
236+
QgsVectorLayer metadataLayer( QStringLiteral( "/%1|layername=layer_styles" ).arg( fileName ) );
237+
QVERIFY( metadataLayer.isValid() );
238+
QgsFeature feature;
239+
QgsFeatureIterator it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 3'" ) ) ) );
240+
QVERIFY( it.nextFeature( feature ) );
241+
QVERIFY( feature.isValid() );
242+
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 1" ) );
243+
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 1' " ) ) ) );
244+
QVERIFY( !it.nextFeature( feature ) );
245+
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 2' " ) ) ) );
246+
QVERIFY( it.nextFeature( feature ) );
247+
QVERIFY( feature.isValid() );
248+
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 2" ) );
249+
}
250+
216251

217252
QGSTEST_MAIN( TestQgsOgrProvider )
218253
#include "testqgsogrprovider.moc"
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.