Bug 1875761 - disable release channel test in browser-engine-gecko component. r=gecko...
[gecko.git] / storage / mozStorageStatementData.h
blob1008496671c19401ce41e729c118902167339218
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
10 #include "sqlite3.h"
12 #include "nsTArray.h"
13 #include "MainThreadUtils.h"
15 #include "mozStorageBindingParamsArray.h"
16 #include "mozStorageConnection.h"
17 #include "StorageBaseStatementInternal.h"
18 #include "mozStoragePrivateHelpers.h"
20 struct sqlite3_stmt;
22 namespace mozilla {
23 namespace storage {
25 class StatementData {
26 public:
27 StatementData(sqlite3_stmt* aStatement,
28 already_AddRefed<BindingParamsArray> aParamsArray,
29 StorageBaseStatementInternal* aStatementOwner)
30 : mStatement(aStatement),
31 mParamsArray(aParamsArray),
32 mQueryStatusRecorded(false),
33 mStatementOwner(aStatementOwner) {
34 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
36 StatementData(const StatementData& aSource)
37 : mStatement(aSource.mStatement),
38 mParamsArray(aSource.mParamsArray),
39 mQueryStatusRecorded(false),
40 mStatementOwner(aSource.mStatementOwner) {
41 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
43 StatementData() : mStatement(nullptr), mQueryStatusRecorded(false) {}
44 ~StatementData() {
45 // We need to ensure that mParamsArray is released on the main thread,
46 // as the binding arguments may be XPConnect values, which are safe
47 // to release only on the main thread.
48 NS_ReleaseOnMainThread("StatementData::mParamsArray",
49 mParamsArray.forget());
52 /**
53 * Return the sqlite statement, fetching it from the storage statement. In
54 * the case of AsyncStatements this may actually create the statement
56 inline int getSqliteStatement(sqlite3_stmt** _stmt) {
57 if (!mStatement) {
58 int rc = mStatementOwner->getAsyncStatement(&mStatement);
59 MaybeRecordQueryStatus(rc);
60 NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
62 *_stmt = mStatement;
63 return SQLITE_OK;
66 operator BindingParamsArray*() const { return mParamsArray; }
68 /**
69 * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
70 * clear all bindings to it. This is expected to occur on the async thread.
72 inline void reset() {
73 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
74 // In the AsyncStatement case we may never have populated mStatement if the
75 // AsyncExecuteStatements got canceled or a failure occurred in constructing
76 // the statement.
77 if (mStatement) {
78 (void)::sqlite3_reset(mStatement);
79 (void)::sqlite3_clear_bindings(mStatement);
80 mStatement = nullptr;
82 if (!mQueryStatusRecorded) {
83 mStatementOwner->getOwner()->RecordQueryStatus(SQLITE_OK);
88 /**
89 * Indicates if this statement has parameters to be bound before it is
90 * executed.
92 * @return true if the statement has parameters to bind against, false
93 * otherwise.
95 inline bool hasParametersToBeBound() const { return !!mParamsArray; }
96 /**
97 * Indicates the number of implicit statements generated by this statement
98 * requiring a transaction for execution. For example a single statement
99 * with N BindingParams will execute N implicit staments.
101 * @return number of statements requiring a transaction for execution.
103 * @note In the case of AsyncStatements this may actually create the
104 * statement.
106 inline uint32_t needsTransaction() {
107 MOZ_ASSERT(!NS_IsMainThread());
108 // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
109 // can only analyze prepared statements and AsyncStatements are prepared
110 // lazily.
111 sqlite3_stmt* stmt;
112 int rc = getSqliteStatement(&stmt);
113 if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) {
114 return 0;
116 return mParamsArray ? mParamsArray->length() : 1;
119 void MaybeRecordQueryStatus(int srv) {
120 if (mQueryStatusRecorded || !isErrorCode(srv)) {
121 return;
124 mStatementOwner->getOwner()->RecordQueryStatus(srv);
125 mQueryStatusRecorded = true;
128 private:
129 sqlite3_stmt* mStatement;
130 RefPtr<BindingParamsArray> mParamsArray;
131 bool mQueryStatusRecorded;
134 * We hold onto a reference of the statement's owner so it doesn't get
135 * destroyed out from under us.
137 nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
140 } // namespace storage
141 } // namespace mozilla
143 #endif // mozStorageStatementData_h