Audacity  3.0.3
Public Member Functions | Private Member Functions | Private Attributes | List of all members
TransactionScope Class Reference

RAII for a database transaction, possibly nested. More...

#include <DBConnection.h>

Collaboration diagram for TransactionScope:
[legend]

Public Member Functions

 TransactionScope (DBConnection &connection, const char *name)
 
 ~TransactionScope ()
 
bool Commit ()
 

Private Member Functions

bool TransactionStart (const wxString &name)
 
bool TransactionCommit (const wxString &name)
 
bool TransactionRollback (const wxString &name)
 

Private Attributes

DBConnectionmConnection
 
bool mInTrans
 
wxString mName
 

Detailed Description

RAII for a database transaction, possibly nested.

Make a savepoint (a transaction, possibly nested) with the given name; roll it back at destruction time, unless an explicit Commit() happened first. Commit() must not be called again after one successful call. An exception is thrown from the constructor if the transaction cannot open.

Definition at line 135 of file DBConnection.h.

Constructor & Destructor Documentation

◆ TransactionScope()

TransactionScope::TransactionScope ( DBConnection connection,
const char *  name 
)

Definition at line 626 of file DBConnection.cpp.

628 : mConnection(connection),
629  mName(name)
630 {
632  if ( !mInTrans )
633  // To do, improve the message
635  XO("Database error. Sorry, but we don't have more details."),
636  XO("Warning"),
637  "Error:_Disk_full_or_not_writable"
638  );
639 }

References Internal, mInTrans, mName, TransactionStart(), and XO.

Here is the call graph for this function:

◆ ~TransactionScope()

TransactionScope::~TransactionScope ( )

Definition at line 641 of file DBConnection.cpp.

642 {
643  if (mInTrans)
644  {
645  // Rollback AND REMOVE the transaction
646  // -- must do both; rolling back a savepoint only rewinds it
647  // without removing it, unlike the ROLLBACK command
648  if (!(TransactionRollback(mName) &&
650  {
651  // Do not throw from a destructor!
652  // This has to be a no-fail cleanup that does the best that it can.
653  wxLogMessage("Transaction active at scope destruction");
654  }
655  }
656 }

References mInTrans, mName, TransactionCommit(), and TransactionRollback().

Here is the call graph for this function:

Member Function Documentation

◆ Commit()

bool TransactionScope::Commit ( )

Definition at line 658 of file DBConnection.cpp.

659 {
660  if ( !mInTrans )
661  {
662  wxLogMessage("No active transaction to commit");
663 
664  // Misuse of this class
666  }
667 
669 
670  return mInTrans;
671 }

References mInTrans, mName, THROW_INCONSISTENCY_EXCEPTION, and TransactionCommit().

Referenced by Effect::DoEffect(), AudioIO::DrainRecordBuffers(), UndoManager::RemoveStates(), and AudioIO::StopStream().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransactionCommit()

bool TransactionScope::TransactionCommit ( const wxString &  name)
private

Definition at line 578 of file DBConnection.cpp.

579 {
580  char *errmsg = nullptr;
581 
582  int rc = sqlite3_exec(mConnection.DB(),
583  wxT("RELEASE ") + name + wxT(";"),
584  nullptr,
585  nullptr,
586  &errmsg);
587 
588  if (errmsg)
589  {
590  ADD_EXCEPTION_CONTEXT("sqlite3.rc", std::to_string(rc));
591  ADD_EXCEPTION_CONTEXT("sqlite3.context", "TransactionScope::TransactionCommit");
592 
594  XO("Failed to release savepoint:\n\n%s").Format(name)
595  );
596  sqlite3_free(errmsg);
597  }
598 
599  return rc == SQLITE_OK;
600 }

References ADD_EXCEPTION_CONTEXT, name, and XO.

Referenced by Commit(), and ~TransactionScope().

Here is the caller graph for this function:

◆ TransactionRollback()

bool TransactionScope::TransactionRollback ( const wxString &  name)
private

Definition at line 602 of file DBConnection.cpp.

603 {
604  char *errmsg = nullptr;
605 
606  int rc = sqlite3_exec(mConnection.DB(),
607  wxT("ROLLBACK TO ") + name + wxT(";"),
608  nullptr,
609  nullptr,
610  &errmsg);
611 
612  if (errmsg)
613  {
614  ADD_EXCEPTION_CONTEXT("sqlite3.rc", std::to_string(rc));
615  ADD_EXCEPTION_CONTEXT("sqlite3.context", "TransactionScope::TransactionRollback");
616 
618  XO("Failed to release savepoint:\n\n%s").Format(name)
619  );
620  sqlite3_free(errmsg);
621  }
622 
623  return rc == SQLITE_OK;
624 }

