detach additional database connection

See issue #2239
This commit is contained in:
horst-p-w-neubauer
2020-05-12 01:59:05 +02:00
committed by Martin Kleusberg
parent 156fc15d69
commit 29635e9e80
8 changed files with 135 additions and 1 deletions

View File

@@ -168,6 +168,7 @@ void DbStructureModel::reloadData()
itemAll->setIcon(ColumnName, IconCache::get("database"));
itemAll->setText(ColumnName, tr("All"));
itemAll->setText(ColumnObjectType, "database");
itemAll->setText(ColumnSchema, "main");
buildTree(itemAll, "main");
// Add the temporary database as a node if it isn't empty. Make sure it's always second if it exists.
@@ -177,6 +178,7 @@ void DbStructureModel::reloadData()
itemTemp->setIcon(ColumnName, IconCache::get("database"));
itemTemp->setText(ColumnName, tr("Temporary"));
itemTemp->setText(ColumnObjectType, "database");
itemTemp->setText(ColumnSchema, "temp");
buildTree(itemTemp, "temp");
}
@@ -190,6 +192,7 @@ void DbStructureModel::reloadData()
itemSchema->setIcon(ColumnName, IconCache::get("database"));
itemSchema->setText(ColumnName, QString::fromStdString(it.first));
itemSchema->setText(ColumnObjectType, "database");
itemSchema->setText(ColumnSchema, QString::fromStdString(it.first));
buildTree(itemSchema, it.first);
}
}

View File

