Add menu item to allow attaching other databases

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 #100.
This commit is contained in:
Martin Kleusberg
2014-09-16 19:16:07 +02:00
parent 48ea79b163
commit 6c52ac7368
3 changed files with 51 additions and 0 deletions

View File

@@ -822,6 +822,7 @@ void MainWindow::dbState( bool dirty )
{
ui->fileSaveAction->setEnabled(dirty);
ui->fileRevertAction->setEnabled(dirty);
ui->fileAttachAction->setEnabled(!dirty);
}
void MainWindow::fileSave()
@@ -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);
}