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
15 * The Original Code is Storage code
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
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"
43 #include "mozIStorageStatement.h"
47 * This class wraps a transaction inside a given C++ scope, guaranteeing that
48 * the transaction will be completed even if you have an exception or
51 * aCommitOnComplete controls whether the transaction is committed or rolled
52 * back when it goes out of scope. A common use is to create an instance with
53 * commitOnComplete = FALSE (rollback), then call Commit on this object manually
54 * when your function completes successfully.
56 * Note that nested transactions are not supported by sqlite, so if a transaction
57 * is already in progress, this object does nothing. Note that in this case,
58 * you may not get the transaction type you ask for, and you won't be able
61 class mozStorageTransaction
64 mozStorageTransaction(mozIStorageConnection
* aConnection
,
65 PRBool aCommitOnComplete
,
66 PRInt32 aType
= mozIStorageConnection::TRANSACTION_DEFERRED
)
67 : mConnection(aConnection
),
68 mHasTransaction(PR_FALSE
),
69 mCommitOnComplete(aCommitOnComplete
),
72 // We won't try to get a transaction if one is already in progress.
74 mHasTransaction
= NS_SUCCEEDED(mConnection
->BeginTransactionAs(aType
));
76 ~mozStorageTransaction()
78 if (mConnection
&& mHasTransaction
&& ! mCompleted
) {
79 if (mCommitOnComplete
)
80 mConnection
->CommitTransaction();
82 mConnection
->RollbackTransaction();
87 * Commits the transaction if one is in progress. If one is not in progress,
88 * this is a NOP since the actual owner of the transaction outside of our
89 * scope is in charge of finally comitting or rolling back the transaction.
93 if (!mConnection
|| mCompleted
)
94 return NS_OK
; // no connection, or already done
96 if (! mHasTransaction
)
97 return NS_OK
; // transaction not ours, ignore
98 return mConnection
->CommitTransaction();
102 * Rolls back the transaction in progress. You should only call this function
103 * if this object has a real transaction (HasTransaction() = true) because
104 * otherwise, there is no transaction to roll back.
108 if (!mConnection
|| mCompleted
)
109 return NS_OK
; // no connection, or already done
110 mCompleted
= PR_TRUE
;
111 if (! mHasTransaction
)
112 return NS_ERROR_FAILURE
;
113 return mConnection
->RollbackTransaction();
117 * Returns whether this object wraps a real transaction. False means that
118 * this object doesn't do anything because there was already a transaction in
119 * progress when it was created.
121 PRBool
HasTransaction()
123 return mHasTransaction
;
127 * This sets the default action (commit or rollback) when this object goes
130 void SetDefaultAction(PRBool aCommitOnComplete
)
132 mCommitOnComplete
= aCommitOnComplete
;
136 nsCOMPtr
<mozIStorageConnection
> mConnection
;
137 PRBool mHasTransaction
;
138 PRBool mCommitOnComplete
;
144 * This class wraps a statement so that it is guaraneed to be reset when
145 * this object goes out of scope.
147 * Note that this always just resets the statement. If the statement doesn't
148 * need resetting, the reset operation is inexpensive.
150 class NS_STACK_CLASS mozStorageStatementScoper
153 mozStorageStatementScoper(mozIStorageStatement
* aStatement
)
154 : mStatement(aStatement
)
157 ~mozStorageStatementScoper()
164 * Call this to make the statement not reset. You might do this if you know
165 * that the statement has been reset.
173 nsCOMPtr
<mozIStorageStatement
> mStatement
;
176 #endif /* _MOZSTORAGEHELPER_H_ */