Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
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 {
29 public:
30 StatementData(sqlite3_stmt *aStatement,
31 already_AddRefed<BindingParamsArray> aParamsArray,
32 StorageBaseStatementInternal *aStatementOwner)
33 : mStatement(aStatement)
34 , mParamsArray(aParamsArray)
35 , mStatementOwner(aStatementOwner)
36 {
37 NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
38 }
39 StatementData(const StatementData &aSource)
40 : mStatement(aSource.mStatement)
41 , mParamsArray(aSource.mParamsArray)
42 , mStatementOwner(aSource.mStatementOwner)
43 {
44 NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
45 }
46 StatementData()
47 {
48 }
49 ~StatementData()
50 {
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);
56 }
58 /**
59 * Return the sqlite statement, fetching it from the storage statement. In
60 * the case of AsyncStatements this may actually create the statement
61 */
62 inline int getSqliteStatement(sqlite3_stmt **_stmt)
63 {
64 if (!mStatement) {
65 int rc = mStatementOwner->getAsyncStatement(&mStatement);
66 NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
67 }
68 *_stmt = mStatement;
69 return SQLITE_OK;
70 }
72 operator BindingParamsArray *() const { return mParamsArray; }
74 /**
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.
77 */
78 inline void reset()
79 {
80 NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
81 #ifdef DEBUG
82 {
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.
87 if (asyncThread) {
88 bool onAsyncThread;
89 NS_ASSERTION(NS_SUCCEEDED(asyncThread->IsOnCurrentThread(&onAsyncThread)) && onAsyncThread,
90 "This should only be running on the async thread!");
91 }
92 }
93 #endif
94 // In the AsyncStatement case we may never have populated mStatement if the
95 // AsyncExecuteStatements got canceled or a failure occurred in constructing
96 // the statement.
97 if (mStatement) {
98 (void)::sqlite3_reset(mStatement);
99 (void)::sqlite3_clear_bindings(mStatement);
100 mStatement = nullptr;
101 }
102 }
104 /**
105 * Indicates if this statement has parameters to be bound before it is
106 * executed.
107 *
108 * @return true if the statement has parameters to bind against, false
109 * otherwise.
110 */
111 inline bool hasParametersToBeBound() const { return !!mParamsArray; }
112 /**
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.
116 *
117 * @return number of statements requiring a transaction for execution.
118 *
119 * @note In the case of AsyncStatements this may actually create the
120 * statement.
121 */
122 inline uint32_t needsTransaction()
123 {
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
127 // lazily.
128 sqlite3_stmt *stmt;
129 int rc = getSqliteStatement(&stmt);
130 if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) {
131 return 0;
132 }
133 return mParamsArray ? mParamsArray->length() : 1;
134 }
136 private:
137 sqlite3_stmt *mStatement;
138 nsRefPtr<BindingParamsArray> mParamsArray;
140 /**
141 * We hold onto a reference of the statement's owner so it doesn't get
142 * destroyed out from under us.
143 */
144 nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
145 };
147 } // namespace storage
148 } // namespace mozilla
150 #endif // mozStorageStatementData_h