1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef mozStorageAsyncStatementExecution_h
41 #define mozStorageAsyncStatementExecution_h
45 #include "nsAutoPtr.h"
46 #include "nsThreadUtils.h"
47 #include "mozilla/Mutex.h"
48 #include "mozilla/TimeStamp.h"
50 #include "SQLiteMutex.h"
51 #include "mozIStoragePendingStatement.h"
52 #include "mozIStorageStatementCallback.h"
55 class mozStorageTransaction
;
64 class AsyncExecuteStatements
: public nsIRunnable
65 , public mozIStoragePendingStatement
70 NS_DECL_MOZISTORAGEPENDINGSTATEMENT
73 * Describes the state of execution.
77 COMPLETED
= mozIStorageStatementCallback::REASON_FINISHED
,
78 CANCELED
= mozIStorageStatementCallback::REASON_CANCELED
,
79 ERROR
= mozIStorageStatementCallback::REASON_ERROR
82 typedef nsTArray
<StatementData
> StatementDataArray
;
85 * Executes a statement in the background, and passes results back to the
89 * The statements to execute and possibly bind in the background.
90 * Ownership is transfered from the caller.
92 * The connection that created the statements to execute.
94 * The callback that is notified of results, completion, and errors.
96 * The handle to control the execution of the statements.
98 static nsresult
execute(StatementDataArray
&aStatements
,
99 Connection
*aConnection
,
100 mozIStorageStatementCallback
*aCallback
,
101 mozIStoragePendingStatement
**_stmt
);
104 * Indicates when events on the calling thread should run or not. Certain
105 * events posted back to the calling thread should call this see if they
108 * @pre mMutex is not held
110 * @returns true if the event should notify still, false otherwise.
115 AsyncExecuteStatements(StatementDataArray
&aStatements
,
116 Connection
*aConnection
,
117 mozIStorageStatementCallback
*aCallback
);
120 * Binds and then executes a given statement until completion, an error
121 * occurs, or we are canceled. If aLastStatement is true, we should set
122 * mState accordingly.
124 * @pre mMutex is not held
127 * The StatementData to bind, execute, and then process.
128 * @param aLastStatement
129 * Indicates if this is the last statement or not. If it is, we have
130 * to set the proper state.
131 * @returns true if we should continue to process statements, false otherwise.
133 bool bindExecuteAndProcessStatement(StatementData
&aData
,
134 bool aLastStatement
);
137 * Executes a given statement until completion, an error occurs, or we are
138 * canceled. If aLastStatement is true, we should set mState accordingly.
140 * @pre mMutex is not held
143 * The statement to execute and then process.
144 * @param aLastStatement
145 * Indicates if this is the last statement or not. If it is, we have
146 * to set the proper state.
147 * @returns true if we should continue to process statements, false otherwise.
149 bool executeAndProcessStatement(sqlite3_stmt
*aStatement
,
150 bool aLastStatement
);
153 * Executes a statement to completion, properly handling any error conditions.
155 * @pre mMutex is not held
158 * The statement to execute to completion.
159 * @returns true if results were obtained, false otherwise.
161 bool executeStatement(sqlite3_stmt
*aStatement
);
164 * Builds a result set up with a row from a given statement. If we meet the
165 * right criteria, go ahead and notify about this results too.
167 * @pre mMutex is not held
170 * The statement to get the row data from.
172 nsresult
buildAndNotifyResults(sqlite3_stmt
*aStatement
);
175 * Notifies callback about completion, and does any necessary cleanup.
177 * @pre mMutex is not held
179 nsresult
notifyComplete();
182 * Notifies callback about an error.
184 * @pre mMutex is not held
185 * @pre mDBMutex is not held
188 * The error code defined in mozIStorageError for the error.
190 * The error string, if any.
192 * The error object to notify the caller with.
194 nsresult
notifyError(PRInt32 aErrorCode
, const char *aMessage
);
195 nsresult
notifyError(mozIStorageError
*aError
);
198 * Notifies the callback about a result set.
200 * @pre mMutex is not held
202 nsresult
notifyResults();
204 StatementDataArray mStatements
;
205 nsRefPtr
<Connection
> mConnection
;
206 mozStorageTransaction
*mTransactionManager
;
207 mozIStorageStatementCallback
*mCallback
;
208 nsCOMPtr
<nsIThread
> mCallingThread
;
209 nsRefPtr
<ResultSet
> mResultSet
;
212 * The maximum amount of time we want to wait between results. Defined by
213 * MAX_MILLISECONDS_BETWEEN_RESULTS and set at construction.
215 const TimeDuration mMaxWait
;
218 * The start time since our last set of results.
220 TimeStamp mIntervalStart
;
223 * Indicates our state of execution.
225 ExecutionState mState
;
228 * Indicates if we should try to cancel at a cancelation point.
230 bool mCancelRequested
;
233 * This is the mutex that protects our state from changing between threads.
234 * This includes the following variables:
235 * - mCancelRequested is only set on the calling thread while the lock is
236 * held. It is always read from within the lock on the background thread,
237 * but not on the calling thread (see shouldNotify for why).
242 * The wrapped SQLite recursive connection mutex. We use it whenever we call
243 * sqlite3_step and care about having reliable error messages. By taking it
244 * prior to the call and holding it until the point where we no longer care
245 * about the error message, the user gets reliable error messages.
247 SQLiteMutex
&mDBMutex
;
250 } // namespace storage
251 } // namespace mozilla
253 #endif // mozStorageAsyncStatementExecution_h