Bug 1509459 - Get the flexbox highlighter state if the highlighter is ready in the...
[gecko.git] / storage / mozStorageStatementData.h
blobc03d1423bb6bc3fcebbe2a482540f0af7e02e16a
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 "nsAutoPtr.h"
13 #include "nsTArray.h"
14 #include "nsIEventTarget.h"
15 #include "MainThreadUtils.h"
17 #include "mozStorageBindingParamsArray.h"
18 #include "mozIStorageBaseStatement.h"
19 #include "mozStorageConnection.h"
20 #include "StorageBaseStatementInternal.h"
22 struct sqlite3_stmt;
24 namespace mozilla {
25 namespace storage {
27 class StatementData {
28 public:
29 StatementData(sqlite3_stmt *aStatement,
30 already_AddRefed<BindingParamsArray> aParamsArray,
31 StorageBaseStatementInternal *aStatementOwner)
32 : mStatement(aStatement),
33 mParamsArray(aParamsArray),
34 mStatementOwner(aStatementOwner) {
35 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
37 StatementData(const StatementData &aSource)
38 : mStatement(aSource.mStatement),
39 mParamsArray(aSource.mParamsArray),
40 mStatementOwner(aSource.mStatementOwner) {
41 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
43 StatementData() : mStatement(nullptr) {}
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_ReleaseOnMainThreadSystemGroup("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 NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
61 *_stmt = mStatement;
62 return SQLITE_OK;
65 operator BindingParamsArray *() const { return mParamsArray; }
67 /**
68 * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
69 * clear all bindings to it. This is expected to occur on the async thread.
71 inline void reset() {
72 MOZ_ASSERT(mStatementOwner, "Must have a statement owner!");
73 // In the AsyncStatement case we may never have populated mStatement if the
74 // AsyncExecuteStatements got canceled or a failure occurred in constructing
75 // the statement.
76 if (mStatement) {
77 (void)::sqlite3_reset(mStatement);
78 (void)::sqlite3_clear_bindings(mStatement);
79 mStatement = nullptr;
83 /**
84 * Indicates if this statement has parameters to be bound before it is
85 * executed.
87 * @return true if the statement has parameters to bind against, false
88 * otherwise.
90 inline bool hasParametersToBeBound() const { return !!mParamsArray; }
91 /**
92 * Indicates the number of implicit statements generated by this statement
93 * requiring a transaction for execution. For example a single statement
94 * with N BindingParams will execute N implicit staments.
96 * @return number of statements requiring a transaction for execution.
98 * @note In the case of AsyncStatements this may actually create the
99 * statement.
101 inline uint32_t needsTransaction() {
102 MOZ_ASSERT(!NS_IsMainThread());
103 // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
104 // can only analyze prepared statements and AsyncStatements are prepared
105 // lazily.
106 sqlite3_stmt *stmt;
107 int rc = getSqliteStatement(&stmt);
108 if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) {
109 return 0;
111 return mParamsArray ? mParamsArray->length() : 1;
114 private:
115 sqlite3_stmt *mStatement;
116 RefPtr<BindingParamsArray> mParamsArray;
119 * We hold onto a reference of the statement's owner so it doesn't get
120 * destroyed out from under us.
122 nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
125 } // namespace storage
126 } // namespace mozilla
128 #endif // mozStorageStatementData_h