1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 et
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozStorageStatementData_h
8 #define mozStorageStatementData_h
14 #include "mozStorageBindingParamsArray.h"
15 #include "mozStorageConnection.h"
16 #include "StorageBaseStatementInternal.h"
17 #include "mozStoragePrivateHelpers.h"
21 namespace mozilla::storage
{
25 StatementData(sqlite3_stmt
* aStatement
,
26 already_AddRefed
<BindingParamsArray
> aParamsArray
,
27 StorageBaseStatementInternal
* aStatementOwner
)
28 : mStatement(aStatement
),
29 mParamsArray(aParamsArray
),
30 mQueryStatusRecorded(false),
31 mStatementOwner(aStatementOwner
) {
32 MOZ_ASSERT(mStatementOwner
, "Must have a statement owner!");
34 StatementData(const StatementData
& aSource
)
35 : mStatement(aSource
.mStatement
),
36 mParamsArray(aSource
.mParamsArray
),
37 mQueryStatusRecorded(false),
38 mStatementOwner(aSource
.mStatementOwner
) {
39 MOZ_ASSERT(mStatementOwner
, "Must have a statement owner!");
41 StatementData() : mStatement(nullptr), mQueryStatusRecorded(false) {}
44 * Return the sqlite statement, fetching it from the storage statement. In
45 * the case of AsyncStatements this may actually create the statement
47 inline int getSqliteStatement(sqlite3_stmt
** _stmt
) {
49 int rc
= mStatementOwner
->getAsyncStatement(&mStatement
);
50 MaybeRecordQueryStatus(rc
);
51 NS_ENSURE_TRUE(rc
== SQLITE_OK
, rc
);
57 operator BindingParamsArray
*() const { return mParamsArray
; }
60 * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
61 * clear all bindings to it. This is expected to occur on the async thread.
64 MOZ_ASSERT(mStatementOwner
, "Must have a statement owner!");
65 // In the AsyncStatement case we may never have populated mStatement if the
66 // AsyncExecuteStatements got canceled or a failure occurred in constructing
69 (void)::sqlite3_reset(mStatement
);
70 (void)::sqlite3_clear_bindings(mStatement
);
73 if (!mQueryStatusRecorded
) {
74 mStatementOwner
->getOwner()->RecordQueryStatus(SQLITE_OK
);
80 * Indicates if this statement has parameters to be bound before it is
83 * @return true if the statement has parameters to bind against, false
86 inline bool hasParametersToBeBound() const { return !!mParamsArray
; }
88 * Indicates the number of implicit statements generated by this statement
89 * requiring a transaction for execution. For example a single statement
90 * with N BindingParams will execute N implicit staments.
92 * @return number of statements requiring a transaction for execution.
94 * @note In the case of AsyncStatements this may actually create the
97 inline uint32_t needsTransaction() {
98 MOZ_ASSERT(!NS_IsMainThread());
99 // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
100 // can only analyze prepared statements and AsyncStatements are prepared
103 int rc
= getSqliteStatement(&stmt
);
104 if (SQLITE_OK
!= rc
|| ::sqlite3_stmt_readonly(stmt
)) {
107 return mParamsArray
? mParamsArray
->length() : 1;
110 void MaybeRecordQueryStatus(int srv
) {
111 if (mQueryStatusRecorded
|| !isErrorCode(srv
)) {
115 mStatementOwner
->getOwner()->RecordQueryStatus(srv
);
116 mQueryStatusRecorded
= true;
120 sqlite3_stmt
* mStatement
;
123 * It's safe to release this on any thread, as it holds `BindingParams` that
124 * can only contain thread-safe derivates of `Variant_base`.
126 RefPtr
<BindingParamsArray
> mParamsArray
;
128 bool mQueryStatusRecorded
;
131 * We hold onto a reference of the statement's owner so it doesn't get
132 * destroyed out from under us.
134 nsCOMPtr
<StorageBaseStatementInternal
> mStatementOwner
;
137 } // namespace mozilla::storage
139 #endif // mozStorageStatementData_h