Skip to content

Commit 29635e9

Browse files
horst-p-w-neubauerMKleusberg
authored andcommitted
detach additional database connection
See issue #2239
1 parent 156fc15 commit 29635e9

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

src/DbStructureModel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ void DbStructureModel::reloadData()
168168
itemAll->setIcon(ColumnName, IconCache::get("database"));
169169
itemAll->setText(ColumnName, tr("All"));
170170
itemAll->setText(ColumnObjectType, "database");
171+
itemAll->setText(ColumnSchema, "main");
171172
buildTree(itemAll, "main");
172173

173174
// 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()
177178
itemTemp->setIcon(ColumnName, IconCache::get("database"));
178179
itemTemp->setText(ColumnName, tr("Temporary"));
179180
itemTemp->setText(ColumnObjectType, "database");
181+
itemTemp->setText(ColumnSchema, "temp");
180182
buildTree(itemTemp, "temp");
181183
}
182184

@@ -190,6 +192,7 @@ void DbStructureModel::reloadData()
190192
itemSchema->setIcon(ColumnName, IconCache::get("database"));
191193
itemSchema->setText(ColumnName, QString::fromStdString(it.first));
192194
itemSchema->setText(ColumnObjectType, "database");
195+
itemSchema->setText(ColumnSchema, QString::fromStdString(it.first));
193196
buildTree(itemSchema, it.first);
194197
}
195198
}

src/MainWindow.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ void MainWindow::init()
240240
popupTableMenu->addAction(ui->actionEditBrowseTable);
241241
popupTableMenu->addAction(ui->editModifyObjectAction);
242242
popupTableMenu->addAction(ui->editDeleteObjectAction);
243+
popupTableMenu->addAction(ui->fileDetachAction);
243244
popupTableMenu->addSeparator();
244245
popupTableMenu->addAction(ui->actionEditCopyCreateStatement);
245246
popupTableMenu->addAction(ui->actionExportCsvPopup);
246247

247248
popupSchemaDockMenu = new QMenu(this);
248249
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockBrowseTable);
250+
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockDetachDatabase);
249251
popupSchemaDockMenu->addSeparator();
250252
popupSchemaDockMenu->addAction(ui->actionDropQualifiedCheck);
251253
popupSchemaDockMenu->addAction(ui->actionEnquoteNamesCheck);
@@ -1503,7 +1505,7 @@ void MainWindow::createTreeContextMenu(const QPoint &qPoint)
15031505
return;
15041506

