Skip to content

Commit

Permalink
Unify and (hopefully) simplify generation of savepoint names
Browse files Browse the repository at this point in the history
This shouldn't change anything for the user.
  • Loading branch information
MKleusberg committed Jan 31, 2017
1 parent 665837f commit c683034
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/EditTableDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const QString& tableName, bool
curTable(tableName),
m_table(tableName),
m_bNewTable(createTable),
m_sRestorePointName(sqlb::escapeIdentifier(QString("edittable_%1_save_%2").arg(curTable).arg(QDateTime::currentMSecsSinceEpoch())))
m_sRestorePointName(pdb.generateSavepointName("edittable"))
{
// Create UI
ui->setupUi(this);
Expand Down
2 changes: 1 addition & 1 deletion src/ImportCsvDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void ImportCsvDialog::accept()

// Create a savepoint, so we can rollback in case of any errors during importing
// db needs to be saved or an error will occur
QString restorepointName = QString("CSVIMPORT_%1").arg(QDateTime::currentMSecsSinceEpoch());
QString restorepointName = pdb->generateSavepointName("csvimport");
if(!pdb->setSavepoint(restorepointName))
return rollback(this, pdb, progress, restorepointName, 0, tr("Creating restore point failed: %1").arg(pdb->lastError()));

Expand Down
23 changes: 12 additions & 11 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ bool DBBrowserDB::setSavepoint(const QString& pointname)
if(savepointList.contains(pointname))
return true;

QString query = QString("SAVEPOINT %1;").arg(pointname);
QString query = QString("SAVEPOINT %1;").arg(sqlb::escapeIdentifier(pointname));
executeSQL(query, false, false);
savepointList.append(pointname);
emit dbChanged(getDirty());
Expand All @@ -298,7 +298,7 @@ bool DBBrowserDB::releaseSavepoint(const QString& pointname)
// the operation should be successfull
return true;

QString query = QString("RELEASE %1;").arg(pointname);
QString query = QString("RELEASE %1;").arg(sqlb::escapeIdentifier(pointname));
if(!executeSQL(query, false, false))
return false;
// SQLite releases all savepoints that were created between
Expand All @@ -316,9 +316,9 @@ bool DBBrowserDB::revertToSavepoint(const QString& pointname)
if(!isOpen() || savepointList.contains(pointname) == false)
return false;

QString query = QString("ROLLBACK TO SAVEPOINT %1;").arg(pointname);
QString query = QString("ROLLBACK TO SAVEPOINT %1;").arg(sqlb::escapeIdentifier(pointname));
executeSQL(query, false, false);
query = QString("RELEASE %1;").arg(pointname);
query = QString("RELEASE %1;").arg(sqlb::escapeIdentifier(pointname));
executeSQL(query, false, false);
// SQLite releases all savepoints that were created between
// creation of given savepoint and releasing of it,
Expand Down Expand Up @@ -684,7 +684,7 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log
QString savepoint_name;
if(dirty)
{
savepoint_name = sqlb::escapeIdentifier(generateSavepointName("execmultisql"));
savepoint_name = generateSavepointName("execmultisql");
setSavepoint(savepoint_name);
}

Expand Down Expand Up @@ -1044,7 +1044,8 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& tabl
}

// Create savepoint to be able to go back to it in case of any error
if(!setSavepoint("sqlitebrowser_rename_column"))
QString savepointName = generateSavepointName("renamecolumn");
if(!setSavepoint(savepointName))
{
lastErrorMessage = tr("renameColumn: creating savepoint failed. DB says: %1").arg(lastErrorMessage);
return false;
Expand Down Expand Up @@ -1096,7 +1097,7 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& tabl
if(!executeSQL(newSchema.sql(), true, true))
{
QString error(tr("renameColumn: creating new table failed. DB says: %1").arg(lastErrorMessage));
revertToSavepoint("sqlitebrowser_rename_column");
revertToSavepoint(savepointName);
lastErrorMessage = error;
return false;
}
Expand All @@ -1105,7 +1106,7 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& tabl
if(!executeSQL(QString("INSERT INTO sqlitebrowser_rename_column_new_table SELECT %1 FROM %2;").arg(select_cols).arg(sqlb::escapeIdentifier(tablename))))
{
QString error(tr("renameColumn: copying data to new table failed. DB says:\n%1").arg(lastErrorMessage));
revertToSavepoint("sqlitebrowser_rename_column");
revertToSavepoint(savepointName);
lastErrorMessage = error;
return false;
}
Expand Down Expand Up @@ -1148,15 +1149,15 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& tabl
if(!executeSQL(QString("DROP TABLE %1;").arg(sqlb::escapeIdentifier(tablename)), true, true))
{
QString error(tr("renameColumn: deleting old table failed. DB says: %1").arg(lastErrorMessage));
revertToSavepoint("sqlitebrowser_rename_column");
revertToSavepoint(savepointName);
lastErrorMessage = error;
return false;
}

// Rename the temporary table
if(!renameTable("sqlitebrowser_rename_column_new_table", tablename))
{
revertToSavepoint("sqlitebrowser_rename_column");
revertToSavepoint(savepointName);
return false;
}

Expand All @@ -1179,7 +1180,7 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& tabl
}

// Release the savepoint - everything went fine
if(!releaseSavepoint("sqlitebrowser_rename_column"))
if(!releaseSavepoint(savepointName))
{
lastErrorMessage = tr("renameColumn: releasing savepoint failed. DB says: %1").arg(lastErrorMessage);
return false;
Expand Down

0 comments on commit c683034

Please sign in to comment.