michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_indexeddb_idbkeyrange_h__ michael@0: #define mozilla_dom_indexeddb_idbkeyrange_h__ michael@0: michael@0: #include "mozilla/dom/indexedDB/IndexedDatabase.h" michael@0: #include "mozilla/dom/indexedDB/Key.h" michael@0: michael@0: #include "nsISupports.h" michael@0: michael@0: #include "mozilla/ErrorResult.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: michael@0: class mozIStorageStatement; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: class GlobalObject; michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: BEGIN_INDEXEDDB_NAMESPACE michael@0: michael@0: namespace ipc { michael@0: class KeyRange; michael@0: } // namespace ipc michael@0: michael@0: class IDBKeyRange MOZ_FINAL : public nsISupports michael@0: { michael@0: public: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBKeyRange) michael@0: michael@0: static nsresult FromJSVal(JSContext* aCx, michael@0: JS::Handle aVal, michael@0: IDBKeyRange** aKeyRange); michael@0: michael@0: template michael@0: static already_AddRefed michael@0: FromSerializedKeyRange(const T& aKeyRange); michael@0: michael@0: const Key& Lower() const michael@0: { michael@0: return mLower; michael@0: } michael@0: michael@0: Key& Lower() michael@0: { michael@0: return mLower; michael@0: } michael@0: michael@0: const Key& Upper() const michael@0: { michael@0: return mIsOnly ? mLower : mUpper; michael@0: } michael@0: michael@0: Key& Upper() michael@0: { michael@0: return mIsOnly ? mLower : mUpper; michael@0: } michael@0: michael@0: // TODO: Remove these in favour of LowerOpen() / UpperOpen(), bug 900578. michael@0: bool IsLowerOpen() const michael@0: { michael@0: return mLowerOpen; michael@0: } michael@0: michael@0: bool IsUpperOpen() const michael@0: { michael@0: return mUpperOpen; michael@0: } michael@0: michael@0: bool IsOnly() const michael@0: { michael@0: return mIsOnly; michael@0: } michael@0: michael@0: void GetBindingClause(const nsACString& aKeyColumnName, michael@0: nsACString& _retval) const michael@0: { michael@0: NS_NAMED_LITERAL_CSTRING(andStr, " AND "); michael@0: NS_NAMED_LITERAL_CSTRING(spacecolon, " :"); michael@0: NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key"); michael@0: michael@0: if (IsOnly()) { michael@0: // Both keys are set and they're equal. michael@0: _retval = andStr + aKeyColumnName + NS_LITERAL_CSTRING(" =") + michael@0: spacecolon + lowerKey; michael@0: } michael@0: else { michael@0: nsAutoCString clause; michael@0: michael@0: if (!Lower().IsUnset()) { michael@0: // Lower key is set. michael@0: clause.Append(andStr + aKeyColumnName); michael@0: clause.AppendLiteral(" >"); michael@0: if (!IsLowerOpen()) { michael@0: clause.AppendLiteral("="); michael@0: } michael@0: clause.Append(spacecolon + lowerKey); michael@0: } michael@0: michael@0: if (!Upper().IsUnset()) { michael@0: // Upper key is set. michael@0: clause.Append(andStr + aKeyColumnName); michael@0: clause.AppendLiteral(" <"); michael@0: if (!IsUpperOpen()) { michael@0: clause.AppendLiteral("="); michael@0: } michael@0: clause.Append(spacecolon + NS_LITERAL_CSTRING("upper_key")); michael@0: } michael@0: michael@0: _retval = clause; michael@0: } michael@0: } michael@0: michael@0: nsresult BindToStatement(mozIStorageStatement* aStatement) const michael@0: { michael@0: NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key"); michael@0: michael@0: if (IsOnly()) { michael@0: return Lower().BindToStatement(aStatement, lowerKey); michael@0: } michael@0: michael@0: nsresult rv; michael@0: michael@0: if (!Lower().IsUnset()) { michael@0: rv = Lower().BindToStatement(aStatement, lowerKey); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); michael@0: } michael@0: michael@0: if (!Upper().IsUnset()) { michael@0: rv = Upper().BindToStatement(aStatement, NS_LITERAL_CSTRING("upper_key")); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: template michael@0: void ToSerializedKeyRange(T& aKeyRange); michael@0: michael@0: void DropJSObjects(); michael@0: michael@0: // WebIDL michael@0: JSObject* michael@0: WrapObject(JSContext* aCx); michael@0: michael@0: nsISupports* michael@0: GetParentObject() const michael@0: { michael@0: return mGlobal; michael@0: } michael@0: michael@0: void michael@0: GetLower(JSContext* aCx, JS::MutableHandle aResult, michael@0: ErrorResult& aRv); michael@0: michael@0: void michael@0: GetUpper(JSContext* aCx, JS::MutableHandle aResult, michael@0: ErrorResult& aRv); michael@0: michael@0: bool michael@0: LowerOpen() const michael@0: { michael@0: return mLowerOpen; michael@0: } michael@0: michael@0: bool michael@0: UpperOpen() const michael@0: { michael@0: return mUpperOpen; michael@0: } michael@0: michael@0: static already_AddRefed michael@0: Only(const GlobalObject& aGlobal, JSContext* aCx, michael@0: JS::Handle aValue, ErrorResult& aRv); michael@0: michael@0: static already_AddRefed michael@0: LowerBound(const GlobalObject& aGlobal, JSContext* aCx, michael@0: JS::Handle aValue, bool aOpen, ErrorResult& aRv); michael@0: michael@0: static already_AddRefed michael@0: UpperBound(const GlobalObject& aGlobal, JSContext* aCx, michael@0: JS::Handle aValue, bool aOpen, ErrorResult& aRv); michael@0: michael@0: static already_AddRefed michael@0: Bound(const GlobalObject& aGlobal, JSContext* aCx, michael@0: JS::Handle aLower, JS::Handle aUpper, michael@0: bool aLowerOpen, bool aUpperOpen, ErrorResult& aRv); michael@0: michael@0: private: michael@0: IDBKeyRange(nsISupports* aGlobal, michael@0: bool aLowerOpen, michael@0: bool aUpperOpen, michael@0: bool aIsOnly) michael@0: : mGlobal(aGlobal), mCachedLowerVal(JSVAL_VOID), mCachedUpperVal(JSVAL_VOID), michael@0: mLowerOpen(aLowerOpen), mUpperOpen(aUpperOpen), mIsOnly(aIsOnly), michael@0: mHaveCachedLowerVal(false), mHaveCachedUpperVal(false), mRooted(false) michael@0: { } michael@0: michael@0: ~IDBKeyRange(); michael@0: michael@0: nsCOMPtr mGlobal; michael@0: Key mLower; michael@0: Key mUpper; michael@0: JS::Heap mCachedLowerVal; michael@0: JS::Heap mCachedUpperVal; michael@0: const bool mLowerOpen; michael@0: const bool mUpperOpen; michael@0: const bool mIsOnly; michael@0: bool mHaveCachedLowerVal; michael@0: bool mHaveCachedUpperVal; michael@0: bool mRooted; michael@0: }; michael@0: michael@0: END_INDEXEDDB_NAMESPACE michael@0: michael@0: #endif // mozilla_dom_indexeddb_idbkeyrange_h__