Skip to content

Commit

Permalink
[FEATURE] Delete SpatiaLite tables from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 17, 2011
1 parent 57ea79f commit 4f9fa31
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/providers/spatialite/qgsspatialitedataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@
#include <QMessageBox>
#include <QSettings>

QGISEXTERN bool deleteLayer( const QString& dbPath, const QString& tableName, QString& errCause );

QgsSLLayerItem::QgsSLLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType )
: QgsLayerItem( parent, name, path, uri, layerType, "spatialite" )
{
mPopulated = true; // no children are expected
}

QList<QAction*> QgsSLLayerItem::actions()
{
QList<QAction*> lst;

QAction* actionDeleteLayer = new QAction( tr( "Delete layer" ), this );
connect( actionDeleteLayer, SIGNAL( triggered() ), this, SLOT( deleteLayer() ) );
lst.append( actionDeleteLayer );

return lst;
}

void QgsSLLayerItem::deleteLayer()
{
QgsDataSourceURI uri( mUri );
QString errCause;
bool res = ::deleteLayer( uri.database(), uri.table(), errCause );
if ( !res )
{
QMessageBox::warning( 0, tr( "Delete layer" ), errCause );
}
else
{
QMessageBox::information( 0, tr( "Delete layer" ), tr( "Layer deleted successfully." ) );
mParent->refresh();
}
}

// ------

QgsSLConnectionItem::QgsSLConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
Expand Down
12 changes: 7 additions & 5 deletions src/providers/spatialite/qgsspatialitedataitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

class QgsSLLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsSLLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType )
: QgsLayerItem( parent, name, path, uri, layerType, "spatialite" )
{
mPopulated = true; // no children are expected
}
QgsSLLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType );

QList<QAction*> actions();

public slots:
void deleteLayer();
};

class QgsSLConnectionItem : public QgsDataCollectionItem
Expand Down
53 changes: 53 additions & 0 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4988,3 +4988,56 @@ QGISEXTERN bool createDb( const QString& dbPath, QString& errCause )

return init_res;
}

// -------------

QGISEXTERN bool deleteLayer( const QString& dbPath, const QString& tableName, QString& errCause )
{
QgsDebugMsg( "deleting layer " + tableName );

spatialite_init( 0 );
QgsSpatiaLiteProvider::SqliteHandles* hndl = QgsSpatiaLiteProvider::SqliteHandles::openDb( dbPath );
if ( !hndl )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
}
sqlite3* sqlite_handle = hndl->handle();

// drop the table

QString sql = QString( "DROP TABLE " ) + QgsSpatiaLiteProvider::quotedIdentifier( tableName );
QgsDebugMsg( sql );
char *errMsg = NULL;
int ret = sqlite3_exec( sqlite_handle, sql.toUtf8().constData(), NULL, NULL, &errMsg );
if ( ret != SQLITE_OK )
{
errCause = QObject::tr( "Unable to delete table %1:\n" ).arg( tableName );
errCause += QString::fromUtf8( errMsg );
sqlite3_free( errMsg );
QgsSpatiaLiteProvider::SqliteHandles::closeDb( hndl );
return false;
}

// remove table from geometry columns
sql = QString( "DELETE FROM geometry_columns WHERE f_table_name = %1" )
.arg( QgsSpatiaLiteProvider::quotedValue( tableName ) );
ret = sqlite3_exec( sqlite_handle, sql.toUtf8().constData(), NULL, NULL, NULL );
if ( ret != SQLITE_OK )
{
QgsDebugMsg( "sqlite error: " + QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) ) );
}

// TODO: remove spatial indexes?

// run VACUUM to free unused space and compact the database
ret = sqlite3_exec( sqlite_handle, "VACUUM", NULL, NULL, &errMsg );
if ( ret != SQLITE_OK )
{
QgsDebugMsg( "Failed to run VACUUM after deleting table on database " + dbPath );
}

QgsSpatiaLiteProvider::SqliteHandles::closeDb( hndl );

return true;
}
5 changes: 3 additions & 2 deletions src/providers/spatialite/qgsspatialiteprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,6 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
*/
//void sqliteOpen();
void closeDb();
static QString quotedIdentifier( QString id );
static QString quotedValue( QString value );
bool checkLayerType();
bool getGeometryDetails();
bool getTableGeometryDetails();
Expand Down Expand Up @@ -445,6 +443,9 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
};

public:
static QString quotedIdentifier( QString id );
static QString quotedValue( QString value );

class SqliteHandles
{
//
Expand Down

0 comments on commit 4f9fa31

Please sign in to comment.