Skip to content

Commit f06bb79

Browse files
committedNov 19, 2018
delete/truncate selected tables for mssql-tables
1 parent 9dd96da commit f06bb79

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed
 

‎src/providers/mssql/qgsmssqldataitems.cpp

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -568,57 +568,81 @@ QgsMssqlLayerItem::QgsMssqlLayerItem( QgsDataItem *parent, const QString &name,
568568
#ifdef HAVE_GUI
569569
QList<QAction *> QgsMssqlLayerItem::actions( QWidget *actionParent )
570570
{
571-
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );
572-
573571
QList<QAction *> lst;
574572

575573
// delete
576-
QAction *actionDeleteLayer = new QAction( tr( "Delete Table" ), actionParent );
574+
const QString deleteText = selectedItems().count() == 1 ? tr( "Delete Table" )
575+
: tr( "Delete Selected Tables" );
576+
QAction *actionDeleteLayer = new QAction( deleteText, actionParent );
577577
connect( actionDeleteLayer, &QAction::triggered, this, [ = ]
578578
{
579-
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
580-
QObject::tr( "Are you sure you want to delete [%1].[%2]?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
581-
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
582-
return;
583-
584-
QString errCause;
585-
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
586-
if ( !res )
579+
QList<QgsDataItem *> items = selectedItems();
580+
for ( QgsDataItem *item : items )
587581
{
588-
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
589-
}
590-
else
591-
{
592-
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
593-
if ( connItem )
594-
connItem->refresh();
582+
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
583+
mssqlLayerItem->deleteLayer();
595584
}
596585
} );
597586
lst.append( actionDeleteLayer );
598587

599588
// truncate
600-
QAction *actionTruncateLayer = new QAction( tr( "Truncate Table" ), actionParent );
589+
const QString truncateText = selectedItems().count() == 1 ? tr( "Truncate Table" )
590+
: tr( "Truncate Selected Tables" );
591+
QAction *actionTruncateLayer = new QAction( truncateText, actionParent );
601592
connect( actionTruncateLayer, &QAction::triggered, this, [ = ]
602593
{
603-
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
604-
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 ),
605-
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
606-
return;
607-
608-
QString errCause;
609-
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
610-
if ( !res )
594+
QList<QgsDataItem *> items = selectedItems();
595+
for ( QgsDataItem *item : items )
611596
{
612-
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
613-
}
614-
else
615-
{
616-
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
597+
if ( QgsMssqlLayerItem *mssqlLayerItem = qobject_cast< QgsMssqlLayerItem *>( item ) )
598+
mssqlLayerItem->truncateTable();
617599
}
618600
} );
619601
lst.append( actionTruncateLayer );
620602
return lst;
621603
}
604+
605+
void QgsMssqlLayerItem::deleteLayer()
606+
{
607+
QgsMssqlConnectionItem *connItem = qobject_cast<QgsMssqlConnectionItem *>( parent() ? parent()->parent() : nullptr );
608+
609+
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
610+
QObject::tr( "Are you sure you want to delete [%1].[%2]?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
611+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
612+
return;
613+
614+
QString errCause;
615+
bool res = QgsMssqlConnection::dropTable( mUri, &errCause );
616+
if ( !res )
617+
{
618+
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
619+
}
620+
else
621+
{
622+
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
623+
if ( connItem )
624+
connItem->refresh();
625+
}
626+
}
627+
628+
void QgsMssqlLayerItem::truncateTable()
629+
{
630+
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
631+
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 ),
632+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
633+
return;
634+
635+
QString errCause;
636+
bool res = QgsMssqlConnection::truncateTable( mUri, &errCause );
637+
if ( !res )
638+
{
639+
QMessageBox::warning( nullptr, tr( "Truncate Table" ), errCause );
640+
}
641+
else
642+
{
643+
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
644+
}
645+
}
622646
#endif
623647

624648
QgsMssqlLayerItem *QgsMssqlLayerItem::createClone()

‎src/providers/mssql/qgsmssqldataitems.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class QgsMssqlLayerItem : public QgsLayerItem
140140

141141
bool disableInvalidGeometryHandling() const;
142142

143+
public slots:
144+
#ifdef HAVE_GUI
145+
void deleteLayer();
146+
void truncateTable();
147+
#endif
148+
143149
private:
144150
QgsMssqlLayerProperty mLayerProperty;
145151
bool mDisableInvalidGeometryHandling = false;

0 commit comments

Comments
 (0)
Please sign in to comment.