Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
7 #ifndef mozStorageBindingParams_h
8 #define mozStorageBindingParams_h
10 #include "nsCOMArray.h"
11 #include "nsIVariant.h"
12 #include "nsInterfaceHashtable.h"
14 #include "mozStorageBindingParamsArray.h"
15 #include "mozStorageStatement.h"
16 #include "mozStorageAsyncStatement.h"
18 #include "mozIStorageBindingParams.h"
19 #include "IStorageBindingParamsInternal.h"
21 namespace mozilla {
22 namespace storage {
24 class BindingParams : public mozIStorageBindingParams
25 , public IStorageBindingParamsInternal
26 {
27 public:
28 NS_DECL_THREADSAFE_ISUPPORTS
29 NS_DECL_MOZISTORAGEBINDINGPARAMS
30 NS_DECL_ISTORAGEBINDINGPARAMSINTERNAL
32 /**
33 * Locks the parameters and prevents further modification to it (such as
34 * binding more elements to it).
35 */
36 void lock();
38 /**
39 * Unlocks the parameters and allows modification to it again.
40 *
41 * @param aOwningStatement
42 * The statement that owns us. We cleared this when we were locked,
43 * and our invariant requires us to have this, so you need to tell us
44 * again.
45 */
46 void unlock(Statement *aOwningStatement);
48 /**
49 * @returns the pointer to the owning BindingParamsArray. Used by a
50 * BindingParamsArray to verify that we belong to it when added.
51 */
52 const mozIStorageBindingParamsArray *getOwner() const;
54 BindingParams(mozIStorageBindingParamsArray *aOwningArray,
55 Statement *aOwningStatement);
56 virtual ~BindingParams() {}
58 protected:
59 BindingParams(mozIStorageBindingParamsArray *aOwningArray);
60 nsCOMArray<nsIVariant> mParameters;
61 bool mLocked;
63 private:
65 /**
66 * Track the BindingParamsArray that created us until we are added to it.
67 * (Once we are added we are locked and no one needs to look up our owner.)
68 * Ref-counted since there is no invariant that guarantees it stays alive
69 * otherwise. This keeps mOwningStatement alive for us too since the array
70 * also holds a reference.
71 */
72 nsCOMPtr<mozIStorageBindingParamsArray> mOwningArray;
73 /**
74 * Used in the synchronous binding case to map parameter names to indices.
75 * Not reference-counted because this is only non-null as long as mOwningArray
76 * is non-null and mOwningArray also holds a statement reference.
77 */
78 Statement *mOwningStatement;
79 uint32_t mParamCount;
80 };
82 /**
83 * Adds late resolution of named parameters so they don't get resolved until we
84 * try and bind the parameters on the async thread. We also stop checking
85 * parameter indices for being too big since we just just don't know how many
86 * there are.
87 *
88 * We support *either* binding by name or binding by index. Trying to do both
89 * results in only binding by name at sqlite3_stmt bind time.
90 */
91 class AsyncBindingParams : public BindingParams
92 {
93 public:
94 NS_IMETHOD BindByName(const nsACString & aName,
95 nsIVariant *aValue);
96 NS_IMETHOD BindByIndex(uint32_t aIndex, nsIVariant *aValue);
98 virtual already_AddRefed<mozIStorageError> bind(sqlite3_stmt * aStatement);
100 AsyncBindingParams(mozIStorageBindingParamsArray *aOwningArray);
101 virtual ~AsyncBindingParams() {}
103 private:
104 nsInterfaceHashtable<nsCStringHashKey, nsIVariant> mNamedParameters;
106 struct NamedParameterIterationClosureThunk
107 {
108 AsyncBindingParams *self;
109 sqlite3_stmt *statement;
110 nsCOMPtr<mozIStorageError> err;
111 };
113 static PLDHashOperator iterateOverNamedParameters(const nsACString &aName,
114 nsIVariant *aValue,
115 void *voidClosureThunk);
116 };
118 } // namespace storage
119 } // namespace mozilla
121 #endif // mozStorageBindingParams_h