15051507
QString type = dbSelected->objectType();
1506-
if(type == "table" || type == "view" || type == "trigger" || type == "index")
1508+
if(type == "table" || type == "view" || type == "trigger" || type == "index" || type == "database")
15071509
{
15081510
// needed for first click on treeView as for first time change QItemSelectionModel::currentChanged doesn't fire
15091511
changeTreeSelection();
@@ -1515,14 +1517,18 @@ void MainWindow::createTreeContextMenu(const QPoint &qPoint)
15151517
void MainWindow::createSchemaDockContextMenu(const QPoint &qPoint)
15161518
{
15171519
bool enable_browse_table = false;
1520+
bool enable_detach_file = false;
15181521

15191522
if(dockDbSelected->hasSelection())
15201523
{
15211524
QString type = dockDbSelected->objectType();
15221525
if(type == "table" || type == "view")
15231526
enable_browse_table = true;
1527+
else if(type == "database" && dockDbSelected->schema() != "main" && dockDbSelected->schema() != "temp")
1528+
enable_detach_file = true;
15241529
}
15251530
ui->actionPopupSchemaDockBrowseTable->setEnabled(enable_browse_table);
1531+
ui->actionPopupSchemaDockDetachDatabase->setEnabled(enable_detach_file);
15261532

15271533
popupSchemaDockMenu->exec(ui->treeSchemaDock->mapToGlobal(qPoint));
15281534
}
@@ -1533,12 +1539,18 @@ void MainWindow::changeTreeSelection()
15331539
ui->editDeleteObjectAction->setEnabled(false);
15341540
ui->editModifyObjectAction->setEnabled(false);
15351541
ui->actionEditBrowseTable->setEnabled(false);
1542+
ui->actionExportCsvPopup->setEnabled(false);
1543+
ui->fileDetachAction->setEnabled(false);
1544+
ui->actionEditCopyCreateStatement->setEnabled(false);
1545+
1546+
ui->fileDetachAction->setVisible(false);
15361547

15371548
if(!dbSelected->hasSelection())
15381549
return;
15391550

15401551
// Change the text and tooltips of the actions
15411552
QString type = dbSelected->objectType();
1553+
QString schema = dbSelected->schema();
15421554

15431555
if (type.isEmpty())
15441556
{
@@ -1561,15 +1573,24 @@ void MainWindow::changeTreeSelection()
15611573
} else if(type == "table") {
15621574
ui->editDeleteObjectAction->setText(tr("Delete Table"));
15631575
ui->editModifyObjectAction->setText(tr("Modify Table"));
1576+
} else if(type == "database") {
1577+
ui->editDeleteObjectAction->setVisible(false);
1578+
ui->editModifyObjectAction->setVisible(false);
1579+
ui->fileDetachAction->setVisible(true);
1580+
ui->fileDetachAction->setEnabled(!(schema == "main" || schema == "temp"));
1581+
return;
15641582
} else {
15651583
// Nothing to do for other types. Set the buttons not visible and return.
15661584
ui->editDeleteObjectAction->setVisible(false);
15671585
ui->editModifyObjectAction->setVisible(false);
1586+
ui->fileDetachAction->setVisible(false);
1587+
ui->actionEditCopyCreateStatement->setEnabled(true);
15681588
return;
15691589
}
15701590

15711591
ui->editDeleteObjectAction->setVisible(true);
15721592
ui->editModifyObjectAction->setVisible(true);
1593+
ui->actionEditCopyCreateStatement->setEnabled(true);
15731594

15741595
// Activate actions
15751596
ui->editDeleteObjectAction->setEnabled(!db.readOnly());
@@ -3165,6 +3186,36 @@ void MainWindow::switchToBrowseDataTab(sqlb::ObjectIdentifier tableToBrowse)
31653186
d->raise();
31663187
}
31673188

