mozStorageBindingParams.h (3936B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 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/. */ 6 7 #ifndef mozStorageBindingParams_h 8 #define mozStorageBindingParams_h 9 10 #include "nsIVariant.h" 11 12 #include "mozStorageStatement.h" 13 #include "mozStorageAsyncStatement.h" 14 #include "Variant.h" 15 #include "nsTHashMap.h" 16 #include "mozIStorageBindingParams.h" 17 #include "IStorageBindingParamsInternal.h" 18 19 namespace mozilla::storage { 20 21 class BindingParams : public mozIStorageBindingParams, 22 public IStorageBindingParamsInternal { 23 public: 24 NS_DECL_THREADSAFE_ISUPPORTS 25 NS_DECL_MOZISTORAGEBINDINGPARAMS 26 NS_DECL_ISTORAGEBINDINGPARAMSINTERNAL 27 28 /** 29 * Locks the parameters and prevents further modification to it (such as 30 * binding more elements to it). 31 */ 32 void lock(); 33 34 /** 35 * Unlocks the parameters and allows modification to it again. 36 * 37 * @param aOwningStatement 38 * The statement that owns us. We cleared this when we were locked, 39 * and our invariant requires us to have this, so you need to tell us 40 * again. 41 */ 42 void unlock(Statement* aOwningStatement); 43 44 /** 45 * @returns the pointer to the owning BindingParamsArray. Used by a 46 * BindingParamsArray to verify that we belong to it when added. 47 */ 48 const mozIStorageBindingParamsArray* getOwner() const; 49 50 BindingParams(mozIStorageBindingParamsArray* aOwningArray, 51 Statement* aOwningStatement); 52 53 protected: 54 virtual ~BindingParams() {} 55 56 explicit BindingParams(mozIStorageBindingParamsArray* aOwningArray); 57 58 // Note that this is managed as a sparse array, so particular caution should 59 // be used for out-of-bounds usage. 60 // Variants added to this array must be thread-safe, thus you should not use 61 // XPCVariant or similar main-thread only implementations. 62 nsTArray<RefPtr<Variant_base>> mParameters; 63 64 bool mLocked; 65 66 private: 67 /** 68 * Track the BindingParamsArray that created us until we are added to it. 69 * (Once we are added we are locked and no one needs to look up our owner.) 70 * Ref-counted since there is no invariant that guarantees it stays alive 71 * otherwise. This keeps mOwningStatement alive for us too since the array 72 * also holds a reference. 73 */ 74 nsCOMPtr<mozIStorageBindingParamsArray> mOwningArray; 75 /** 76 * Used in the synchronous binding case to map parameter names to indices. 77 * Not reference-counted because this is only non-null as long as mOwningArray 78 * is non-null and mOwningArray also holds a statement reference. 79 */ 80 Statement* mOwningStatement; 81 uint32_t mParamCount; 82 }; 83 84 /** 85 * Adds late resolution of named parameters so they don't get resolved until we 86 * try and bind the parameters on the async thread. We also stop checking 87 * parameter indices for being too big since we just just don't know how many 88 * there are. 89 * 90 * We support *either* binding by name or binding by index. Trying to do both 91 * results in only binding by name at sqlite3_stmt bind time. 92 */ 93 class AsyncBindingParams : public BindingParams { 94 public: 95 NS_IMETHOD BindByName(const nsACString& aName, nsIVariant* aValue) override; 96 NS_IMETHOD BindByIndex(uint32_t aIndex, nsIVariant* aValue) override; 97 98 virtual already_AddRefed<mozIStorageError> bind( 99 sqlite3_stmt* aStatement) override; 100 101 explicit AsyncBindingParams(mozIStorageBindingParamsArray* aOwningArray); 102 virtual ~AsyncBindingParams() {} 103 104 private: 105 // Variants added to this array must be thread-safe, thus you should not use 106 // XPCVariant or similar main-thread only implementations. 107 nsTHashMap<nsCStringHashKey, RefPtr<Variant_base>> mNamedParameters; 108 }; 109 110 } // namespace mozilla::storage 111 112 #endif // mozStorageBindingParams_h