Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 sts=2 et |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozStorageStatementData_h |
michael@0 | 8 | #define mozStorageStatementData_h |
michael@0 | 9 | |
michael@0 | 10 | #include "sqlite3.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "nsTArray.h" |
michael@0 | 14 | #include "nsIEventTarget.h" |
michael@0 | 15 | #include "MainThreadUtils.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "mozStorageBindingParamsArray.h" |
michael@0 | 18 | #include "mozIStorageBaseStatement.h" |
michael@0 | 19 | #include "mozStorageConnection.h" |
michael@0 | 20 | #include "StorageBaseStatementInternal.h" |
michael@0 | 21 | |
michael@0 | 22 | struct sqlite3_stmt; |
michael@0 | 23 | |
michael@0 | 24 | namespace mozilla { |
michael@0 | 25 | namespace storage { |
michael@0 | 26 | |
michael@0 | 27 | class StatementData |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | StatementData(sqlite3_stmt *aStatement, |
michael@0 | 31 | already_AddRefed<BindingParamsArray> aParamsArray, |
michael@0 | 32 | StorageBaseStatementInternal *aStatementOwner) |
michael@0 | 33 | : mStatement(aStatement) |
michael@0 | 34 | , mParamsArray(aParamsArray) |
michael@0 | 35 | , mStatementOwner(aStatementOwner) |
michael@0 | 36 | { |
michael@0 | 37 | NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); |
michael@0 | 38 | } |
michael@0 | 39 | StatementData(const StatementData &aSource) |
michael@0 | 40 | : mStatement(aSource.mStatement) |
michael@0 | 41 | , mParamsArray(aSource.mParamsArray) |
michael@0 | 42 | , mStatementOwner(aSource.mStatementOwner) |
michael@0 | 43 | { |
michael@0 | 44 | NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); |
michael@0 | 45 | } |
michael@0 | 46 | StatementData() |
michael@0 | 47 | { |
michael@0 | 48 | } |
michael@0 | 49 | ~StatementData() |
michael@0 | 50 | { |
michael@0 | 51 | // We need to ensure that mParamsArray is released on the main thread, |
michael@0 | 52 | // as the binding arguments may be XPConnect values, which are safe |
michael@0 | 53 | // to release only on the main thread. |
michael@0 | 54 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 55 | (void)NS_ProxyRelease(mainThread, mParamsArray); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Return the sqlite statement, fetching it from the storage statement. In |
michael@0 | 60 | * the case of AsyncStatements this may actually create the statement |
michael@0 | 61 | */ |
michael@0 | 62 | inline int getSqliteStatement(sqlite3_stmt **_stmt) |
michael@0 | 63 | { |
michael@0 | 64 | if (!mStatement) { |
michael@0 | 65 | int rc = mStatementOwner->getAsyncStatement(&mStatement); |
michael@0 | 66 | NS_ENSURE_TRUE(rc == SQLITE_OK, rc); |
michael@0 | 67 | } |
michael@0 | 68 | *_stmt = mStatement; |
michael@0 | 69 | return SQLITE_OK; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | operator BindingParamsArray *() const { return mParamsArray; } |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and |
michael@0 | 76 | * clear all bindings to it. This is expected to occur on the async thread. |
michael@0 | 77 | */ |
michael@0 | 78 | inline void reset() |
michael@0 | 79 | { |
michael@0 | 80 | NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); |
michael@0 | 81 | #ifdef DEBUG |
michael@0 | 82 | { |
michael@0 | 83 | nsCOMPtr<nsIEventTarget> asyncThread = |
michael@0 | 84 | mStatementOwner->getOwner()->getAsyncExecutionTarget(); |
michael@0 | 85 | // It's possible that we are shutting down the async thread, and this |
michael@0 | 86 | // method would return nullptr as a result. |
michael@0 | 87 | if (asyncThread) { |
michael@0 | 88 | bool onAsyncThread; |
michael@0 | 89 | NS_ASSERTION(NS_SUCCEEDED(asyncThread->IsOnCurrentThread(&onAsyncThread)) && onAsyncThread, |
michael@0 | 90 | "This should only be running on the async thread!"); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | #endif |
michael@0 | 94 | // In the AsyncStatement case we may never have populated mStatement if the |
michael@0 | 95 | // AsyncExecuteStatements got canceled or a failure occurred in constructing |
michael@0 | 96 | // the statement. |
michael@0 | 97 | if (mStatement) { |
michael@0 | 98 | (void)::sqlite3_reset(mStatement); |
michael@0 | 99 | (void)::sqlite3_clear_bindings(mStatement); |
michael@0 | 100 | mStatement = nullptr; |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Indicates if this statement has parameters to be bound before it is |
michael@0 | 106 | * executed. |
michael@0 | 107 | * |
michael@0 | 108 | * @return true if the statement has parameters to bind against, false |
michael@0 | 109 | * otherwise. |
michael@0 | 110 | */ |
michael@0 | 111 | inline bool hasParametersToBeBound() const { return !!mParamsArray; } |
michael@0 | 112 | /** |
michael@0 | 113 | * Indicates the number of implicit statements generated by this statement |
michael@0 | 114 | * requiring a transaction for execution. For example a single statement |
michael@0 | 115 | * with N BindingParams will execute N implicit staments. |
michael@0 | 116 | * |
michael@0 | 117 | * @return number of statements requiring a transaction for execution. |
michael@0 | 118 | * |
michael@0 | 119 | * @note In the case of AsyncStatements this may actually create the |
michael@0 | 120 | * statement. |
michael@0 | 121 | */ |
michael@0 | 122 | inline uint32_t needsTransaction() |
michael@0 | 123 | { |
michael@0 | 124 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 125 | // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly |
michael@0 | 126 | // can only analyze prepared statements and AsyncStatements are prepared |
michael@0 | 127 | // lazily. |
michael@0 | 128 | sqlite3_stmt *stmt; |
michael@0 | 129 | int rc = getSqliteStatement(&stmt); |
michael@0 | 130 | if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) { |
michael@0 | 131 | return 0; |
michael@0 | 132 | } |
michael@0 | 133 | return mParamsArray ? mParamsArray->length() : 1; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | private: |
michael@0 | 137 | sqlite3_stmt *mStatement; |
michael@0 | 138 | nsRefPtr<BindingParamsArray> mParamsArray; |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * We hold onto a reference of the statement's owner so it doesn't get |
michael@0 | 142 | * destroyed out from under us. |
michael@0 | 143 | */ |
michael@0 | 144 | nsCOMPtr<StorageBaseStatementInternal> mStatementOwner; |
michael@0 | 145 | }; |
michael@0 | 146 | |
michael@0 | 147 | } // namespace storage |
michael@0 | 148 | } // namespace mozilla |
michael@0 | 149 | |
michael@0 | 150 | #endif // mozStorageStatementData_h |