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.
This commit is contained in:
Martin Kleusberg
2017-05-14 20:21:05 +02:00
parent e61d7d9fa7
commit f06b63bc66
2 changed files with 17 additions and 2 deletions

View File

@@ -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());

View File

@@ -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();