Skip to content

Commit

Permalink
delete/truncate selected tables for mssql-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Nov 19, 2018
1 parent 9dd96da commit f06bb79
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
88 changes: 56 additions & 32 deletions src/providers/mssql/qgsmssqldataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,57 +568,81 @@ QgsMssqlLayerItem::QgsMssqlLayerItem( QgsDataItem *parent, const QString &name,
#ifdef HAVE_GUI
QList<QAction *> QgsMssqlLayerItem::actions( QWidget *actionParent )
{
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );

QList<QAction *> lst;

// delete
QAction *actionDeleteLayer = new QAction( tr( "Delete Table" ), actionParent );
const QString deleteText = selectedItems().count() == 1 ? tr( "Delete Table" )
: tr( "Delete Selected Tables" );
QAction *actionDeleteLayer = new QAction( deleteText, actionParent );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete [%1].[%2]?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
if ( !res )
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( connItem )
connItem->refresh();
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
mssqlLayerItem->deleteLayer();
}
} );
lst.append( actionDeleteLayer );

// truncate
QAction *actionTruncateLayer = new QAction( tr( "Truncate Table" ), actionParent );
const QString truncateText = selectedItems().count() == 1 ? tr( "Truncate Table" )
: tr( "Truncate Selected Tables" );
QAction *actionTruncateLayer = new QAction( truncateText, actionParent );
connect( actionTruncateLayer, &QAction::triggered, this, [ = ]
{
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate [%1].[%2]?\n\nThis will delete all data within the table." ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
if ( !res )
QList<QgsDataItem *> items = selectedItems();
for ( QgsDataItem *item : items )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
mssqlLayerItem->truncateTable();
}
} );
lst.append( actionTruncateLayer );
return lst;
}

void QgsMssqlLayerItem::deleteLayer()
{
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );

if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
QObject::tr( "Are you sure you want to delete [%1].[%2]?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
if ( connItem )
connItem->refresh();
}
}

void QgsMssqlLayerItem::truncateTable()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate [%1].[%2]?\n\nThis will delete all data within the table." ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QString errCause;
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}
}
#endif

QgsMssqlLayerItem *QgsMssqlLayerItem::createClone()
Expand Down
6 changes: 6 additions & 0 deletions src/providers/mssql/qgsmssqldataitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ class QgsMssqlLayerItem : public QgsLayerItem

bool disableInvalidGeometryHandling() const;

public slots:
#ifdef HAVE_GUI
void deleteLayer();
void truncateTable();
#endif

private:
QgsMssqlLayerProperty mLayerProperty;
bool mDisableInvalidGeometryHandling = false;
Expand Down

0 comments on commit f06bb79

Please sign in to comment.