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
12 #include "nsAutoPtr.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"
30 StatementData(sqlite3_stmt
*aStatement
,
31 already_AddRefed
<BindingParamsArray
> aParamsArray
,
32 StorageBaseStatementInternal
*aStatementOwner
)
33 : mStatement(aStatement
)
34 , mParamsArray(aParamsArray
)
35 , mStatementOwner(aStatementOwner
)
37 NS_PRECONDITION(mStatementOwner
, "Must have a statement owner!");
39 StatementData(const StatementData
&aSource
)
40 : mStatement(aSource
.mStatement
)
41 , mParamsArray(aSource
.mParamsArray
)
42 , mStatementOwner(aSource
.mStatementOwner
)
44 NS_PRECONDITION(mStatementOwner
, "Must have a statement owner!");
51 // We need to ensure that mParamsArray is released on the main thread,
52 // as the binding arguments may be XPConnect values, which are safe
53 // to release only on the main thread.
54 nsCOMPtr
<nsIThread
> mainThread
= do_GetMainThread();
55 (void)NS_ProxyRelease(mainThread
, mParamsArray
);
59 * Return the sqlite statement, fetching it from the storage statement. In
60 * the case of AsyncStatements this may actually create the statement
62 inline int getSqliteStatement(sqlite3_stmt
**_stmt
)
65 int rc
= mStatementOwner
->getAsyncStatement(&mStatement
);
66 NS_ENSURE_TRUE(rc
== SQLITE_OK
, rc
);
72 operator BindingParamsArray
*() const { return mParamsArray
; }
75 * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
76 * clear all bindings to it. This is expected to occur on the async thread.
80 NS_PRECONDITION(mStatementOwner
, "Must have a statement owner!");
83 nsCOMPtr
<nsIEventTarget
> asyncThread
=
84 mStatementOwner
->getOwner()->getAsyncExecutionTarget();
85 // It's possible that we are shutting down the async thread, and this
86 // method would return nullptr as a result.
89 NS_ASSERTION(NS_SUCCEEDED(asyncThread
->IsOnCurrentThread(&onAsyncThread
)) && onAsyncThread
,
90 "This should only be running on the async thread!");
94 // In the AsyncStatement case we may never have populated mStatement if the
95 // AsyncExecuteStatements got canceled or a failure occurred in constructing
98 (void)::sqlite3_reset(mStatement
);
99 (void)::sqlite3_clear_bindings(mStatement
);
100 mStatement
= nullptr;
105 * Indicates if this statement has parameters to be bound before it is
108 * @return true if the statement has parameters to bind against, false
111 inline bool hasParametersToBeBound() const { return !!mParamsArray
; }
113 * Indicates the number of implicit statements generated by this statement
114 * requiring a transaction for execution. For example a single statement
115 * with N BindingParams will execute N implicit staments.
117 * @return number of statements requiring a transaction for execution.
119 * @note In the case of AsyncStatements this may actually create the
122 inline uint32_t needsTransaction()
124 MOZ_ASSERT(!NS_IsMainThread());
125 // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
126 // can only analyze prepared statements and AsyncStatements are prepared
129 int rc
= getSqliteStatement(&stmt
);
130 if (SQLITE_OK
!= rc
|| ::sqlite3_stmt_readonly(stmt
)) {
133 return mParamsArray
? mParamsArray
->length() : 1;
137 sqlite3_stmt
*mStatement
;
138 nsRefPtr
<BindingParamsArray
> mParamsArray
;
141 * We hold onto a reference of the statement's owner so it doesn't get
142 * destroyed out from under us.
144 nsCOMPtr
<StorageBaseStatementInternal
> mStatementOwner
;
147 } // namespace storage
148 } // namespace mozilla
150 #endif // mozStorageStatementData_h