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 134 of file DBConnection.h.

Constructor & Destructor Documentation

◆ TransactionScope()

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

Definition at line 591 of file DBConnection.cpp.

593 : mConnection(connection),
594  mName(name)
595 {
597  if ( !mInTrans )
598  // To do, improve the message
600  XO("Database error. Sorry, but we don't have more details."),
601  XO("Warning"),
602  "Error:_Disk_full_or_not_writable"
603  );
604 }

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

Here is the call graph for this function:

◆ ~TransactionScope()

TransactionScope::~TransactionScope ( )

Definition at line 606 of file DBConnection.cpp.

607 {
608  if (mInTrans)
609  {
610  // Rollback AND REMOVE the transaction
611  // -- must do both; rolling back a savepoint only rewinds it
612  // without removing it, unlike the ROLLBACK command
613  if (!(TransactionRollback(mName) &&
615  {
616  // Do not throw from a destructor!
617  // This has to be a no-fail cleanup that does the best that it can.
618  wxLogMessage("Transaction active at scope destruction");
619  }
620  }
621 }

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

Here is the call graph for this function:

Member Function Documentation

◆ Commit()

bool TransactionScope::Commit ( )

Definition at line 623 of file DBConnection.cpp.

624 {
625  if ( !mInTrans )
626  {
627  wxLogMessage("No active transaction to commit");
628 
629  // Misuse of this class
631  }
632 
634 
635  return mInTrans;
636 }

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

Referenced by Effect::DoEffect(), AudioIO::FillBuffers(), 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 549 of file DBConnection.cpp.

550 {
551  char *errmsg = nullptr;
552 
553  int rc = sqlite3_exec(mConnection.DB(),
554  wxT("RELEASE ") + name + wxT(";"),
555  nullptr,
556  nullptr,
557  &errmsg);
558 
559  if (errmsg)
560  {
562  XO("Failed to release savepoint:\n\n%s").Format(name)
563  );
564  sqlite3_free(errmsg);
565  }
566 
567  return rc == SQLITE_OK;
568 }

References 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 570 of file DBConnection.cpp.

571 {
572  char *errmsg = nullptr;
573 
574  int rc = sqlite3_exec(mConnection.DB(),
575  wxT("ROLLBACK TO ") + name + wxT(";"),
576  nullptr,
577  nullptr,
578  &errmsg);
579 
580  if (errmsg)
581  {
583  XO("Failed to release savepoint:\n\n%s").Format(name)
584  );
585  sqlite3_free(errmsg);
586  }
587 
588  return rc == SQLITE_OK;
589 }

References 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 528 of file DBConnection.cpp.

529 {
530  char *errmsg = nullptr;
531 
532  int rc = sqlite3_exec(mConnection.DB(),
533  wxT("SAVEPOINT ") + name + wxT(";"),
534  nullptr,
535  nullptr,
536  &errmsg);
537 
538  if (errmsg)
539  {
541  XO("Failed to create savepoint:\n\n%s").Format(name)
542  );
543  sqlite3_free(errmsg);
544  }
545 
546  return rc == SQLITE_OK;
547 }

References 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 147 of file DBConnection.h.

◆ mInTrans

bool TransactionScope::mInTrans
private

Definition at line 148 of file DBConnection.h.

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

◆ mName

wxString TransactionScope::mName
private

Definition at line 149 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:100
Format
Abstract base class used in importing a file.
XO
#define XO(s)
Definition: Internat.h:32
DBConnection::DB
sqlite3 * DB()
Definition: DBConnection.cpp:356
TransactionScope::mConnection
DBConnection & mConnection
Definition: DBConnection.h:147
TransactionScope::TransactionRollback
bool TransactionRollback(const wxString &name)
Definition: DBConnection.cpp:570
TransactionScope::TransactionCommit
bool TransactionCommit(const wxString &name)
Definition: DBConnection.cpp:549
TransactionScope::TransactionStart
bool TransactionStart(const wxString &name)
Definition: DBConnection.cpp:528
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:77
TransactionScope::mName
wxString mName
Definition: DBConnection.h:149
TransactionScope::mInTrans
bool mInTrans
Definition: DBConnection.h:148
SimpleMessageBoxException
A MessageBoxException that shows a given, unvarying string.
Definition: AudacityException.h:78