Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
GPKG Browser VACUUM menu item
Fixes #19895 - Garbage-collection is not performed after deletion of vector layer from geopackage

By design, VACUUM (being a potentially time-consuming operation)
was automatically performed only after deleting a raster
while when deleting a vector layer it was not executed.

This commit adds a menu item in the browser that allows
the user to perform this operation on the DB.
  • Loading branch information
elpaso committed Sep 21, 2018
1 parent 1b2d885 commit b51cb21
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Expand Up @@ -469,6 +469,44 @@ bool QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( const QString &ur
return result;
}

void QgsGeoPackageCollectionItem::vacuumGeoPackageDb()
{
QString errCause;
// Better safe than sorry
if ( ! mPath.isEmpty( ) )
{
char *errmsg = nullptr;
sqlite3_database_unique_ptr database;
int status = database.open_v2( mPath, SQLITE_OPEN_READWRITE, nullptr );
if ( status != SQLITE_OK )
{
errCause = sqlite3_errmsg( database.get() );
}
else
{
( void )sqlite3_exec(
database.get(), /* An open database */
"VACUUM", /* SQL to be evaluated */
nullptr, /* Callback function */
nullptr, /* 1st argument to callback */
&errmsg /* Error msg written here */
);
}
if ( status == SQLITE_OK && ! errmsg )
{
QMessageBox::information( nullptr, tr( "Database compact (VACUUM)" ), tr( "Database <b>%1</b> has been compacted successfully." ).arg( mName ) );
}
else
{
errCause = tr( "There was an error compacting (VACUUM) the database <b>%1</b>: %2" )
.arg( mName )
.arg( QString::fromUtf8( errmsg ) );
QMessageBox::warning( nullptr, tr( "Database compact (VACUUM)" ), errCause );
}
sqlite3_free( errmsg );
}
}

QgsGeoPackageConnectionItem::QgsGeoPackageConnectionItem( QgsDataItem *parent, const QString &name, const QString &path )
: QgsGeoPackageCollectionItem( parent, name, path )
{
Expand Down Expand Up @@ -500,6 +538,11 @@ QList<QAction *> QgsGeoPackageConnectionItem::actions( QWidget *parent )
connect( actionAddTable, &QAction::triggered, this, &QgsGeoPackageConnectionItem::addTable );
lst.append( actionAddTable );

// Run VACUUM
QAction *actionVacuumDb = new QAction( tr( "Compact database (VACUUM)" ), parent );
connect( actionVacuumDb, &QAction::triggered, this, &QgsGeoPackageConnectionItem::vacuumGeoPackageDb );
lst.append( actionVacuumDb );


return lst;
}
Expand Down
6 changes: 5 additions & 1 deletion src/providers/ogr/qgsgeopackagedataitems.h
Expand Up @@ -88,14 +88,18 @@ class QgsGeoPackageCollectionItem : public QgsDataCollectionItem
//! Returns the layer type from \a geometryType
static QgsLayerItem::LayerType layerTypeFromDb( const QString &geometryType );

//! Delete a geopackage layer
//! Deletes a geopackage layer
static bool deleteGeoPackageRasterLayer( const QString &uri, QString &errCause );



public slots:
#ifdef HAVE_GUI
void addTable();
void addConnection();
void deleteConnection();
//! Compacts (VACUUM) a geopackage database
void vacuumGeoPackageDb();
#endif

protected:
Expand Down

0 comments on commit b51cb21

Please sign in to comment.