2009-04-21 19:27:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#ifndef mozStorageAsyncStatementExecution_h
|
|
|
|
#define mozStorageAsyncStatementExecution_h
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2009-09-03 00:30:24 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2009-07-29 00:18:10 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2012-06-13 07:35:10 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsIRunnable.h"
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "SQLiteMutex.h"
|
2009-04-21 19:27:03 +04:00
|
|
|
#include "mozIStoragePendingStatement.h"
|
|
|
|
#include "mozIStorageStatementCallback.h"
|
2013-06-27 17:00:59 +04:00
|
|
|
#include "mozStorageHelper.h"
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
struct sqlite3_stmt;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
class Connection;
|
2009-05-09 04:29:56 +04:00
|
|
|
class ResultSet;
|
2009-06-17 23:12:51 +04:00
|
|
|
class StatementData;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class AsyncExecuteStatements final : public nsIRunnable
|
2015-03-27 21:52:19 +03:00
|
|
|
, public mozIStoragePendingStatement
|
2009-04-21 19:27:03 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:24:15 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2009-04-21 19:27:03 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_MOZISTORAGEPENDINGSTATEMENT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describes the state of execution.
|
|
|
|
*/
|
|
|
|
enum ExecutionState {
|
|
|
|
PENDING = -1,
|
|
|
|
COMPLETED = mozIStorageStatementCallback::REASON_FINISHED,
|
|
|
|
CANCELED = mozIStorageStatementCallback::REASON_CANCELED,
|
|
|
|
ERROR = mozIStorageStatementCallback::REASON_ERROR
|
|
|
|
};
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
typedef nsTArray<StatementData> StatementDataArray;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a statement in the background, and passes results back to the
|
|
|
|
* caller.
|
|
|
|
*
|
|
|
|
* @param aStatements
|
2009-06-17 23:12:51 +04:00
|
|
|
* The statements to execute and possibly bind in the background.
|
|
|
|
* Ownership is transfered from the caller.
|
2009-04-21 19:27:03 +04:00
|
|
|
* @param aConnection
|
|
|
|
* The connection that created the statements to execute.
|
2014-04-24 13:54:09 +04:00
|
|
|
* @param aNativeConnection
|
|
|
|
* The native Sqlite connection that created the statements to execute.
|
2009-04-21 19:27:03 +04:00
|
|
|
* @param aCallback
|
|
|
|
* The callback that is notified of results, completion, and errors.
|
|
|
|
* @param _stmt
|
|
|
|
* The handle to control the execution of the statements.
|
|
|
|
*/
|
2009-06-17 23:12:51 +04:00
|
|
|
static nsresult execute(StatementDataArray &aStatements,
|
2009-04-21 19:27:03 +04:00
|
|
|
Connection *aConnection,
|
2014-04-24 13:54:09 +04:00
|
|
|
sqlite3 *aNativeConnection,
|
2009-04-21 19:27:03 +04:00
|
|
|
mozIStorageStatementCallback *aCallback,
|
|
|
|
mozIStoragePendingStatement **_stmt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates when events on the calling thread should run or not. Certain
|
|
|
|
* events posted back to the calling thread should call this see if they
|
|
|
|
* should run or not.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
|
|
|
*
|
2009-04-21 19:27:03 +04:00
|
|
|
* @returns true if the event should notify still, false otherwise.
|
|
|
|
*/
|
|
|
|
bool shouldNotify();
|
|
|
|
|
|
|
|
private:
|
2009-06-17 23:12:51 +04:00
|
|
|
AsyncExecuteStatements(StatementDataArray &aStatements,
|
2009-06-17 23:12:43 +04:00
|
|
|
Connection *aConnection,
|
2014-04-24 13:54:09 +04:00
|
|
|
sqlite3 *aNativeConnection,
|
2009-04-21 19:27:03 +04:00
|
|
|
mozIStorageStatementCallback *aCallback);
|
2014-04-24 13:54:15 +04:00
|
|
|
~AsyncExecuteStatements();
|
2009-04-21 19:27:03 +04:00
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
/**
|
|
|
|
* Binds and then executes a given statement until completion, an error
|
|
|
|
* occurs, or we are canceled. If aLastStatement is true, we should set
|
|
|
|
* mState accordingly.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
|
|
|
*
|
2009-06-17 23:12:51 +04:00
|
|
|
* @param aData
|
|
|
|
* The StatementData to bind, execute, and then process.
|
|
|
|
* @param aLastStatement
|
|
|
|
* Indicates if this is the last statement or not. If it is, we have
|
|
|
|
* to set the proper state.
|
|
|
|
* @returns true if we should continue to process statements, false otherwise.
|
|
|
|
*/
|
|
|
|
bool bindExecuteAndProcessStatement(StatementData &aData,
|
|
|
|
bool aLastStatement);
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
/**
|
|
|
|
* Executes a given statement until completion, an error occurs, or we are
|
|
|
|
* canceled. If aLastStatement is true, we should set mState accordingly.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
|
|
|
*
|
2009-04-21 19:27:03 +04:00
|
|
|
* @param aStatement
|
|
|
|
* The statement to execute and then process.
|
|
|
|
* @param aLastStatement
|
|
|
|
* Indicates if this is the last statement or not. If it is, we have
|
|
|
|
* to set the proper state.
|
|
|
|
* @returns true if we should continue to process statements, false otherwise.
|
|
|
|
*/
|
|
|
|
bool executeAndProcessStatement(sqlite3_stmt *aStatement,
|
|
|
|
bool aLastStatement);
|
|
|
|
|
2009-06-17 23:12:43 +04:00
|
|
|
/**
|
|
|
|
* Executes a statement to completion, properly handling any error conditions.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
|
|
|
*
|
2009-06-17 23:12:43 +04:00
|
|
|
* @param aStatement
|
|
|
|
* The statement to execute to completion.
|
|
|
|
* @returns true if results were obtained, false otherwise.
|
|
|
|
*/
|
|
|
|
bool executeStatement(sqlite3_stmt *aStatement);
|
|
|
|
|
2009-04-21 19:27:03 +04:00
|
|
|
/**
|
|
|
|
* Builds a result set up with a row from a given statement. If we meet the
|
|
|
|
* right criteria, go ahead and notify about this results too.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
|
|
|
*
|
2009-04-21 19:27:03 +04:00
|
|
|
* @param aStatement
|
|
|
|
* The statement to get the row data from.
|
|
|
|
*/
|
|
|
|
nsresult buildAndNotifyResults(sqlite3_stmt *aStatement);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies callback about completion, and does any necessary cleanup.
|
2009-09-03 00:30:24 +04:00
|
|
|
*
|
|
|
|
* @pre mMutex is not held
|
2009-04-21 19:27:03 +04:00
|
|
|
*/
|
|
|
|
nsresult notifyComplete();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies callback about an error.
|
|
|
|
*
|
2009-09-03 00:30:24 +04:00
|
|
|
* @pre mMutex is not held
|
2010-03-24 10:32:40 +03:00
|
|
|
* @pre mDBMutex is not held
|
2009-09-03 00:30:24 +04:00
|
|
|
*
|
2009-04-21 19:27:03 +04:00
|
|
|
* @param aErrorCode
|
|
|
|
* The error code defined in mozIStorageError for the error.
|
|
|
|
* @param aMessage
|
|
|
|
* The error string, if any.
|
2009-06-17 23:12:51 +04:00
|
|
|
* @param aError
|
|
|
|
* The error object to notify the caller with.
|
2009-04-21 19:27:03 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult notifyError(int32_t aErrorCode, const char *aMessage);
|
2009-06-17 23:12:51 +04:00
|
|
|
nsresult notifyError(mozIStorageError *aError);
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the callback about a result set.
|
2009-09-03 00:30:24 +04:00
|
|
|
*
|
|
|
|
* @pre mMutex is not held
|
2009-04-21 19:27:03 +04:00
|
|
|
*/
|
|
|
|
nsresult notifyResults();
|
|
|
|
|
2011-05-24 02:15:01 +04:00
|
|
|
/**
|
|
|
|
* Tests whether the current statements should be wrapped in an explicit
|
|
|
|
* transaction.
|
|
|
|
*
|
|
|
|
* @return true if an explicit transaction is needed, false otherwise.
|
|
|
|
*/
|
|
|
|
bool statementsNeedTransaction();
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
StatementDataArray mStatements;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Connection> mConnection;
|
2014-04-24 13:54:09 +04:00
|
|
|
sqlite3 *mNativeConnection;
|
2014-04-24 13:54:15 +04:00
|
|
|
bool mHasTransaction;
|
2017-03-28 13:54:02 +03:00
|
|
|
// Note, this may not be a threadsafe object - never addref/release off
|
|
|
|
// the calling thread. We take a reference when this is created, and
|
|
|
|
// release it in the CompletionNotifier::Run() call back to this thread.
|
|
|
|
RefPtr<mozIStorageStatementCallback> mCallback;
|
2009-04-21 19:27:03 +04:00
|
|
|
nsCOMPtr<nsIThread> mCallingThread;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ResultSet> mResultSet;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum amount of time we want to wait between results. Defined by
|
|
|
|
* MAX_MILLISECONDS_BETWEEN_RESULTS and set at construction.
|
|
|
|
*/
|
2009-07-29 00:18:10 +04:00
|
|
|
const TimeDuration mMaxWait;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The start time since our last set of results.
|
|
|
|
*/
|
2009-07-29 00:18:10 +04:00
|
|
|
TimeStamp mIntervalStart;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2009-09-03 00:30:24 +04:00
|
|
|
* Indicates our state of execution.
|
2009-04-21 19:27:03 +04:00
|
|
|
*/
|
2009-09-03 00:30:24 +04:00
|
|
|
ExecutionState mState;
|
2009-04-21 19:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2009-09-03 00:30:24 +04:00
|
|
|
* Indicates if we should try to cancel at a cancelation point.
|
|
|
|
*/
|
|
|
|
bool mCancelRequested;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the mutex that protects our state from changing between threads.
|
|
|
|
* This includes the following variables:
|
|
|
|
* - mCancelRequested is only set on the calling thread while the lock is
|
|
|
|
* held. It is always read from within the lock on the background thread,
|
|
|
|
* but not on the calling thread (see shouldNotify for why).
|
2009-04-21 19:27:03 +04:00
|
|
|
*/
|
2009-09-03 00:30:24 +04:00
|
|
|
Mutex &mMutex;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The wrapped SQLite recursive connection mutex. We use it whenever we call
|
|
|
|
* sqlite3_step and care about having reliable error messages. By taking it
|
|
|
|
* prior to the call and holding it until the point where we no longer care
|
|
|
|
* about the error message, the user gets reliable error messages.
|
|
|
|
*/
|
|
|
|
SQLiteMutex &mDBMutex;
|
2011-10-13 12:48:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The instant at which the request was started.
|
|
|
|
*
|
|
|
|
* Used by telemetry.
|
|
|
|
*/
|
|
|
|
TimeStamp mRequestStartDate;
|
2009-04-21 19:27:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#endif // mozStorageAsyncStatementExecution_h
|