Skip to content

Commit

Permalink
Merge pull request #6130 from elpaso/bugfix-17915-geopackage-delete-w…
Browse files Browse the repository at this point in the history
…rong-layer

[bugfix] Fixes 17915 geopackage delete wrong layer
  • Loading branch information
elpaso committed Jan 22, 2018
2 parents 5016142 + f235e93 commit f69c1cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
42 changes: 21 additions & 21 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -379,11 +379,11 @@ bool QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( const QString &ur
layerName.toUtf8().constData(), layerName.toUtf8().constData(),
layerName.toUtf8().constData() ); layerName.toUtf8().constData() );
status = sqlite3_exec( status = sqlite3_exec(
database.get(), /* An open database */ database.get(), /* An open database */
sql, /* SQL to be evaluated */ sql, /* SQL to be evaluated */
nullptr, /* Callback function */ nullptr, /* Callback function */
nullptr, /* 1st argument to callback */ nullptr, /* 1st argument to callback */
&errmsg /* Error msg written here */ &errmsg /* Error msg written here */
); );
sqlite3_free( sql ); sqlite3_free( sql );
// Remove from optional tables, may silently fail // Remove from optional tables, may silently fail
Expand All @@ -396,11 +396,11 @@ bool QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( const QString &ur
tableName.toUtf8().constData(), tableName.toUtf8().constData(),
layerName.toUtf8().constData() ); layerName.toUtf8().constData() );
( void )sqlite3_exec( ( void )sqlite3_exec(
database.get(), /* An open database */ database.get(), /* An open database */
sql, /* SQL to be evaluated */ sql, /* SQL to be evaluated */
nullptr, /* Callback function */ nullptr, /* Callback function */
nullptr, /* 1st argument to callback */ nullptr, /* 1st argument to callback */
nullptr /* Error msg written here */ nullptr /* Error msg written here */
); );
sqlite3_free( sql ); sqlite3_free( sql );
} }
Expand All @@ -409,34 +409,34 @@ bool QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( const QString &ur
char *sql = sqlite3_mprintf( "DELETE FROM gpkg_2d_gridded_coverage_ancillary WHERE tile_matrix_set_name = '%q'", char *sql = sqlite3_mprintf( "DELETE FROM gpkg_2d_gridded_coverage_ancillary WHERE tile_matrix_set_name = '%q'",
layerName.toUtf8().constData() ); layerName.toUtf8().constData() );
( void )sqlite3_exec( ( void )sqlite3_exec(
database.get(), /* An open database */ database.get(), /* An open database */
sql, /* SQL to be evaluated */ sql, /* SQL to be evaluated */
nullptr, /* Callback function */ nullptr, /* Callback function */
nullptr, /* 1st argument to callback */ nullptr, /* 1st argument to callback */
nullptr /* Error msg written here */ nullptr /* Error msg written here */
); );
sqlite3_free( sql ); sqlite3_free( sql );
} }
{ {
char *sql = sqlite3_mprintf( "DELETE FROM gpkg_2d_gridded_tile_ancillary WHERE tpudt_name = '%q'", char *sql = sqlite3_mprintf( "DELETE FROM gpkg_2d_gridded_tile_ancillary WHERE tpudt_name = '%q'",
layerName.toUtf8().constData() ); layerName.toUtf8().constData() );
( void )sqlite3_exec( ( void )sqlite3_exec(
database.get(), /* An open database */ database.get(), /* An open database */
sql, /* SQL to be evaluated */ sql, /* SQL to be evaluated */
nullptr, /* Callback function */ nullptr, /* Callback function */
nullptr, /* 1st argument to callback */ nullptr, /* 1st argument to callback */
nullptr /* Error msg written here */ nullptr /* Error msg written here */
); );
sqlite3_free( sql ); sqlite3_free( sql );
} }
// Vacuum // Vacuum
{ {
( void )sqlite3_exec( ( void )sqlite3_exec(
database.get(), /* An open database */ database.get(), /* An open database */
"VACUUM", /* SQL to be evaluated */ "VACUUM", /* SQL to be evaluated */
nullptr, /* Callback function */ nullptr, /* Callback function */
nullptr, /* 1st argument to callback */ nullptr, /* 1st argument to callback */
nullptr /* Error msg written here */ nullptr /* Error msg written here */
); );
} }


