Skip to content

Commit

Permalink
Add menu item to allow attaching other databases
Browse files Browse the repository at this point in the history
Add a new menu option which allows attaching other databases. Doing so
by running the SQL code by yourself using the Execute SQL tab won't work
because the SQL tab creates a restorepoint and attaching databases while
being in a transaction is not allowed.

Note that this commit misses quite a few features: In case of name
conflicts the UI may break, there is no way to tell which databases have
been attached and the attached databases are not stored in the project
files.

See issue sqlitebrowser#100.
  • Loading branch information
MKleusberg committed Sep 16, 2014
1 parent 48ea79b commit 6c52ac7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ void MainWindow::dbState( bool dirty )
{
ui->fileSaveAction->setEnabled(dirty);
ui->fileRevertAction->setEnabled(dirty);
ui->fileAttachAction->setEnabled(!dirty);
}

void MainWindow::fileSave()
Expand Down Expand Up @@ -1932,3 +1933,26 @@ void MainWindow::saveProject()
file.close();
}
}

void MainWindow::fileAttach()
{
// Get file name of database to attach
QString file = QFileDialog::getOpenFileName(
this,
tr("Choose a database file"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString());
if(!QFile::exists(file))
return;

// Ask for name to be given to the attached database
QString attachAs = QInputDialog::getText(this,
qApp->applicationName(),
tr("Please specify the database name under which you want to access the attached database")
).trimmed();
if(attachAs.isEmpty())
return;

// Attach database
if(!db.executeSQL(QString("ATTACH '%1' AS `%2`").arg(file).arg(attachAs), false))
QMessageBox::warning(this, qApp->applicationName(), db.lastErrorMessage);
}
1 change: 1 addition & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ private slots:
void updateBrowseDataColumnWidth(int section, int /*old_size*/, int new_size);
bool loadProject(QString filename = QString());
void saveProject();
void fileAttach();
};

#endif
26 changes: 26 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
</widget>
<addaction name="fileNewAction"/>
<addaction name="fileOpenAction"/>
<addaction name="fileAttachAction"/>
<addaction name="fileCloseAction"/>
<addaction name="separator"/>
<addaction name="fileSaveAction"/>
Expand Down Expand Up @@ -1514,6 +1515,14 @@
<string>Load a working session from a file</string>
</property>
</action>
<action name="fileAttachAction">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Attach Database</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down Expand Up @@ -2289,6 +2298,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>fileAttachAction</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>fileAttach()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>499</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
Expand Down Expand Up @@ -2337,5 +2362,6 @@
<slot>loadExtension()</slot>
<slot>loadProject()</slot>
<slot>saveProject()</slot>
<slot>fileAttach()</slot>
</slots>
</ui>

0 comments on commit 6c52ac7

Please sign in to comment.