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"
44 #include "mozStorage.h"
48 * This class wraps a transaction inside a given C++ scope, guaranteeing that
49 * the transaction will be completed even if you have an exception or
52 * aCommitOnComplete controls whether the transaction is committed or rolled
53 * back when it goes out of scope. A common use is to create an instance with
54 * commitOnComplete = FALSE (rollback), then call Commit on this object manually
55 * when your function completes successfully.
57 * Note that nested transactions are not supported by sqlite, so if a transaction
58 * is already in progress, this object does nothing. Note that in this case,
59 * you may not get the transaction type you ask for, and you won't be able
62 class mozStorageTransaction
65 mozStorageTransaction(mozIStorageConnection
* aConnection
,
66 PRBool aCommitOnComplete
,
67 PRInt32 aType
= mozIStorageConnection::TRANSACTION_DEFERRED
)
68 : mConnection(aConnection
),
69 mHasTransaction(PR_FALSE
),
70 mCommitOnComplete(aCommitOnComplete
),
73 // We won't try to get a transaction if one is already in progress.
75 mHasTransaction
= NS_SUCCEEDED(mConnection
->BeginTransactionAs(aType
));
77 ~mozStorageTransaction()
79 if (mConnection
&& mHasTransaction
&& ! mCompleted
) {
80 if (mCommitOnComplete
)
81 mConnection
->CommitTransaction();
83 mConnection
->RollbackTransaction();
88 * Commits the transaction if one is in progress. If one is not in progress,
89 * this is a NOP since the actual owner of the transaction outside of our
90 * scope is in charge of finally comitting or rolling back the transaction.
94 if (!mConnection
|| mCompleted
)
95 return NS_OK
; // no connection, or already done
97 if (! mHasTransaction
)
98 return NS_OK
; // transaction not ours, ignore
99 return mConnection
->CommitTransaction();
103 * Rolls back the transaction in progress. You should only call this function
104 * if this object has a real transaction (HasTransaction() = true) because
105 * otherwise, there is no transaction to roll back.
109 if (!mConnection
|| mCompleted
)
110 return NS_OK
; // no connection, or already done
111 mCompleted
= PR_TRUE
;
112 if (! mHasTransaction
)
113 return NS_ERROR_FAILURE
;
115 // It is possible that a rollback will return busy, so we busy wait...
118 rv
= mConnection
->RollbackTransaction();
119 if (rv
== NS_ERROR_STORAGE_BUSY
)
120 (void)PR_Sleep(PR_INTERVAL_NO_WAIT
);
121 } while (rv
== NS_ERROR_STORAGE_BUSY
);
127 * Returns whether this object wraps a real transaction. False means that
128 * this object doesn't do anything because there was already a transaction in
129 * progress when it was created.
131 PRBool
HasTransaction()
133 return mHasTransaction
;
137 * This sets the default action (commit or rollback) when this object goes
140 void SetDefaultAction(PRBool aCommitOnComplete
)
142 mCommitOnComplete
= aCommitOnComplete
;
146 nsCOMPtr
<mozIStorageConnection
> mConnection
;
147 PRBool mHasTransaction
;
148 PRBool mCommitOnComplete
;
154 * This class wraps a statement so that it is guaraneed to be reset when
155 * this object goes out of scope.
157 * Note that this always just resets the statement. If the statement doesn't
158 * need resetting, the reset operation is inexpensive.
160 class NS_STACK_CLASS mozStorageStatementScoper
163 mozStorageStatementScoper(mozIStorageStatement
* aStatement
)
164 : mStatement(aStatement
)
167 ~mozStorageStatementScoper()
174 * Call this to make the statement not reset. You might do this if you know
175 * that the statement has been reset.
183 nsCOMPtr
<mozIStorageStatement
> mStatement
;
186 #endif /* _MOZSTORAGEHELPER_H_ */