Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Attributes
mozStorageTransaction Class Reference

This class wraps a transaction inside a given C++ scope, guaranteeing that the transaction will be completed even if you have an exception or return early. More...

#include <mozStorageHelper.h>

Collaboration diagram for mozStorageTransaction:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 mozStorageTransaction (mozIStorageConnection *aConnection, PRBool aCommitOnComplete, PRInt32 aType=mozIStorageConnection::TRANSACTION_DEFERRED)
 ~mozStorageTransaction ()
nsresult Commit ()
 Commits the transaction if one is in progress.
nsresult Rollback ()
 Rolls back the transaction in progress.
PRBool HasTransaction ()
 Returns whether this object wraps a real transaction.
void SetDefaultAction (PRBool aCommitOnComplete)
 This sets the default action (commit or rollback) when this object goes out of scope.

Protected Attributes

nsCOMPtr< mozIStorageConnectionmConnection
PRBool mHasTransaction
PRBool mCommitOnComplete
PRBool mCompleted

Detailed Description

This class wraps a transaction inside a given C++ scope, guaranteeing that the transaction will be completed even if you have an exception or return early.

aCommitOnComplete controls whether the transaction is committed or rolled back when it goes out of scope. A common use is to create an instance with commitOnComplete = FALSE (rollback), then call Commit on this object manually when your function completes successfully.

Note that nested transactions are not supported by sqlite, so if a transaction is already in progress, this object does nothing. Note that in this case, you may not get the transaction type you ask for, and you won't be able to rollback.

Definition at line 60 of file mozStorageHelper.h.


Constructor & Destructor Documentation

Definition at line 63 of file mozStorageHelper.h.

    : mConnection(aConnection),
      mCommitOnComplete(aCommitOnComplete),
      mCompleted(PR_FALSE)
  {
    PRBool transactionInProgress = PR_FALSE;
    mConnection->GetTransactionInProgress(&transactionInProgress);
    mHasTransaction = ! transactionInProgress;
    if (mHasTransaction)
      mConnection->BeginTransactionAs(aType);
  }

Definition at line 76 of file mozStorageHelper.h.

  {
    if (mHasTransaction && ! mCompleted) {
      if (mCommitOnComplete)
        mConnection->CommitTransaction();
      else
        mConnection->RollbackTransaction();
    }
  }

Member Function Documentation

Commits the transaction if one is in progress.

If one is not in progress, this is a NOP since the actual owner of the transaction outside of our scope is in charge of finally comitting or rolling back the transaction.

Definition at line 91 of file mozStorageHelper.h.

  {
    if (mCompleted)
      return NS_OK; // already done
    mCompleted = PR_TRUE;
    if (! mHasTransaction)
      return NS_OK; // transaction not ours, ignore
    return mConnection->CommitTransaction();
  }

Returns whether this object wraps a real transaction.

False means that this object doesn't do anything because there was already a transaction in progress when it was created.

Definition at line 121 of file mozStorageHelper.h.

  {
    return mHasTransaction;
  }

Rolls back the transaction in progress.

You should only call this function if this object has a real transaction (HasTransaction() = true) because otherwise, there is no transaction to roll back.

Definition at line 106 of file mozStorageHelper.h.

  {
    if (mCompleted)
      return NS_OK; // already done
    mCompleted = PR_TRUE;
    if (! mHasTransaction)
      return NS_ERROR_FAILURE;
    return mConnection->RollbackTransaction();
  }
void mozStorageTransaction::SetDefaultAction ( PRBool  aCommitOnComplete) [inline]

This sets the default action (commit or rollback) when this object goes out of scope.

Definition at line 130 of file mozStorageHelper.h.

  {
    mCommitOnComplete = aCommitOnComplete;
  }

Member Data Documentation

Definition at line 138 of file mozStorageHelper.h.

Definition at line 139 of file mozStorageHelper.h.

Definition at line 136 of file mozStorageHelper.h.

Definition at line 137 of file mozStorageHelper.h.


The documentation for this class was generated from the following file: