Whitespace change to force builds.
[wine-gecko.git] / storage / public / mozStorageHelper.h
blob9afbf1842848cf703651cdd0819a6ee9b14a2dbb
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Storage code
17 * The Initial Developer of the Original Code is
18 * Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Brett Wilson <brettw@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef _MOZSTORAGEHELPER_H_
40 #define _MOZSTORAGEHELPER_H_
42 #include "mozIStorageConnection.h"
45 /**
46 * This class wraps a transaction inside a given C++ scope, guaranteeing that
47 * the transaction will be completed even if you have an exception or
48 * return early.
50 * aCommitOnComplete controls whether the transaction is committed or rolled
51 * back when it goes out of scope. A common use is to create an instance with
52 * commitOnComplete = FALSE (rollback), then call Commit on this object manually
53 * when your function completes successfully.
55 * Note that nested transactions are not supported by sqlite, so if a transaction
56 * is already in progress, this object does nothing. Note that in this case,
57 * you may not get the transaction type you ask for, and you won't be able
58 * to rollback.
60 class mozStorageTransaction
62 public:
63 mozStorageTransaction(mozIStorageConnection* aConnection,
64 PRBool aCommitOnComplete,
65 PRInt32 aType = mozIStorageConnection::TRANSACTION_DEFERRED)
66 : mConnection(aConnection),
67 mHasTransaction(PR_FALSE),
68 mCommitOnComplete(aCommitOnComplete),
69 mCompleted(PR_FALSE)
71 if (mConnection) {
72 PRBool transactionInProgress = PR_FALSE;
73 mConnection->GetTransactionInProgress(&transactionInProgress);
74 mHasTransaction = ! transactionInProgress;
75 if (mHasTransaction)
76 mConnection->BeginTransactionAs(aType);
79 ~mozStorageTransaction()
81 if (mConnection && mHasTransaction && ! mCompleted) {
82 if (mCommitOnComplete)
83 mConnection->CommitTransaction();
84 else
85 mConnection->RollbackTransaction();
89 /**
90 * Commits the transaction if one is in progress. If one is not in progress,
91 * this is a NOP since the actual owner of the transaction outside of our
92 * scope is in charge of finally comitting or rolling back the transaction.
94 nsresult Commit()
96 if (!mConnection || mCompleted)
97 return NS_OK; // no connection, or already done
98 mCompleted = PR_TRUE;
99 if (! mHasTransaction)
100 return NS_OK; // transaction not ours, ignore
101 return mConnection->CommitTransaction();
105 * Rolls back the transaction in progress. You should only call this function
106 * if this object has a real transaction (HasTransaction() = true) because
107 * otherwise, there is no transaction to roll back.
109 nsresult Rollback()
111 if (!mConnection || mCompleted)
112 return NS_OK; // no connection, or already done
113 mCompleted = PR_TRUE;
114 if (! mHasTransaction)
115 return NS_ERROR_FAILURE;
116 return mConnection->RollbackTransaction();
120 * Returns whether this object wraps a real transaction. False means that
121 * this object doesn't do anything because there was already a transaction in
122 * progress when it was created.
124 PRBool HasTransaction()
126 return mHasTransaction;
130 * This sets the default action (commit or rollback) when this object goes
131 * out of scope.
133 void SetDefaultAction(PRBool aCommitOnComplete)
135 mCommitOnComplete = aCommitOnComplete;
138 protected:
139 nsCOMPtr<mozIStorageConnection> mConnection;
140 PRBool mHasTransaction;
141 PRBool mCommitOnComplete;
142 PRBool mCompleted;
147 * This class wraps a statement so that it is guaraneed to be reset when
148 * this object goes out of scope.
150 * Note that this always just resets the statement. If the statement doesn't
151 * need resetting, the reset operation is inexpensive.
153 class NS_STACK_CLASS mozStorageStatementScoper
155 public:
156 mozStorageStatementScoper(mozIStorageStatement* aStatement)
157 : mStatement(aStatement)
160 ~mozStorageStatementScoper()
162 if (mStatement)
163 mStatement->Reset();
167 * Call this to make the statement not reset. You might do this if you know
168 * that the statement has been reset.
170 void Abandon()
172 mStatement = nsnull;
175 protected:
176 nsCOMPtr<mozIStorageStatement> mStatement;
179 #endif /* _MOZSTORAGEHELPER_H_ */