mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-12 00:02:25 -04:00
delete/truncate selected tables for mssql-tables
This commit is contained in:
parent
9dd96dab34
commit
f06bb799ef
@ -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()
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user