1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/storage/src/mozStorageStatementData.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 sts=2 et 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozStorageStatementData_h 1.11 +#define mozStorageStatementData_h 1.12 + 1.13 +#include "sqlite3.h" 1.14 + 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsTArray.h" 1.17 +#include "nsIEventTarget.h" 1.18 +#include "MainThreadUtils.h" 1.19 + 1.20 +#include "mozStorageBindingParamsArray.h" 1.21 +#include "mozIStorageBaseStatement.h" 1.22 +#include "mozStorageConnection.h" 1.23 +#include "StorageBaseStatementInternal.h" 1.24 + 1.25 +struct sqlite3_stmt; 1.26 + 1.27 +namespace mozilla { 1.28 +namespace storage { 1.29 + 1.30 +class StatementData 1.31 +{ 1.32 +public: 1.33 + StatementData(sqlite3_stmt *aStatement, 1.34 + already_AddRefed<BindingParamsArray> aParamsArray, 1.35 + StorageBaseStatementInternal *aStatementOwner) 1.36 + : mStatement(aStatement) 1.37 + , mParamsArray(aParamsArray) 1.38 + , mStatementOwner(aStatementOwner) 1.39 + { 1.40 + NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); 1.41 + } 1.42 + StatementData(const StatementData &aSource) 1.43 + : mStatement(aSource.mStatement) 1.44 + , mParamsArray(aSource.mParamsArray) 1.45 + , mStatementOwner(aSource.mStatementOwner) 1.46 + { 1.47 + NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); 1.48 + } 1.49 + StatementData() 1.50 + { 1.51 + } 1.52 + ~StatementData() 1.53 + { 1.54 + // We need to ensure that mParamsArray is released on the main thread, 1.55 + // as the binding arguments may be XPConnect values, which are safe 1.56 + // to release only on the main thread. 1.57 + nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); 1.58 + (void)NS_ProxyRelease(mainThread, mParamsArray); 1.59 + } 1.60 + 1.61 + /** 1.62 + * Return the sqlite statement, fetching it from the storage statement. In 1.63 + * the case of AsyncStatements this may actually create the statement 1.64 + */ 1.65 + inline int getSqliteStatement(sqlite3_stmt **_stmt) 1.66 + { 1.67 + if (!mStatement) { 1.68 + int rc = mStatementOwner->getAsyncStatement(&mStatement); 1.69 + NS_ENSURE_TRUE(rc == SQLITE_OK, rc); 1.70 + } 1.71 + *_stmt = mStatement; 1.72 + return SQLITE_OK; 1.73 + } 1.74 + 1.75 + operator BindingParamsArray *() const { return mParamsArray; } 1.76 + 1.77 + /** 1.78 + * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and 1.79 + * clear all bindings to it. This is expected to occur on the async thread. 1.80 + */ 1.81 + inline void reset() 1.82 + { 1.83 + NS_PRECONDITION(mStatementOwner, "Must have a statement owner!"); 1.84 +#ifdef DEBUG 1.85 + { 1.86 + nsCOMPtr<nsIEventTarget> asyncThread = 1.87 + mStatementOwner->getOwner()->getAsyncExecutionTarget(); 1.88 + // It's possible that we are shutting down the async thread, and this 1.89 + // method would return nullptr as a result. 1.90 + if (asyncThread) { 1.91 + bool onAsyncThread; 1.92 + NS_ASSERTION(NS_SUCCEEDED(asyncThread->IsOnCurrentThread(&onAsyncThread)) && onAsyncThread, 1.93 + "This should only be running on the async thread!"); 1.94 + } 1.95 + } 1.96 +#endif 1.97 + // In the AsyncStatement case we may never have populated mStatement if the 1.98 + // AsyncExecuteStatements got canceled or a failure occurred in constructing 1.99 + // the statement. 1.100 + if (mStatement) { 1.101 + (void)::sqlite3_reset(mStatement); 1.102 + (void)::sqlite3_clear_bindings(mStatement); 1.103 + mStatement = nullptr; 1.104 + } 1.105 + } 1.106 + 1.107 + /** 1.108 + * Indicates if this statement has parameters to be bound before it is 1.109 + * executed. 1.110 + * 1.111 + * @return true if the statement has parameters to bind against, false 1.112 + * otherwise. 1.113 + */ 1.114 + inline bool hasParametersToBeBound() const { return !!mParamsArray; } 1.115 + /** 1.116 + * Indicates the number of implicit statements generated by this statement 1.117 + * requiring a transaction for execution. For example a single statement 1.118 + * with N BindingParams will execute N implicit staments. 1.119 + * 1.120 + * @return number of statements requiring a transaction for execution. 1.121 + * 1.122 + * @note In the case of AsyncStatements this may actually create the 1.123 + * statement. 1.124 + */ 1.125 + inline uint32_t needsTransaction() 1.126 + { 1.127 + MOZ_ASSERT(!NS_IsMainThread()); 1.128 + // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly 1.129 + // can only analyze prepared statements and AsyncStatements are prepared 1.130 + // lazily. 1.131 + sqlite3_stmt *stmt; 1.132 + int rc = getSqliteStatement(&stmt); 1.133 + if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) { 1.134 + return 0; 1.135 + } 1.136 + return mParamsArray ? mParamsArray->length() : 1; 1.137 + } 1.138 + 1.139 +private: 1.140 + sqlite3_stmt *mStatement; 1.141 + nsRefPtr<BindingParamsArray> mParamsArray; 1.142 + 1.143 + /** 1.144 + * We hold onto a reference of the statement's owner so it doesn't get 1.145 + * destroyed out from under us. 1.146 + */ 1.147 + nsCOMPtr<StorageBaseStatementInternal> mStatementOwner; 1.148 +}; 1.149 + 1.150 +} // namespace storage 1.151 +} // namespace mozilla 1.152 + 1.153 +#endif // mozStorageStatementData_h