Add support for in-memory databases

Add a new menu option for opening an in-memory database. When working on
an in-memory database some menu options are disabled because they only
make sense when the database is also written to disk. I have left the
Write Changes button enabled though because for some actions the current
transaction needs to be committed - even though it's only committed to
memory then.

See issues #335 and #1492.
This commit is contained in:
Martin Kleusberg
2018-08-05 15:45:50 +02:00
parent 52de8c3099
commit 9a8f223b88
4 changed files with 56 additions and 7 deletions
+16 -2
View File
@@ -433,6 +433,19 @@ void MainWindow::fileNew()
}
}
void MainWindow::fileNewInMemoryDatabase()
{
db.create(":memory:");
setCurrentFile(tr("In-Memory database"));
statusEncodingLabel->setText(db.getPragma("encoding"));
statusEncryptionLabel->setVisible(false);
statusReadOnlyLabel->setVisible(false);
loadExtensionsFromSettings();
populateTable();
openSqlTab(true);
createTable();
}
void MainWindow::populateStructure(const QString& old_table)
{
// Refresh the structure tab
@@ -1714,6 +1727,7 @@ void MainWindow::dropEvent(QDropEvent *event)
void MainWindow::activateFields(bool enable)
{
bool write = !db.readOnly();
bool tempDb = db.currentFile() == ":memory:";
ui->fileCloseAction->setEnabled(enable);
ui->fileAttachAction->setEnabled(enable);
@@ -1736,8 +1750,8 @@ void MainWindow::activateFields(bool enable)
ui->actionExecuteSql->setEnabled(enable);
ui->actionLoadExtension->setEnabled(enable);
ui->actionSqlExecuteLine->setEnabled(enable);
ui->actionSaveProject->setEnabled(enable);
ui->actionEncryption->setEnabled(enable && write);
ui->actionSaveProject->setEnabled(enable && !tempDb);
ui->actionEncryption->setEnabled(enable && write && !tempDb);
ui->buttonClearFilters->setEnabled(enable);
ui->buttonSaveFilterAsPopup->setEnabled(enable);
ui->dockEdit->setEnabled(enable);
+1
View File
@@ -212,6 +212,7 @@ private slots:
void createTreeContextMenu(const QPoint & qPoint);
void changeTreeSelection();
void fileNew();
void fileNewInMemoryDatabase();
void populateTable();
void clearTableBrowser();
bool fileClose();
+23
View File
@@ -910,6 +910,7 @@ You can drag SQL statements from an object row and drop them into other applicat
<addaction name="fileExportJsonAction"/>
</widget>
<addaction name="fileNewAction"/>
<addaction name="fileNewInMemoryDatabaseAction"/>
<addaction name="fileOpenAction"/>
<addaction name="fileOpenReadOnlyAction"/>
<addaction name="fileAttachAction"/>
@@ -2093,6 +2094,11 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="fileNewInMemoryDatabaseAction">
<property name="text">
<string>New In-&amp;Memory Database</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -3401,6 +3407,22 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
</hint>
</hints>
</connection>
<connection>
<sender>fileNewInMemoryDatabaseAction</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>fileNewInMemoryDatabase()</slot>
<hints>
<hint type="sourcelabel">
<x>518</x>
<y>314</y>
</hint>
<hint type="destinationlabel">
<x>518</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
@@ -3470,5 +3492,6 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<slot>changeSqlTab(int)</slot>
<slot>hideColumns()</slot>
<slot>renameSqlTab(int)</slot>
<slot>fileNewInMemoryDatabase()</slot>
</slots>
</ui>
+16 -5
View File
@@ -529,11 +529,22 @@ bool DBBrowserDB::close()
{
if (getDirty())
{
QMessageBox::StandardButton reply = QMessageBox::question(nullptr,
QApplication::applicationName(),
tr("Do you want to save the changes "
"made to the database file %1?").arg(curDBFilename),
QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
// In-memory databases can't be saved to disk. So the need another text than regular databases.
// Note that the QMessageBox::Yes option in the :memory: case and the QMessageBox::No option in the regular case are
// doing the same job: proceeding but not saving anything.
QMessageBox::StandardButton reply;
if(curDBFilename == ":memory:")
{
reply = QMessageBox::question(nullptr,
QApplication::applicationName(),
tr("Do you really want to close this temporary database? All data will be lost."),
QMessageBox::Yes | QMessageBox::Cancel);
} else {
reply = QMessageBox::question(nullptr,
QApplication::applicationName(),
tr("Do you want to save the changes made to the database file %1?").arg(curDBFilename),
QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
}
// If the user clicked the cancel button stop here and return false
if(reply == QMessageBox::Cancel)