Save and load attached databases to/from the project file

We were already saving information on tables, etc. of attached
databases, so it only makes sense to save the path and name of the
databases as well.

See issue #1532.
This commit is contained in:
Martin Kleusberg
2018-09-27 22:04:28 +02:00
parent 07b1d7ba00
commit 320c71eed8

View File

@@ -2507,6 +2507,15 @@ bool MainWindow::loadProject(QString filename, bool readOnly)
if(xml.attributes().hasAttribute("synchronous"))
db.setPragma("synchronous", xml.attributes().value("synchronous").toString());
loadPragmas();
} else if(xml.name() == "attached") {
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "attached")
{
if(xml.name() == "db")
{
db.attach(xml.attributes().value("path").toString(), xml.attributes().value("schema").toString());
xml.skipCurrentElement();
}
}
} else if(xml.name() == "window") {
// Window settings
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "window")
@@ -2731,6 +2740,29 @@ void MainWindow::saveProject()
xml.writeAttribute("synchronous", db.getPragma("synchronous"));
xml.writeEndElement();
// Attached databases
xml.writeStartElement("attached");
QString sql("PRAGMA database_list;");
db.logSQL(sql, kLogMsg_App);
sqlite3_stmt* db_vm;
if(sqlite3_prepare_v2(db.get("project").get(), sql.toUtf8(), sql.toUtf8().length(), &db_vm, nullptr) == SQLITE_OK)
{
while(sqlite3_step(db_vm) == SQLITE_ROW)
{
QString schema(QString::fromUtf8((const char*)sqlite3_column_text(db_vm, 1)));
if(schema != "main" && schema != "temp")
{
QString path(QString::fromUtf8((const char*)sqlite3_column_text(db_vm, 2)));
xml.writeStartElement("db");
xml.writeAttribute("schema", schema);
xml.writeAttribute("path", path);
xml.writeEndElement();
}
}
sqlite3_finalize(db_vm);
}
xml.writeEndElement();
// Window settings
xml.writeStartElement("window");
xml.writeStartElement("current_tab"); // Currently selected tab