Expand Down
80 changes: 44 additions & 36 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -5741,57 +5741,65 @@ QGISEXTERN bool deleteLayer( const QString &uri, QString &errCause )
subsetString, subsetString,
ogrGeometryType ); ogrGeometryType );



GDALDatasetH hDS = GDALOpenEx( filePath.toLocal8Bit().data(), GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr ); GDALDatasetH hDS = GDALOpenEx( filePath.toLocal8Bit().data(), GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr );
if ( hDS && ( ! layerName.isEmpty() || layerIndex != -1 ) ) if ( hDS && ( ! layerName.isEmpty() || layerIndex != -1 ) )
{ {
if ( layerIndex == -1 ) // If we have got a name we convert it into an index
if ( ! layerName.isEmpty() )
{ {
layerIndex = -1;
for ( int i = 0; i < GDALDatasetGetLayerCount( hDS ); i++ ) for ( int i = 0; i < GDALDatasetGetLayerCount( hDS ); i++ )
{ {
OGRLayerH hL = GDALDatasetGetLayer( hDS, i ); OGRLayerH hL = GDALDatasetGetLayer( hDS, i );
if ( layerName == QString( OGR_L_GetName( hL ) ) ) if ( layerName == QString( OGR_L_GetName( hL ) ) )
{ {
layerIndex = i; layerIndex = i;
break;
} }
} }
} }
OGRErr error = GDALDatasetDeleteLayer( hDS, layerIndex ); // Do delete!
switch ( error ) if ( layerIndex != -1 )
{ {
case OGRERR_NOT_ENOUGH_DATA: OGRErr error = GDALDatasetDeleteLayer( hDS, layerIndex );
errCause = QObject::tr( "Not enough data to deserialize" ); switch ( error )
break; {
case OGRERR_NOT_ENOUGH_MEMORY: case OGRERR_NOT_ENOUGH_DATA:
errCause = QObject::tr( "Not enough memory" ); errCause = QObject::tr( "Not enough data to deserialize" );
break; break;
case OGRERR_UNSUPPORTED_GEOMETRY_TYPE: case OGRERR_NOT_ENOUGH_MEMORY:
errCause = QObject::tr( "Unsupported geometry type" ); errCause = QObject::tr( "Not enough memory" );
break; break;
case OGRERR_UNSUPPORTED_OPERATION: case OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
errCause = QObject::tr( "Unsupported operation" ); errCause = QObject::tr( "Unsupported geometry type" );
break; break;
case OGRERR_CORRUPT_DATA: case OGRERR_UNSUPPORTED_OPERATION:
errCause = QObject::tr( "Corrupt data" ); errCause = QObject::tr( "Unsupported operation" );
break; break;
case OGRERR_FAILURE: case OGRERR_CORRUPT_DATA:
errCause = QObject::tr( "Failure" ); errCause = QObject::tr( "Corrupt data" );
break; break;
case OGRERR_UNSUPPORTED_SRS: case OGRERR_FAILURE:
errCause = QObject::tr( "Unsupported SRS" ); errCause = QObject::tr( "Failure" );
break; break;
case OGRERR_INVALID_HANDLE: case OGRERR_UNSUPPORTED_SRS:
errCause = QObject::tr( "Invalid handle" ); errCause = QObject::tr( "Unsupported SRS" );
break; break;
case OGRERR_NON_EXISTING_FEATURE: case OGRERR_INVALID_HANDLE:
errCause = QObject::tr( "Non existing feature" ); errCause = QObject::tr( "Invalid handle" );
break; break;
default: case OGRERR_NON_EXISTING_FEATURE:
case OGRERR_NONE: errCause = QObject::tr( "Non existing feature" );
errCause = QObject::tr( "Success" ); break;
break; default:
case OGRERR_NONE:
errCause = QObject::tr( "Success" );
break;
}
errCause = QObject::tr( "GDAL result code: %1" ).arg( errCause );
return error == OGRERR_NONE;
} }
errCause = QObject::tr( "GDAL result code: %1" ).arg( errCause );
return error == OGRERR_NONE;
} }
// This should never happen: // This should never happen:
errCause = QObject::tr( "Layer not found: %1" ).arg( uri ); errCause = QObject::tr( "Layer not found: %1" ).arg( uri );
Expand Down

0 comments on commit f69c1cf

Please sign in to comment.