References ADD_EXCEPTION_CONTEXT, name, and XO.

Referenced by ~TransactionScope().

Here is the caller graph for this function:

◆ TransactionStart()

bool TransactionScope::TransactionStart ( const wxString &  name)
private

Definition at line 554 of file DBConnection.cpp.

555 {
556  char *errmsg = nullptr;
557 
558  int rc = sqlite3_exec(mConnection.DB(),
559  wxT("SAVEPOINT ") + name + wxT(";"),
560  nullptr,
561  nullptr,
562  &errmsg);
563 
564  if (errmsg)
565  {
566  ADD_EXCEPTION_CONTEXT("sqlite3.rc", std::to_string(rc));
567  ADD_EXCEPTION_CONTEXT("sqlite3.context", "TransactionScope::TransactionStart");
568 
570  XO("Failed to create savepoint:\n\n%s").Format(name)
571  );
572  sqlite3_free(errmsg);
573  }
574 
575  return rc == SQLITE_OK;
576 }

References ADD_EXCEPTION_CONTEXT, name, and XO.

Referenced by TransactionScope().

Here is the caller graph for this function:

Member Data Documentation

◆ mConnection

DBConnection& TransactionScope::mConnection
private

Definition at line 148 of file DBConnection.h.

◆ mInTrans

bool TransactionScope::mInTrans
private

Definition at line 149 of file DBConnection.h.

Referenced by Commit(), TransactionScope(), and ~TransactionScope().

◆ mName

wxString TransactionScope::mName
private

Definition at line 150 of file DBConnection.h.

Referenced by Commit(), TransactionScope(), and ~TransactionScope().


The documentation for this class was generated from the following files:
DBConnection::SetDBError
void SetDBError(const TranslatableString &msg, const TranslatableString &libraryError={}, int errorCode=-1)
Set stored errors and write to log; and default libraryError to what database library reports.
Definition: DBConnection.cpp:101
Format
Abstract base class used in importing a file.
XO
#define XO(s)
Definition: Internat.h:31
DBConnection::DB
sqlite3 * DB()
Definition: DBConnection.cpp:375
TransactionScope::mConnection
DBConnection & mConnection
Definition: DBConnection.h:148
TransactionScope::TransactionRollback
bool TransactionRollback(const wxString &name)
Definition: DBConnection.cpp:602
TransactionScope::TransactionCommit
bool TransactionCommit(const wxString &name)
Definition: DBConnection.cpp:578
TransactionScope::TransactionStart
bool TransactionStart(const wxString &name)
Definition: DBConnection.cpp:554
name
const TranslatableString name
Definition: Distortion.cpp:98
THROW_INCONSISTENCY_EXCEPTION
#define THROW_INCONSISTENCY_EXCEPTION
Throw InconsistencyException, using C++ preprocessor to identify the source code location.
Definition: InconsistencyException.h:79
TransactionScope::mName
wxString mName
Definition: DBConnection.h:150
ExceptionType::Internal
@ Internal
Indicates internal failure from Audacity.
TransactionScope::mInTrans
bool mInTrans
Definition: DBConnection.h:149
ADD_EXCEPTION_CONTEXT
#define ADD_EXCEPTION_CONTEXT(name, value)
Definition: SentryHelper.h:21
SimpleMessageBoxException
A MessageBoxException that shows a given, unvarying string.
Definition: AudacityException.h:95