From f06b63bc66ad4ed3e8425e71f277c8ad9942bf3c Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sun, 14 May 2017 20:21:05 +0200 Subject: [PATCH] Use savepoint in Edit Index dialog Create a save point when opening the Edit Index dialog. This allows us to revert back to the last version when pressing the Cancel button. --- src/EditIndexDialog.cpp | 16 ++++++++++++++-- src/EditIndexDialog.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/EditIndexDialog.cpp b/src/EditIndexDialog.cpp index 085b3301..6591cf12 100644 --- a/src/EditIndexDialog.cpp +++ b/src/EditIndexDialog.cpp @@ -11,7 +11,8 @@ EditIndexDialog::EditIndexDialog(DBBrowserDB& db, const QString& indexName, bool curIndex(indexName), index(indexName), newIndex(createIndex), - ui(new Ui::EditIndexDialog) + ui(new Ui::EditIndexDialog), + m_sRestorePointName(pdb.generateSavepointName("editindex")) { // Create UI ui->setupUi(this); @@ -52,6 +53,9 @@ EditIndexDialog::EditIndexDialog(DBBrowserDB& db, const QString& indexName, bool } else { tableChanged(ui->comboTableName->currentText(), false); } + + // Create a savepoint to revert back to + pdb.setSavepoint(m_sRestorePointName); } EditIndexDialog::~EditIndexDialog() @@ -211,7 +215,7 @@ void EditIndexDialog::accept() // When editing an index, delete the old one first if(!newIndex) { - if(!pdb.executeSQL(QString("DROP INDEX %1;").arg(sqlb::escapeIdentifier(curIndex)))) + if(!pdb.executeSQL(QString("DROP INDEX IF EXISTS %1;").arg(sqlb::escapeIdentifier(curIndex)))) { QMessageBox::warning(this, qApp->applicationName(), tr("Deleting the old index failed:\n%1").arg(pdb.lastError())); return; @@ -225,6 +229,14 @@ void EditIndexDialog::accept() QMessageBox::warning(this, QApplication::applicationName(), tr("Creating the index failed:\n%1").arg(pdb.lastError())); } +void EditIndexDialog::reject() +{ + // Rollback to our savepoint + pdb.revertToSavepoint(m_sRestorePointName); + + QDialog::reject(); +} + void EditIndexDialog::updateSqlText() { ui->sqlTextEdit->setText(index.sql()); diff --git a/src/EditIndexDialog.h b/src/EditIndexDialog.h index 574a6d66..a055da0b 100644 --- a/src/EditIndexDialog.h +++ b/src/EditIndexDialog.h @@ -22,6 +22,8 @@ public: private slots: void accept(); + void reject(); + void tableChanged(const QString& new_table, bool initialLoad = false); void checkInput(); void addToIndex(const QModelIndex& idx = QModelIndex()); @@ -33,6 +35,7 @@ private: sqlb::Index index; bool newIndex; Ui::EditIndexDialog* ui; + QString m_sRestorePointName; void updateColumnLists(); void updateSqlText();