dom/indexedDB/IDBKeyRange.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/IDBKeyRange.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,223 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_dom_indexeddb_idbkeyrange_h__
    1.11 +#define mozilla_dom_indexeddb_idbkeyrange_h__
    1.12 +
    1.13 +#include "mozilla/dom/indexedDB/IndexedDatabase.h"
    1.14 +#include "mozilla/dom/indexedDB/Key.h"
    1.15 +
    1.16 +#include "nsISupports.h"
    1.17 +
    1.18 +#include "mozilla/ErrorResult.h"
    1.19 +#include "nsCycleCollectionParticipant.h"
    1.20 +
    1.21 +class mozIStorageStatement;
    1.22 +
    1.23 +namespace mozilla {
    1.24 +namespace dom {
    1.25 +class GlobalObject;
    1.26 +} // namespace dom
    1.27 +} // namespace mozilla
    1.28 +
    1.29 +BEGIN_INDEXEDDB_NAMESPACE
    1.30 +
    1.31 +namespace ipc {
    1.32 +class KeyRange;
    1.33 +} // namespace ipc
    1.34 +
    1.35 +class IDBKeyRange MOZ_FINAL : public nsISupports
    1.36 +{
    1.37 +public:
    1.38 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.39 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBKeyRange)
    1.40 +
    1.41 +  static nsresult FromJSVal(JSContext* aCx,
    1.42 +                            JS::Handle<JS::Value> aVal,
    1.43 +                            IDBKeyRange** aKeyRange);
    1.44 +
    1.45 +  template <class T>
    1.46 +  static already_AddRefed<IDBKeyRange>
    1.47 +  FromSerializedKeyRange(const T& aKeyRange);
    1.48 +
    1.49 +  const Key& Lower() const
    1.50 +  {
    1.51 +    return mLower;
    1.52 +  }
    1.53 +
    1.54 +  Key& Lower()
    1.55 +  {
    1.56 +    return mLower;
    1.57 +  }
    1.58 +
    1.59 +  const Key& Upper() const
    1.60 +  {
    1.61 +    return mIsOnly ? mLower : mUpper;
    1.62 +  }
    1.63 +
    1.64 +  Key& Upper()
    1.65 +  {
    1.66 +    return mIsOnly ? mLower : mUpper;
    1.67 +  }
    1.68 +
    1.69 +  // TODO: Remove these in favour of LowerOpen() / UpperOpen(), bug 900578.
    1.70 +  bool IsLowerOpen() const
    1.71 +  {
    1.72 +    return mLowerOpen;
    1.73 +  }
    1.74 +
    1.75 +  bool IsUpperOpen() const
    1.76 +  {
    1.77 +    return mUpperOpen;
    1.78 +  }
    1.79 +
    1.80 +  bool IsOnly() const
    1.81 +  {
    1.82 +    return mIsOnly;
    1.83 +  }
    1.84 +
    1.85 +  void GetBindingClause(const nsACString& aKeyColumnName,
    1.86 +                        nsACString& _retval) const
    1.87 +  {
    1.88 +    NS_NAMED_LITERAL_CSTRING(andStr, " AND ");
    1.89 +    NS_NAMED_LITERAL_CSTRING(spacecolon, " :");
    1.90 +    NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key");
    1.91 +
    1.92 +    if (IsOnly()) {
    1.93 +      // Both keys are set and they're equal.
    1.94 +      _retval = andStr + aKeyColumnName + NS_LITERAL_CSTRING(" =") +
    1.95 +                spacecolon + lowerKey;
    1.96 +    }
    1.97 +    else {
    1.98 +      nsAutoCString clause;
    1.99 +
   1.100 +      if (!Lower().IsUnset()) {
   1.101 +        // Lower key is set.
   1.102 +        clause.Append(andStr + aKeyColumnName);
   1.103 +        clause.AppendLiteral(" >");
   1.104 +        if (!IsLowerOpen()) {
   1.105 +          clause.AppendLiteral("=");
   1.106 +        }
   1.107 +        clause.Append(spacecolon + lowerKey);
   1.108 +      }
   1.109 +
   1.110 +      if (!Upper().IsUnset()) {
   1.111 +        // Upper key is set.
   1.112 +        clause.Append(andStr + aKeyColumnName);
   1.113 +        clause.AppendLiteral(" <");
   1.114 +        if (!IsUpperOpen()) {
   1.115 +          clause.AppendLiteral("=");
   1.116 +        }
   1.117 +        clause.Append(spacecolon + NS_LITERAL_CSTRING("upper_key"));
   1.118 +      }
   1.119 +
   1.120 +      _retval = clause;
   1.121 +    }
   1.122 +  }
   1.123 +
   1.124 +  nsresult BindToStatement(mozIStorageStatement* aStatement) const
   1.125 +  {
   1.126 +    NS_NAMED_LITERAL_CSTRING(lowerKey, "lower_key");
   1.127 +
   1.128 +    if (IsOnly()) {
   1.129 +      return Lower().BindToStatement(aStatement, lowerKey);
   1.130 +    }
   1.131 +
   1.132 +    nsresult rv;
   1.133 +
   1.134 +    if (!Lower().IsUnset()) {
   1.135 +      rv = Lower().BindToStatement(aStatement, lowerKey);
   1.136 +      NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.137 +    }
   1.138 +
   1.139 +    if (!Upper().IsUnset()) {
   1.140 +      rv = Upper().BindToStatement(aStatement, NS_LITERAL_CSTRING("upper_key"));
   1.141 +      NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.142 +    }
   1.143 +
   1.144 +    return NS_OK;
   1.145 +  }
   1.146 +
   1.147 +  template <class T>
   1.148 +  void ToSerializedKeyRange(T& aKeyRange);
   1.149 +
   1.150 +  void DropJSObjects();
   1.151 +
   1.152 +  // WebIDL
   1.153 +  JSObject*
   1.154 +  WrapObject(JSContext* aCx);
   1.155 +
   1.156 +  nsISupports*
   1.157 +  GetParentObject() const
   1.158 +  {
   1.159 +    return mGlobal;
   1.160 +  }
   1.161 +
   1.162 +  void
   1.163 +  GetLower(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
   1.164 +           ErrorResult& aRv);
   1.165 +
   1.166 +  void
   1.167 +  GetUpper(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
   1.168 +           ErrorResult& aRv);
   1.169 +
   1.170 +  bool
   1.171 +  LowerOpen() const
   1.172 +  {
   1.173 +    return mLowerOpen;
   1.174 +  }
   1.175 +
   1.176 +  bool
   1.177 +  UpperOpen() const
   1.178 +  {
   1.179 +    return mUpperOpen;
   1.180 +  }
   1.181 +
   1.182 +  static already_AddRefed<IDBKeyRange>
   1.183 +  Only(const GlobalObject& aGlobal, JSContext* aCx,
   1.184 +       JS::Handle<JS::Value> aValue, ErrorResult& aRv);
   1.185 +
   1.186 +  static already_AddRefed<IDBKeyRange>
   1.187 +  LowerBound(const GlobalObject& aGlobal, JSContext* aCx,
   1.188 +             JS::Handle<JS::Value> aValue, bool aOpen, ErrorResult& aRv);
   1.189 +
   1.190 +  static already_AddRefed<IDBKeyRange>
   1.191 +  UpperBound(const GlobalObject& aGlobal, JSContext* aCx,
   1.192 +             JS::Handle<JS::Value> aValue, bool aOpen, ErrorResult& aRv);
   1.193 +
   1.194 +  static already_AddRefed<IDBKeyRange>
   1.195 +  Bound(const GlobalObject& aGlobal, JSContext* aCx,
   1.196 +        JS::Handle<JS::Value> aLower, JS::Handle<JS::Value> aUpper,
   1.197 +        bool aLowerOpen, bool aUpperOpen, ErrorResult& aRv);
   1.198 +
   1.199 +private:
   1.200 +  IDBKeyRange(nsISupports* aGlobal,
   1.201 +              bool aLowerOpen,
   1.202 +              bool aUpperOpen,
   1.203 +              bool aIsOnly)
   1.204 +  : mGlobal(aGlobal), mCachedLowerVal(JSVAL_VOID), mCachedUpperVal(JSVAL_VOID),
   1.205 +    mLowerOpen(aLowerOpen), mUpperOpen(aUpperOpen), mIsOnly(aIsOnly),
   1.206 +    mHaveCachedLowerVal(false), mHaveCachedUpperVal(false), mRooted(false)
   1.207 +  { }
   1.208 +
   1.209 +  ~IDBKeyRange();
   1.210 +
   1.211 +  nsCOMPtr<nsISupports> mGlobal;
   1.212 +  Key mLower;
   1.213 +  Key mUpper;
   1.214 +  JS::Heap<JS::Value> mCachedLowerVal;
   1.215 +  JS::Heap<JS::Value> mCachedUpperVal;
   1.216 +  const bool mLowerOpen;
   1.217 +  const bool mUpperOpen;
   1.218 +  const bool mIsOnly;
   1.219 +  bool mHaveCachedLowerVal;
   1.220 +  bool mHaveCachedUpperVal;
   1.221 +  bool mRooted;
   1.222 +};
   1.223 +
   1.224 +END_INDEXEDDB_NAMESPACE
   1.225 +
   1.226 +#endif // mozilla_dom_indexeddb_idbkeyrange_h__

mercurial