2009-06-17 23:12:51 +04: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 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-06-17 23:12:51 +04:00
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#ifndef mozStorageStatementData_h
|
|
|
|
#define mozStorageStatementData_h
|
2009-06-17 23:12:51 +04:00
|
|
|
|
2009-07-28 21:21:03 +04:00
|
|
|
#include "sqlite3.h"
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
#include "nsTArray.h"
|
2013-09-19 17:54:42 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2009-06-17 23:12:51 +04:00
|
|
|
|
|
|
|
#include "mozStorageBindingParamsArray.h"
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "StorageBaseStatementInternal.h"
|
2020-08-11 23:55:33 +03:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-06-17 23:12:51 +04:00
|
|
|
|
|
|
|
struct sqlite3_stmt;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
class StatementData {
|
|
|
|
public:
|
|
|
|
StatementData(sqlite3_stmt* aStatement,
|
2009-07-28 21:21:03 +04:00
|
|
|
already_AddRefed<BindingParamsArray> aParamsArray,
|
2010-03-24 10:32:40 +03:00
|
|
|
StorageBaseStatementInternal* aStatementOwner)
|
2009-06-17 23:12:51 +04:00
|
|
|
: mStatement(aStatement),
|
|
|
|
mParamsArray(aParamsArray),
|
2020-08-11 23:55:33 +03:00
|
|
|
mQueryStatusRecorded(false),
|
2009-07-28 21:21:03 +04:00
|
|
|
mStatementOwner(aStatementOwner) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
|
|
|
StatementData(const StatementData& aSource)
|
|
|
|
: mStatement(aSource.mStatement),
|
|
|
|
mParamsArray(aSource.mParamsArray),
|
2020-08-11 23:55:33 +03:00
|
|
|
mQueryStatusRecorded(false),
|
2009-07-28 21:21:03 +04:00
|
|
|
mStatementOwner(aSource.mStatementOwner) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
2020-08-11 23:55:33 +03:00
|
|
|
StatementData() : mStatement(nullptr), mQueryStatusRecorded(false) {}
|
2013-08-28 01:07:04 +04: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.
|
2020-04-07 18:16:23 +03:00
|
|
|
NS_ReleaseOnMainThread("StatementData::mParamsArray",
|
|
|
|
mParamsArray.forget());
|
2013-08-28 01:07:04 +04:00
|
|
|
}
|
2009-06-17 23:12:51 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
/**
|
|
|
|
* Return the sqlite statement, fetching it from the storage statement. In
|
2017-06-14 04:27:17 +03:00
|
|
|
* the case of AsyncStatements this may actually create the statement
|
2010-03-24 10:32:40 +03:00
|
|
|
*/
|
|
|
|
inline int getSqliteStatement(sqlite3_stmt** _stmt) {
|
|
|
|
if (!mStatement) {
|
|
|
|
int rc = mStatementOwner->getAsyncStatement(&mStatement);
|
2020-08-11 23:55:33 +03:00
|
|
|
MaybeRecordQueryStatus(rc);
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
|
|
|
|
}
|
|
|
|
*_stmt = mStatement;
|
|
|
|
return SQLITE_OK;
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
operator BindingParamsArray*() const { return mParamsArray; }
|
|
|
|
|
|
|
|
/**
|
2009-07-28 21:21:03 +04:00
|
|
|
* NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
|
2010-03-24 10:32:40 +03:00
|
|
|
* clear all bindings to it. This is expected to occur on the async thread.
|
2009-06-17 23:12:51 +04:00
|
|
|
*/
|
2013-08-28 01:07:04 +04:00
|
|
|
inline void reset() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
|
2010-03-24 10:32:40 +03: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 19:44:58 +04:00
|
|
|
mStatement = nullptr;
|
2020-08-11 23:55:33 +03:00
|
|
|
|
|
|
|
if (!mQueryStatusRecorded) {
|
|
|
|
mStatementOwner->getOwner()->RecordQueryStatus(SQLITE_OK);
|
|
|
|
}
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
2009-06-17 23:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if this statement has parameters to be bound before it is
|
|
|
|
* executed.
|
|
|
|
*
|
2009-07-28 21:21:03 +04:00
|
|
|
* @return true if the statement has parameters to bind against, false
|
|
|
|
* otherwise.
|
2009-06-17 23:12:51 +04:00
|
|
|
*/
|
2010-05-06 22:53:12 +04:00
|
|
|
inline bool hasParametersToBeBound() const { return !!mParamsArray; }
|
2009-07-28 21:21:03 +04:00
|
|
|
/**
|
2011-05-24 02:15:01 +04: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 21:21:03 +04:00
|
|
|
*
|
2011-05-24 02:15:01 +04: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 21:21:03 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
inline uint32_t needsTransaction() {
|
2011-05-24 02:15:01 +04: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 21:21:03 +04:00
|
|
|
}
|
2009-06-17 23:12:51 +04:00
|
|
|
|
2020-08-11 23:55:33 +03:00
|
|
|
void MaybeRecordQueryStatus(int srv) {
|
|
|
|
if (mQueryStatusRecorded || !isErrorCode(srv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatementOwner->getOwner()->RecordQueryStatus(srv);
|
|
|
|
mQueryStatusRecorded = true;
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
private:
|
|
|
|
sqlite3_stmt* mStatement;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BindingParamsArray> mParamsArray;
|
2020-08-11 23:55:33 +03:00
|
|
|
bool mQueryStatusRecorded;
|
2009-07-28 21:21:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We hold onto a reference of the statement's owner so it doesn't get
|
|
|
|
* destroyed out from under us.
|
|
|
|
*/
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
|
2009-06-17 23:12:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#endif // mozStorageStatementData_h
|