3189+
void MainWindow::fileDetachDbTree()
3190+
{
3191+
fileDetachTreeViewSelected(ui->dbTreeWidget);
3192+
}
3193+
3194+
void MainWindow::fileDetachTreeSchemaDock()
3195+
{
3196+
fileDetachTreeViewSelected(ui->treeSchemaDock);
3197+
}
3198+
3199+
void MainWindow::fileDetachTreeViewSelected(QTreeView* treeView)
3200+
{
3201+
if (!treeView || !treeView->selectionModel()->hasSelection())
3202+
{
3203+
return;
3204+
}
3205+
3206+
sqlb::ObjectIdentifier attachedDatabase = sqlb::ObjectIdentifier();
3207+
// get the currently selected attached database from treeView parameter
3208+
// Cancel here if there is no selection
3209+
attachedDatabase.setSchema(treeView->model()->data(treeView->currentIndex().sibling(treeView->currentIndex().row(), DbStructureModel::ColumnSchema), Qt::EditRole).toString().toStdString());
3210+
attachedDatabase.setName(treeView->model()->data(treeView->currentIndex().sibling(treeView->currentIndex().row(), DbStructureModel::ColumnName), Qt::EditRole).toString().toStdString());
3211+
3212+
QString attached_as = QString::fromStdString(attachedDatabase.name());
3213+
if (db.detach(attached_as))
3214+
{
3215+
isProjectModified = true;
3216+
}
3217+
}
3218+
31683219
void MainWindow::copyCurrentCreateStatement()
31693220
{
31703221
// Cancel if no field is currently selected

src/MainWindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public slots:
154154
void refresh();
155155
void switchToBrowseDataTab(sqlb::ObjectIdentifier tableToBrowse = sqlb::ObjectIdentifier());
156156
void populateStructure(const std::vector<sqlb::ObjectIdentifier>& old_tables = {});
157+
void fileDetachDbTree();
158+
void fileDetachTreeSchemaDock();
159+
void fileDetachTreeViewSelected(QTreeView* treeView);
157160
void reloadSettings();
158161
bool closeFiles();
159162

src/MainWindow.ui

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,30 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
21952195
<string>Ctrl+Shift+F4</string>
21962196
</property>
21972197
</action>
2198+
<action name="fileDetachAction">
2199+
<property name="icon">
2200+
<iconset resource="icons/icons.qrc">
2201+
<normaloff>:/icons/db_detach</normaloff>:/icons/db_detach</iconset>
2202+
</property>
2203+
<property name="text">
2204+
<string>Detach Database</string>
2205+
</property>
2206+
<property name="toolTip">
2207+
<string>Detach database file attached to the current database connection</string>
2208+
</property>
2209+
</action>
2210+
<action name="actionPopupSchemaDockDetachDatabase">
2211+
<property name="icon">
2212+
<iconset resource="icons/icons.qrc">
2213+
<normaloff>:/icons/db_detach</normaloff>:/icons/db_detach</iconset>
2214+
</property>
2215+
<property name="text">
2216+
<string>Detach Database</string>
2217+
</property>
2218+
<property name="toolTip">
2219+
<string>Detach database file attached to the current database connection</string>
2220+
</property>
2221+
</action>
21982222
</widget>
21992223
<customwidgets>
22002224
<customwidget>
@@ -2913,6 +2937,22 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
29132937
</hint>
29142938
</hints>
29152939
</connection>
2940+
<connection>
2941+
<sender>fileDetachAction</sender>
2942+
<signal>triggered()</signal>
2943+
<receiver>MainWindow</receiver>
2944+
<slot>fileDetachDbTree()</slot>
2945+
<hints>
2946+
<hint type="sourcelabel">
2947+
<x>-1</x>
2948+
<y>-1</y>
2949+
</hint>
2950+
<hint type="destinationlabel">
2951+
<x>499</x>
2952+
<y>314</y>
2953+
</hint>
2954+
</hints>
2955+
</connection>
29162956
<connection>
29172957
<sender>actionEncryption</sender>
29182958
<signal>triggered()</signal>
@@ -3658,6 +3698,8 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
36583698
<slot>loadProject()</slot>
36593699
<slot>saveProject()</slot>
36603700
<slot>fileAttach()</slot>
3701+
<slot>fileDetachDbTree()</slot>
3702+
<slot>fileDetachTreeSchemaDock()</slot>
36613703
<slot>editEncryption()</slot>
36623704
<slot>saveSqlFileAs()</slot>
36633705
<slot>switchToBrowseDataTab()</slot>

src/icons/database_link_broken.png

803 Bytes
Loading

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@
104104
<file>monitor_link.png</file>
105105
<file alias="clone_database">server_add.png</file>
106106
<file alias="cut">cut.png</file>
107+
<file alias="db_detach">database_link_broken.png</file>
107108
</qresource>
108109
</RCC>

src/sqlitedb.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,33 @@ bool DBBrowserDB::open(const QString& db, bool readOnly)
271271
}
272272
}
273273

274+
/**
275+
detaches a previously attached database identified with its alias-name
276+
**/
277+
bool DBBrowserDB::detach(const QString& attached_as)
278+
{
279+
if(!_db)
280+
{
281+
return false;
282+
}
283+
284+
waitForDbRelease();
285+
286+
// dettach database
287+
if(!executeSQL("DETACH " + sqlb::escapeIdentifier(attached_as.toStdString()), false))
288+
{
289+
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
290+
return false;
291+
}
292+
293+
294+
// Update schema to load database schema of the newly attached database
295+
updateSchema();
296+
297+
return true;
298+
}
299+
300+
274301
bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
275302
{
276303
if(!_db)

src/sqlitedb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class DBBrowserDB : public QObject
7171

7272
bool open(const QString& db, bool readOnly = false);
7373
bool attach(const QString& filename, QString attach_as = QString());
74+
75+
/**
76+
detaches a previously attached database identified with its alias-name
77+
78+
\param attached_as the alias-name as witch a additional database file has been attached to the connection
79+
**/
80+
bool detach(const QString& attached_as);
7481
bool create ( const QString & db);
7582
bool close();
7683

0 commit comments

Comments
 (0)