2009-06-17 12:12:51 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 sts=2 et
|
2012-05-21 04:12:37 -07: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-06-17 12:12:51 -07:00
|
|
|
|
2010-06-01 05:38:00 -07:00
|
|
|
#ifndef mozStorageStatementData_h
|
|
|
|
#define mozStorageStatementData_h
|
2009-06-17 12:12:51 -07:00
|
|
|
|
2009-07-28 10:21:03 -07:00
|
|
|
#include "sqlite3.h"
|
|
|
|
|
2009-06-17 12:12:51 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsTArray.h"
|
2011-02-28 11:52:31 -08:00
|
|
|
#include "nsIEventTarget.h"
|
2013-09-19 06:54:42 -07:00
|
|
|
#include "MainThreadUtils.h"
|
2009-06-17 12:12:51 -07:00
|
|
|
|
|
|
|
#include "mozStorageBindingParamsArray.h"
|
2010-03-24 00:32:40 -07:00
|
|
|
#include "mozIStorageBaseStatement.h"
|
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "StorageBaseStatementInternal.h"
|
2009-06-17 12:12:51 -07:00
|
|
|
|
|
|
|
struct sqlite3_stmt;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
class StatementData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StatementData(sqlite3_stmt *aStatement,
|
2009-07-28 10:21:03 -07:00
|
|
|
already_AddRefed<BindingParamsArray> aParamsArray,
|
2010-03-24 00:32:40 -07:00
|
|
|
StorageBaseStatementInternal *aStatementOwner)
|
2009-06-17 12:12:51 -07:00
|
|
|
: mStatement(aStatement)
|
|
|
|
, mParamsArray(aParamsArray)
|
2009-07-28 10:21:03 -07:00
|
|
|
, mStatementOwner(aStatementOwner)
|
2009-06-17 12:12:51 -07:00
|
|
|
{
|
2011-02-28 11:52:31 -08:00
|
|
|
NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
|
2009-06-17 12:12:51 -07:00
|
|
|
}
|
|
|
|
StatementData(const StatementData &aSource)
|
|
|
|
: mStatement(aSource.mStatement)
|
|
|
|
, mParamsArray(aSource.mParamsArray)
|
2009-07-28 10:21:03 -07:00
|
|
|
, mStatementOwner(aSource.mStatementOwner)
|
2009-06-17 12:12:51 -07:00
|
|
|
{
|
2011-02-28 11:52:31 -08:00
|
|
|
NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
|
2009-06-17 12:12:51 -07:00
|
|
|
}
|
|
|
|
StatementData()
|
2016-01-12 10:16:59 -08:00
|
|
|
: mStatement(nullptr)
|
2009-06-17 12:12:51 -07:00
|
|
|
{
|
|
|
|
}
|
2013-08-27 14:07:04 -07:00
|
|
|
~StatementData()
|
|
|
|
{
|
|
|
|
// We need to ensure that mParamsArray is released on the main thread,
|
|
|
|
// as the binding arguments may be XPConnect values, which are safe
|
|
|
|
// to release only on the main thread.
|
2016-02-09 23:23:00 -08:00
|
|
|
NS_ReleaseOnMainThread(mParamsArray.forget());
|
2013-08-27 14:07:04 -07:00
|
|
|
}
|
2009-06-17 12:12:51 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
/**
|
|
|
|
* Return the sqlite statement, fetching it from the storage statement. In
|
|
|
|
* the case of AsyncStatements this may actually create the statement
|
|
|
|
*/
|
|
|
|
inline int getSqliteStatement(sqlite3_stmt **_stmt)
|
2009-06-17 12:12:51 -07:00
|
|
|
{
|
2010-03-24 00:32:40 -07:00
|
|
|
if (!mStatement) {
|
|
|
|
int rc = mStatementOwner->getAsyncStatement(&mStatement);
|
|
|
|
NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
|
|
|
|
}
|
|
|
|
*_stmt = mStatement;
|
|
|
|
return SQLITE_OK;
|
2009-06-17 12:12:51 -07:00
|
|
|
}
|
2010-03-24 00:32:40 -07:00
|
|
|
|
2009-06-17 12:12:51 -07:00
|
|
|
operator BindingParamsArray *() const { return mParamsArray; }
|
|
|
|
|
|
|
|
/**
|
2009-07-28 10:21:03 -07:00
|
|
|
* NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
|
2010-03-24 00:32:40 -07:00
|
|
|
* clear all bindings to it. This is expected to occur on the async thread.
|
2009-06-17 12:12:51 -07:00
|
|
|
*/
|
2013-08-27 14:07:04 -07:00
|
|
|
inline void reset()
|
2009-06-17 12:12:51 -07:00
|
|
|
{
|
2011-02-28 11:52:31 -08:00
|
|
|
NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEventTarget> asyncThread =
|
|
|
|
mStatementOwner->getOwner()->getAsyncExecutionTarget();
|
|
|
|
// It's possible that we are shutting down the async thread, and this
|
2013-07-31 08:44:58 -07:00
|
|
|
// method would return nullptr as a result.
|
2011-02-28 11:52:31 -08:00
|
|
|
if (asyncThread) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool onAsyncThread;
|
2011-02-28 11:52:31 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(asyncThread->IsOnCurrentThread(&onAsyncThread)) && onAsyncThread,
|
|
|
|
"This should only be running on the async thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-03-24 00:32:40 -07:00
|
|
|
// In the AsyncStatement case we may never have populated mStatement if the
|
|
|
|
// AsyncExecuteStatements got canceled or a failure occurred in constructing
|
|
|
|
// the statement.
|
|
|
|
if (mStatement) {
|
|
|
|
(void)::sqlite3_reset(mStatement);
|
|
|
|
(void)::sqlite3_clear_bindings(mStatement);
|
2013-07-31 08:44:58 -07:00
|
|
|
mStatement = nullptr;
|
2010-03-24 00:32:40 -07:00
|
|
|
}
|
2009-06-17 12:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if this statement has parameters to be bound before it is
|
|
|
|
* executed.
|
|
|
|
*
|
2009-07-28 10:21:03 -07:00
|
|
|
* @return true if the statement has parameters to bind against, false
|
|
|
|
* otherwise.
|
2009-06-17 12:12:51 -07:00
|
|
|
*/
|
2010-05-06 11:53:12 -07:00
|
|
|
inline bool hasParametersToBeBound() const { return !!mParamsArray; }
|
2009-07-28 10:21:03 -07:00
|
|
|
/**
|
2011-05-23 15:15:01 -07:00
|
|
|
* Indicates the number of implicit statements generated by this statement
|
|
|
|
* requiring a transaction for execution. For example a single statement
|
|
|
|
* with N BindingParams will execute N implicit staments.
|
2009-07-28 10:21:03 -07:00
|
|
|
*
|
2011-05-23 15:15:01 -07:00
|
|
|
* @return number of statements requiring a transaction for execution.
|
|
|
|
*
|
|
|
|
* @note In the case of AsyncStatements this may actually create the
|
|
|
|
* statement.
|
2009-07-28 10:21:03 -07:00
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline uint32_t needsTransaction()
|
2009-07-28 10:21:03 -07:00
|
|
|
{
|
2011-05-23 15:15:01 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
// Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
|
|
|
|
// can only analyze prepared statements and AsyncStatements are prepared
|
|
|
|
// lazily.
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = getSqliteStatement(&stmt);
|
|
|
|
if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mParamsArray ? mParamsArray->length() : 1;
|
2009-07-28 10:21:03 -07:00
|
|
|
}
|
2009-06-17 12:12:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
sqlite3_stmt *mStatement;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<BindingParamsArray> mParamsArray;
|
2009-07-28 10:21:03 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We hold onto a reference of the statement's owner so it doesn't get
|
|
|
|
* destroyed out from under us.
|
|
|
|
*/
|
2010-03-24 00:32:40 -07:00
|
|
|
nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
|
2009-06-17 12:12:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-06-01 05:38:00 -07:00
|
|
|
#endif // mozStorageStatementData_h
|