|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
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 mozilla_dom_indexeddb_idbkeyrange_h__ |
|
8 #define mozilla_dom_indexeddb_idbkeyrange_h__ |
|
9 |
|
10 #include "mozilla/dom/indexedDB/IndexedDatabase.h" |
|
11 #include "mozilla/dom/indexedDB/Key.h" |
|
12 |
|
13 #include "nsISupports.h" |
|
14 |
|
15 #include "mozilla/ErrorResult.h" |
|
16 #include "nsCycleCollectionParticipant.h" |
|
17 |
|
18 class mozIStorageStatement; |
|
19 |
|
20 namespace mozilla { |
|
21 namespace dom { |
|
22 class GlobalObject; |
|
23 } // namespace dom |
|
24 } // namespace mozilla |
|
25 |
|
26 BEGIN_INDEXEDDB_NAMESPACE |
|
27 |
|
28 namespace ipc { |
|
29 class KeyRange; |
|
30 } // namespace ipc |
|
31 |
|
32 class IDBKeyRange MOZ_FINAL : public nsISupports |
|
33 { |
|
34 public: |
|
35 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
36 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBKeyRange) |
|
37 |
|
38 static nsresult FromJSVal(JSContext* aCx, |
|
39 JS::Handle<JS::Value> aVal, |
|
40 IDBKeyRange** aKeyRange); |
|
41 |
|
42 template <class T> |
|
43 static already_AddRefed<IDBKeyRange> |
|
44 FromSerializedKeyRange(const T& aKeyRange); |
|
45 |
|
46 const Key& Lower() const |
|
47 { |
|
48 return mLower; |
|
49 } |
|
50 |
|
51 Key& Lower() |
|
52 { |
|
53 return mLower; |
|
54 } |
|
55 |
|
56 const Key& Upper() const |
|
57 { |
|
58 return mIsOnly ? mLower : mUpper; |
|
59 } |
|
60 |
|
61 Key& Upper() |
|
62 { |
|
63 return mIsOnly ? mLower : mUpper; |
|
64 } |
|
65 |
|
66 // TODO: Remove these in favour of LowerOpen() / UpperOpen(), bug 900578. |
|
67 bool IsLowerOpen() const |
|
68 { |
|
69 return mLowerOpen; |
|
70 } |
|
71 |
|
72 bool IsUpperOpen() const |
|
73 { |
|
74 return mUpperOpen; |
|
75 } |
|
76 |
|
77 bool IsOnly() const |
|
78 { |
|
79 return mIsOnly; |
|
80 } |
|
81 |
|
82 void GetBindingClause(const nsACString& aKeyColumnName, |
|
83 nsACString& _retval) const |
|
84 { |
|
85 NS_NAMED_LITERAL_CSTRING(andStr, " AND "); |
|
86 NS_NAMED_LITERAL_CSTRING(spacecolon, " :"); |
|
87 NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key"); |
|
88 |
|
89 if (IsOnly()) { |
|
90 // Both keys are set and they're equal. |
|
91 _retval = andStr + aKeyColumnName + NS_LITERAL_CSTRING(" =") + |
|
92 spacecolon + lowerKey; |
|
93 } |
|
94 else { |
|
95 nsAutoCString clause; |
|
96 |
|
97 if (!Lower().IsUnset()) { |
|
98 // Lower key is set. |
|
99 clause.Append(andStr + aKeyColumnName); |
|
100 clause.AppendLiteral(" >"); |
|
101 if (!IsLowerOpen()) { |
|
102 clause.AppendLiteral("="); |
|
103 } |
|
104 clause.Append(spacecolon + lowerKey); |
|
105 } |
|
106 |
|
107 if (!Upper().IsUnset()) { |
|
108 // Upper key is set. |
|
109 clause.Append(andStr + aKeyColumnName); |
|
110 clause.AppendLiteral(" <"); |
|
111 if (!IsUpperOpen()) { |
|
112 clause.AppendLiteral("="); |
|
113 } |
|
114 clause.Append(spacecolon + NS_LITERAL_CSTRING("upper_key")); |
|
115 } |
|
116 |
|
117 _retval = clause; |
|
118 } |
|
119 } |
|
120 |
|
121 nsresult BindToStatement(mozIStorageStatement* aStatement) const |
|
122 { |
|
123 NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key"); |
|
124 |
|
125 if (IsOnly()) { |
|
126 return Lower().BindToStatement(aStatement, lowerKey); |
|
127 } |
|
128 |
|
129 nsresult rv; |
|
130 |
|
131 if (!Lower().IsUnset()) { |
|
132 rv = Lower().BindToStatement(aStatement, lowerKey); |
|
133 NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
|
134 } |
|
135 |
|
136 if (!Upper().IsUnset()) { |
|
137 rv = Upper().BindToStatement(aStatement, NS_LITERAL_CSTRING("upper_key")); |
|
138 NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); |
|
139 } |
|
140 |
|
141 return NS_OK; |
|
142 } |
|
143 |
|
144 template <class T> |
|
145 void ToSerializedKeyRange(T& aKeyRange); |
|
146 |
|
147 void DropJSObjects(); |
|
148 |
|
149 // WebIDL |
|
150 JSObject* |
|
151 WrapObject(JSContext* aCx); |
|
152 |
|
153 nsISupports* |
|
154 GetParentObject() const |
|
155 { |
|
156 return mGlobal; |
|
157 } |
|
158 |
|
159 void |
|
160 GetLower(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, |
|
161 ErrorResult& aRv); |
|
162 |
|
163 void |
|
164 GetUpper(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, |
|
165 ErrorResult& aRv); |
|
166 |
|
167 bool |
|
168 LowerOpen() const |
|
169 { |
|
170 return mLowerOpen; |
|
171 } |
|
172 |
|
173 bool |
|
174 UpperOpen() const |
|
175 { |
|
176 return mUpperOpen; |
|
177 } |
|
178 |
|
179 static already_AddRefed<IDBKeyRange> |
|
180 Only(const GlobalObject& aGlobal, JSContext* aCx, |
|
181 JS::Handle<JS::Value> aValue, ErrorResult& aRv); |
|
182 |
|
183 static already_AddRefed<IDBKeyRange> |
|
184 LowerBound(const GlobalObject& aGlobal, JSContext* aCx, |
|
185 JS::Handle<JS::Value> aValue, bool aOpen, ErrorResult& aRv); |
|
186 |
|
187 static already_AddRefed<IDBKeyRange> |
|
188 UpperBound(const GlobalObject& aGlobal, JSContext* aCx, |
|
189 JS::Handle<JS::Value> aValue, bool aOpen, ErrorResult& aRv); |
|
190 |
|
191 static already_AddRefed<IDBKeyRange> |
|
192 Bound(const GlobalObject& aGlobal, JSContext* aCx, |
|
193 JS::Handle<JS::Value> aLower, JS::Handle<JS::Value> aUpper, |
|
194 bool aLowerOpen, bool aUpperOpen, ErrorResult& aRv); |
|
195 |
|
196 private: |
|
197 IDBKeyRange(nsISupports* aGlobal, |
|
198 bool aLowerOpen, |
|
199 bool aUpperOpen, |
|
200 bool aIsOnly) |
|
201 : mGlobal(aGlobal), mCachedLowerVal(JSVAL_VOID), mCachedUpperVal(JSVAL_VOID), |
|
202 mLowerOpen(aLowerOpen), mUpperOpen(aUpperOpen), mIsOnly(aIsOnly), |
|
203 mHaveCachedLowerVal(false), mHaveCachedUpperVal(false), mRooted(false) |
|
204 { } |
|
205 |
|
206 ~IDBKeyRange(); |
|
207 |
|
208 nsCOMPtr<nsISupports> mGlobal; |
|
209 Key mLower; |
|
210 Key mUpper; |
|
211 JS::Heap<JS::Value> mCachedLowerVal; |
|
212 JS::Heap<JS::Value> mCachedUpperVal; |
|
213 const bool mLowerOpen; |
|
214 const bool mUpperOpen; |
|
215 const bool mIsOnly; |
|
216 bool mHaveCachedLowerVal; |
|
217 bool mHaveCachedUpperVal; |
|
218 bool mRooted; |
|
219 }; |
|
220 |
|
221 END_INDEXEDDB_NAMESPACE |
|
222 |
|
223 #endif // mozilla_dom_indexeddb_idbkeyrange_h__ |