Back to index

lightning-sunbird  0.9+nobinonly
mozStorageHelper.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Storage code
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Brett Wilson <brettw@gmail.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef _MOZSTORAGEHELPER_H_
00040 #define _MOZSTORAGEHELPER_H_
00041 
00042 #include "mozIStorageConnection.h"
00043 
00044 
00060 class mozStorageTransaction
00061 {
00062 public:
00063   mozStorageTransaction(mozIStorageConnection* aConnection,
00064                         PRBool aCommitOnComplete,
00065                         PRInt32 aType = mozIStorageConnection::TRANSACTION_DEFERRED)
00066     : mConnection(aConnection),
00067       mCommitOnComplete(aCommitOnComplete),
00068       mCompleted(PR_FALSE)
00069   {
00070     PRBool transactionInProgress = PR_FALSE;
00071     mConnection->GetTransactionInProgress(&transactionInProgress);
00072     mHasTransaction = ! transactionInProgress;
00073     if (mHasTransaction)
00074       mConnection->BeginTransactionAs(aType);
00075   }
00076   ~mozStorageTransaction()
00077   {
00078     if (mHasTransaction && ! mCompleted) {
00079       if (mCommitOnComplete)
00080         mConnection->CommitTransaction();
00081       else
00082         mConnection->RollbackTransaction();
00083     }
00084   }
00085 
00091   nsresult Commit()
00092   {
00093     if (mCompleted)
00094       return NS_OK; // already done
00095     mCompleted = PR_TRUE;
00096     if (! mHasTransaction)
00097       return NS_OK; // transaction not ours, ignore
00098     return mConnection->CommitTransaction();
00099   }
00100 
00106   nsresult Rollback()
00107   {
00108     if (mCompleted)
00109       return NS_OK; // already done
00110     mCompleted = PR_TRUE;
00111     if (! mHasTransaction)
00112       return NS_ERROR_FAILURE;
00113     return mConnection->RollbackTransaction();
00114   }
00115 
00121   PRBool HasTransaction()
00122   {
00123     return mHasTransaction;
00124   }
00125 
00130   void SetDefaultAction(PRBool aCommitOnComplete)
00131   {
00132     mCommitOnComplete = aCommitOnComplete;
00133   }
00134 
00135 protected:
00136   nsCOMPtr<mozIStorageConnection> mConnection;
00137   PRBool mHasTransaction;
00138   PRBool mCommitOnComplete;
00139   PRBool mCompleted;
00140 };
00141 
00142 
00150 class mozStorageStatementScoper
00151 {
00152 public:
00153   mozStorageStatementScoper(mozIStorageStatement* aStatement)
00154       : mStatement(aStatement)
00155   {
00156   }
00157   ~mozStorageStatementScoper()
00158   {
00159     if (mStatement)
00160       mStatement->Reset();
00161   }
00162 
00167   void Abandon()
00168   {
00169     mStatement = nsnull;
00170   }
00171 
00172 protected:
00173   nsCOMPtr<mozIStorageStatement> mStatement;
00174 };
00175 
00176 #endif /* _MOZSTORAGEHELPER_H_ */