1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-15 15:49:36 +02:00

TransactionScope only requires DBConnection not ProjectFileIO

This commit is contained in:
Paul Licameli 2020-09-02 15:14:11 -04:00
parent eb7e67623e
commit ddc2593282
4 changed files with 24 additions and 24 deletions

View File

@ -423,11 +423,11 @@ void ProjectFileIO::UseConnection(Connection &&conn, const FilePath &filePath)
SetFileName(filePath); SetFileName(filePath);
} }
bool ProjectFileIO::TransactionStart(const wxString &name) bool TransactionScope::TransactionStart(const wxString &name)
{ {
char *errmsg = nullptr; char *errmsg = nullptr;
int rc = sqlite3_exec(DB(), int rc = sqlite3_exec(mConnection.DB(),
wxT("SAVEPOINT ") + name + wxT(";"), wxT("SAVEPOINT ") + name + wxT(";"),
nullptr, nullptr,
nullptr, nullptr,
@ -435,7 +435,7 @@ bool ProjectFileIO::TransactionStart(const wxString &name)
if (errmsg) if (errmsg)
{ {
SetDBError( mConnection.SetDBError(
XO("Failed to create savepoint:\n\n%s").Format(name) XO("Failed to create savepoint:\n\n%s").Format(name)
); );
sqlite3_free(errmsg); sqlite3_free(errmsg);
@ -444,11 +444,11 @@ bool ProjectFileIO::TransactionStart(const wxString &name)
return rc == SQLITE_OK; return rc == SQLITE_OK;
} }
bool ProjectFileIO::TransactionCommit(const wxString &name) bool TransactionScope::TransactionCommit(const wxString &name)
{ {
char *errmsg = nullptr; char *errmsg = nullptr;
int rc = sqlite3_exec(DB(), int rc = sqlite3_exec(mConnection.DB(),
wxT("RELEASE ") + name + wxT(";"), wxT("RELEASE ") + name + wxT(";"),
nullptr, nullptr,
nullptr, nullptr,
@ -456,7 +456,7 @@ bool ProjectFileIO::TransactionCommit(const wxString &name)
if (errmsg) if (errmsg)
{ {
SetDBError( mConnection.SetDBError(
XO("Failed to release savepoint:\n\n%s").Format(name) XO("Failed to release savepoint:\n\n%s").Format(name)
); );
sqlite3_free(errmsg); sqlite3_free(errmsg);
@ -465,11 +465,11 @@ bool ProjectFileIO::TransactionCommit(const wxString &name)
return rc == SQLITE_OK; return rc == SQLITE_OK;
} }
bool ProjectFileIO::TransactionRollback(const wxString &name) bool TransactionScope::TransactionRollback(const wxString &name)
{ {
char *errmsg = nullptr; char *errmsg = nullptr;
int rc = sqlite3_exec(DB(), int rc = sqlite3_exec(mConnection.DB(),
wxT("ROLLBACK TO ") + name + wxT(";"), wxT("ROLLBACK TO ") + name + wxT(";"),
nullptr, nullptr,
nullptr, nullptr,
@ -477,7 +477,7 @@ bool ProjectFileIO::TransactionRollback(const wxString &name)
if (errmsg) if (errmsg)
{ {
SetDBError( mConnection.SetDBError(
XO("Failed to release savepoint:\n\n%s").Format(name) XO("Failed to release savepoint:\n\n%s").Format(name)
); );
sqlite3_free(errmsg); sqlite3_free(errmsg);
@ -2449,12 +2449,12 @@ int ProjectFileIO::get_varint(const unsigned char *ptr, int64_t *out)
return 9; return 9;
} }
TransactionScope::TransactionScope(ProjectFileIO &projectFileIO, TransactionScope::TransactionScope(
const char *name) DBConnection &connection, const char *name)
: mIO(projectFileIO), : mConnection(connection),
mName(name) mName(name)
{ {
mInTrans = mIO.TransactionStart(mName); mInTrans = TransactionStart(mName);
if ( !mInTrans ) if ( !mInTrans )
// To do, improve the message // To do, improve the message
throw SimpleMessageBoxException( XO("Database error") ); throw SimpleMessageBoxException( XO("Database error") );
@ -2467,8 +2467,8 @@ TransactionScope::~TransactionScope()
// Rollback AND REMOVE the transaction // Rollback AND REMOVE the transaction
// -- must do both; rolling back a savepoint only rewinds it // -- must do both; rolling back a savepoint only rewinds it
// without removing it, unlike the ROLLBACK command // without removing it, unlike the ROLLBACK command
if (!(mIO.TransactionRollback(mName) && if (!(TransactionRollback(mName) &&
mIO.TransactionCommit(mName) ) ) TransactionCommit(mName) ) )
{ {
// Do not throw from a destructor! // Do not throw from a destructor!
// This has to be a no-fail cleanup that does the best that it can. // This has to be a no-fail cleanup that does the best that it can.
@ -2482,7 +2482,7 @@ bool TransactionScope::Commit()
// Misuse of this class // Misuse of this class
THROW_INCONSISTENCY_EXCEPTION; THROW_INCONSISTENCY_EXCEPTION;
mInTrans = !mIO.TransactionCommit(mName); mInTrans = !TransactionCommit(mName);
return mInTrans; return mInTrans;
} }

View File

@ -130,10 +130,6 @@ public:
// The last compact check found unused blocks in the project file // The last compact check found unused blocks in the project file
bool HadUnused(); bool HadUnused();
bool TransactionStart(const wxString &name);
bool TransactionCommit(const wxString &name);
bool TransactionRollback(const wxString &name);
// In one SQL command, delete sample blocks with ids in the given set, or // In one SQL command, delete sample blocks with ids in the given set, or
// (when complement is true), with ids not in the given set. // (when complement is true), with ids not in the given set.
bool DeleteBlocks(const BlockIDs &blockids, bool complement); bool DeleteBlocks(const BlockIDs &blockids, bool complement);
@ -250,13 +246,17 @@ private:
class TransactionScope class TransactionScope
{ {
public: public:
TransactionScope(ProjectFileIO &projectFileIO, const char *name); TransactionScope(DBConnection &connection, const char *name);
~TransactionScope(); ~TransactionScope();
bool Commit(); bool Commit();
private: private:
ProjectFileIO &mIO; bool TransactionStart(const wxString &name);
bool TransactionCommit(const wxString &name);
bool TransactionRollback(const wxString &name);
DBConnection &mConnection;
bool mInTrans; bool mInTrans;
wxString mName; wxString mName;
}; };

View File

@ -740,7 +740,7 @@ void ProjectManager::OnCloseWindow(wxCloseEvent & event)
projectFileIO.SetBypass(); projectFileIO.SetBypass();
{ {
TransactionScope trans(projectFileIO, "Shutdown"); TransactionScope trans(projectFileIO.GetConnection(), "Shutdown");
// This can reduce reference counts of sample blocks in the project's // This can reduce reference counts of sample blocks in the project's
// tracks. // tracks.

View File

@ -1215,7 +1215,7 @@ bool Effect::DoEffect(double projectRate,
// This is for performance purposes only, no additional recovery implied // This is for performance purposes only, no additional recovery implied
auto &pProject = *const_cast<AudacityProject*>(FindProject()); // how to remove this const_cast? auto &pProject = *const_cast<AudacityProject*>(FindProject()); // how to remove this const_cast?
auto &pIO = ProjectFileIO::Get(pProject); auto &pIO = ProjectFileIO::Get(pProject);
TransactionScope trans(pIO, "Effect"); TransactionScope trans(pIO.GetConnection(), "Effect");
// Update track/group counts // Update track/group counts
CountWaveTracks(); CountWaveTracks();