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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : * Andrew Sutherland <asutherland@asutherland.org>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef mozStorageBindingParams_h
42 : #define mozStorageBindingParams_h
43 :
44 : #include "nsCOMArray.h"
45 : #include "nsIVariant.h"
46 : #include "nsInterfaceHashtable.h"
47 :
48 : #include "mozStorageBindingParamsArray.h"
49 : #include "mozStorageStatement.h"
50 : #include "mozStorageAsyncStatement.h"
51 :
52 : #include "mozIStorageBindingParams.h"
53 : #include "IStorageBindingParamsInternal.h"
54 :
55 : namespace mozilla {
56 : namespace storage {
57 :
58 : class BindingParams : public mozIStorageBindingParams
59 : , public IStorageBindingParamsInternal
60 : {
61 : public:
62 : NS_DECL_ISUPPORTS
63 : NS_DECL_MOZISTORAGEBINDINGPARAMS
64 : NS_DECL_ISTORAGEBINDINGPARAMSINTERNAL
65 :
66 : /**
67 : * Locks the parameters and prevents further modification to it (such as
68 : * binding more elements to it).
69 : */
70 : void lock();
71 :
72 : /**
73 : * Unlocks the parameters and allows modification to it again.
74 : *
75 : * @param aOwningStatement
76 : * The statement that owns us. We cleared this when we were locked,
77 : * and our invariant requires us to have this, so you need to tell us
78 : * again.
79 : */
80 : void unlock(Statement *aOwningStatement);
81 :
82 : /**
83 : * @returns the pointer to the owning BindingParamsArray. Used by a
84 : * BindingParamsArray to verify that we belong to it when added.
85 : */
86 : const mozIStorageBindingParamsArray *getOwner() const;
87 :
88 : BindingParams(mozIStorageBindingParamsArray *aOwningArray,
89 : Statement *aOwningStatement);
90 421280 : virtual ~BindingParams() {}
91 :
92 : protected:
93 : BindingParams(mozIStorageBindingParamsArray *aOwningArray);
94 : nsCOMArray<nsIVariant> mParameters;
95 : bool mLocked;
96 :
97 : private:
98 :
99 : /**
100 : * Track the BindingParamsArray that created us until we are added to it.
101 : * (Once we are added we are locked and no one needs to look up our owner.)
102 : * Ref-counted since there is no invariant that guarantees it stays alive
103 : * otherwise. This keeps mOwningStatement alive for us too since the array
104 : * also holds a reference.
105 : */
106 : nsCOMPtr<mozIStorageBindingParamsArray> mOwningArray;
107 : /**
108 : * Used in the synchronous binding case to map parameter names to indices.
109 : * Not reference-counted because this is only non-null as long as mOwningArray
110 : * is non-null and mOwningArray also holds a statement reference.
111 : */
112 : Statement *mOwningStatement;
113 : PRUint32 mParamCount;
114 : };
115 :
116 : /**
117 : * Adds late resolution of named parameters so they don't get resolved until we
118 : * try and bind the parameters on the async thread. We also stop checking
119 : * parameter indices for being too big since we just just don't know how many
120 : * there are.
121 : *
122 : * We support *either* binding by name or binding by index. Trying to do both
123 : * results in only binding by name at sqlite3_stmt bind time.
124 : */
125 : class AsyncBindingParams : public BindingParams
126 : {
127 : public:
128 : NS_SCRIPTABLE NS_IMETHOD BindByName(const nsACString & aName,
129 : nsIVariant *aValue);
130 : NS_SCRIPTABLE NS_IMETHOD BindByIndex(PRUint32 aIndex, nsIVariant *aValue);
131 :
132 : virtual already_AddRefed<mozIStorageError> bind(sqlite3_stmt * aStatement);
133 :
134 : AsyncBindingParams(mozIStorageBindingParamsArray *aOwningArray);
135 132776 : virtual ~AsyncBindingParams() {}
136 :
137 : private:
138 : nsInterfaceHashtable<nsCStringHashKey, nsIVariant> mNamedParameters;
139 :
140 : struct NamedParameterIterationClosureThunk
141 32827 : {
142 : AsyncBindingParams *self;
143 : sqlite3_stmt *statement;
144 : nsCOMPtr<mozIStorageError> err;
145 : };
146 :
147 : static PLDHashOperator iterateOverNamedParameters(const nsACString &aName,
148 : nsIVariant *aValue,
149 : void *voidClosureThunk);
150 : };
151 :
152 : } // namespace storage
153 : } // namespace mozilla
154 :
155 : #endif // mozStorageBindingParams_h
|