@@ -240,12 +240,14 @@ void MainWindow::init()
popupTableMenu->addAction(ui->actionEditBrowseTable);
popupTableMenu->addAction(ui->editModifyObjectAction);
popupTableMenu->addAction(ui->editDeleteObjectAction);
popupTableMenu->addAction(ui->fileDetachAction);
popupTableMenu->addSeparator();
popupTableMenu->addAction(ui->actionEditCopyCreateStatement);
popupTableMenu->addAction(ui->actionExportCsvPopup);
popupSchemaDockMenu = new QMenu(this);
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockBrowseTable);
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockDetachDatabase);
popupSchemaDockMenu->addSeparator();
popupSchemaDockMenu->addAction(ui->actionDropQualifiedCheck);
popupSchemaDockMenu->addAction(ui->actionEnquoteNamesCheck);
@@ -1503,7 +1505,7 @@ void MainWindow::createTreeContextMenu(const QPoint &qPoint)
return;
QString type = dbSelected->objectType();
if(type == "table" || type == "view" || type == "trigger" || type == "index")
if(type == "table" || type == "view" || type == "trigger" || type == "index" || type == "database")
{
// needed for first click on treeView as for first time change QItemSelectionModel::currentChanged doesn't fire
changeTreeSelection();
@@ -1515,14 +1517,18 @@ void MainWindow::createTreeContextMenu(const QPoint &qPoint)
void MainWindow::createSchemaDockContextMenu(const QPoint &qPoint)
{
bool enable_browse_table = false;
bool enable_detach_file = false;
if(dockDbSelected->hasSelection())
{
QString type = dockDbSelected->objectType();
if(type == "table" || type == "view")
enable_browse_table = true;
else if(type == "database" && dockDbSelected->schema() != "main" && dockDbSelected->schema() != "temp")
enable_detach_file = true;
}
ui->actionPopupSchemaDockBrowseTable->setEnabled(enable_browse_table);
ui->actionPopupSchemaDockDetachDatabase->setEnabled(enable_detach_file);
popupSchemaDockMenu->exec(ui->treeSchemaDock->mapToGlobal(qPoint));
}
@@ -1533,12 +1539,18 @@ void MainWindow::changeTreeSelection()
ui->editDeleteObjectAction->setEnabled(false);
ui->editModifyObjectAction->setEnabled(false);
ui->actionEditBrowseTable->setEnabled(false);
ui->actionExportCsvPopup->setEnabled(false);
ui->fileDetachAction->setEnabled(false);
ui->actionEditCopyCreateStatement->setEnabled(false);
ui->fileDetachAction->setVisible(false);
if(!dbSelected->hasSelection())
return;
// Change the text and tooltips of the actions
QString type = dbSelected->objectType();
QString schema = dbSelected->schema();
if (type.isEmpty())
{
@@ -1561,15 +1573,24 @@ void MainWindow::changeTreeSelection()
} else if(type == "table") {
ui->editDeleteObjectAction->setText(tr("Delete Table"));
ui->editModifyObjectAction->setText(tr("Modify Table"));
} else if(type == "database") {
ui->editDeleteObjectAction->setVisible(false);
ui->editModifyObjectAction->setVisible(false);
ui->fileDetachAction->setVisible(true);
ui->fileDetachAction->setEnabled(!(schema == "main" || schema == "temp"));
return;
} else {
// Nothing to do for other types. Set the buttons not visible and return.
ui->editDeleteObjectAction->setVisible(false);
ui->editModifyObjectAction->setVisible(false);
ui->fileDetachAction->setVisible(false);
ui->actionEditCopyCreateStatement->setEnabled(true);
return;
}
ui->editDeleteObjectAction->setVisible(true);
ui->editModifyObjectAction->setVisible(true);
ui->actionEditCopyCreateStatement->setEnabled(true);
// Activate actions
ui->editDeleteObjectAction->setEnabled(!db.readOnly());
@@ -3165,6 +3186,36 @@ void MainWindow::switchToBrowseDataTab(sqlb::ObjectIdentifier tableToBrowse)
d->raise();
}
void MainWindow::fileDetachDbTree()
{
fileDetachTreeViewSelected(ui->dbTreeWidget);
}
void MainWindow::fileDetachTreeSchemaDock()
{
fileDetachTreeViewSelected(ui->treeSchemaDock);
}
void MainWindow::fileDetachTreeViewSelected(QTreeView* treeView)
{
if (!treeView || !treeView->selectionModel()->hasSelection())
{
return;
}
sqlb::ObjectIdentifier attachedDatabase = sqlb::ObjectIdentifier();
// get the currently selected attached database from treeView parameter
// Cancel here if there is no selection
attachedDatabase.setSchema(treeView->model()->data(treeView->currentIndex().sibling(treeView->currentIndex().row(), DbStructureModel::ColumnSchema), Qt::EditRole).toString().toStdString());
attachedDatabase.setName(treeView->model()->data(treeView->currentIndex().sibling(treeView->currentIndex().row(), DbStructureModel::ColumnName), Qt::EditRole).toString().toStdString());
QString attached_as = QString::fromStdString(attachedDatabase.name());
if (db.detach(attached_as))
{
isProjectModified = true;
}
}
void MainWindow::copyCurrentCreateStatement()
{
// Cancel if no field is currently selected

View File

@@ -154,6 +154,9 @@ public slots:
void refresh();
void switchToBrowseDataTab(sqlb::ObjectIdentifier tableToBrowse = sqlb::ObjectIdentifier());
void populateStructure(const std::vector<sqlb::ObjectIdentifier>& old_tables = {});
void fileDetachDbTree();
void fileDetachTreeSchemaDock();
void fileDetachTreeViewSelected(QTreeView* treeView);
void reloadSettings();
bool closeFiles();

View File

@@ -2195,6 +2195,30 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<string>Ctrl+Shift+F4</string>
</property>
</action>
<action name="fileDetachAction">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/db_detach</normaloff>:/icons/db_detach</iconset>
</property>
<property name="text">
<string>Detach Database</string>
</property>
<property name="toolTip">
<string>Detach database file attached to the current database connection</string>
</property>
</action>
<action name="actionPopupSchemaDockDetachDatabase">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/db_detach</normaloff>:/icons/db_detach</iconset>
</property>
<property name="text">
<string>Detach Database</string>
</property>
<property name="toolTip">
<string>Detach database file attached to the current database connection</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -2913,6 +2937,22 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
</hint>
</hints>
</connection>
<connection>
<sender>fileDetachAction</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>fileDetachDbTree()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>499</x>
<y>314</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionEncryption</sender>
<signal>triggered()</signal>
@@ -3658,6 +3698,8 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<slot>loadProject()</slot>
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
<slot>fileDetachDbTree()</slot>
<slot>fileDetachTreeSchemaDock()</slot>
<slot>editEncryption()</slot>
<slot>saveSqlFileAs()</slot>
<slot>switchToBrowseDataTab()</slot>

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 B

View File

@@ -104,5 +104,6 @@
<file>monitor_link.png</file>
<file alias="clone_database">server_add.png</file>
<file alias="cut">cut.png</file>
<file alias="db_detach">database_link_broken.png</file>
</qresource>
</RCC>

View File

@@ -271,6 +271,33 @@ bool DBBrowserDB::open(const QString& db, bool readOnly)
}
}
/**
detaches a previously attached database identified with its alias-name
**/
bool DBBrowserDB::detach(const QString& attached_as)
{
if(!_db)
{
return false;
}
waitForDbRelease();
// dettach database
if(!executeSQL("DETACH " + sqlb::escapeIdentifier(attached_as.toStdString()), false))
{
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
return false;
}
// Update schema to load database schema of the newly attached database
updateSchema();
return true;
}
bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
{
if(!_db)

View File

@@ -71,6 +71,13 @@ public:
bool open(const QString& db, bool readOnly = false);
bool attach(const QString& filename, QString attach_as = QString());
/**
detaches a previously attached database identified with its alias-name
\param attached_as the alias-name as witch a additional database file has been attached to the connection
**/
bool detach(const QString& attached_as);
bool create ( const QString & db);
bool close();