Home | History | Annotate | Download | only in indexeddb
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "modules/indexeddb/IDBKeyRange.h"
     28 
     29 #include "bindings/v8/DOMRequestState.h"
     30 #include "bindings/v8/ExceptionState.h"
     31 #include "bindings/v8/IDBBindingUtilities.h"
     32 #include "core/dom/ExceptionCode.h"
     33 #include "modules/indexeddb/IDBDatabase.h"
     34 #include "modules/indexeddb/IDBKey.h"
     35 
     36 namespace WebCore {
     37 
     38 PassRefPtr<IDBKeyRange> IDBKeyRange::create(PassRefPtr<IDBKey> prpKey)
     39 {
     40     RefPtr<IDBKey> key = prpKey;
     41     return adoptRef(new IDBKeyRange(key, key, LowerBoundClosed, UpperBoundClosed));
     42 }
     43 
     44 IDBKeyRange::IDBKeyRange(PassRefPtr<IDBKey> lower, PassRefPtr<IDBKey> upper, LowerBoundType lowerType, UpperBoundType upperType)
     45     : m_lower(lower)
     46     , m_upper(upper)
     47     , m_lowerType(lowerType)
     48     , m_upperType(upperType)
     49 {
     50     ScriptWrappable::init(this);
     51 }
     52 
     53 ScriptValue IDBKeyRange::lowerValue(ScriptExecutionContext* context) const
     54 {
     55     DOMRequestState requestState(context);
     56     return idbKeyToScriptValue(&requestState, m_lower);
     57 }
     58 
     59 ScriptValue IDBKeyRange::upperValue(ScriptExecutionContext* context) const
     60 {
     61     DOMRequestState requestState(context);
     62     return idbKeyToScriptValue(&requestState, m_upper);
     63 }
     64 
     65 PassRefPtr<IDBKeyRange> IDBKeyRange::only(PassRefPtr<IDBKey> prpKey, ExceptionState& es)
     66 {
     67     RefPtr<IDBKey> key = prpKey;
     68     if (!key || !key->isValid()) {
     69         es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
     70         return 0;
     71     }
     72 
     73     return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed);
     74 }
     75 
     76 PassRefPtr<IDBKeyRange> IDBKeyRange::only(ScriptExecutionContext* context, const ScriptValue& keyValue, ExceptionState& es)
     77 {
     78     DOMRequestState requestState(context);
     79     RefPtr<IDBKey> key = scriptValueToIDBKey(&requestState, keyValue);
     80     if (!key || !key->isValid()) {
     81         es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
     82         return 0;
     83     }
     84 
     85     return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed);
     86 }
     87 
     88 PassRefPtr<IDBKeyRange> IDBKeyRange::lowerBound(ScriptExecutionContext* context, const ScriptValue& boundValue, bool open, ExceptionState& es)
     89 {
     90     DOMRequestState requestState(context);
     91     RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue);
     92     if (!bound || !bound->isValid()) {
     93         es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
     94         return 0;
     95     }
     96 
     97     return IDBKeyRange::create(bound, 0, open ? LowerBoundOpen : LowerBoundClosed, UpperBoundOpen);
     98 }
     99 
    100 PassRefPtr<IDBKeyRange> IDBKeyRange::upperBound(ScriptExecutionContext* context, const ScriptValue& boundValue, bool open, ExceptionState& es)
    101 {
    102     DOMRequestState requestState(context);
    103     RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue);
    104     if (!bound || !bound->isValid()) {
    105         es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
    106         return 0;
    107     }
    108 
    109     return IDBKeyRange::create(0, bound, LowerBoundOpen, open ? UpperBoundOpen : UpperBoundClosed);
    110 }
    111 
    112 PassRefPtr<IDBKeyRange> IDBKeyRange::bound(ScriptExecutionContext* context, const ScriptValue& lowerValue, const ScriptValue& upperValue, bool lowerOpen, bool upperOpen, ExceptionState& es)
    113 {
    114     DOMRequestState requestState(context);
    115     RefPtr<IDBKey> lower = scriptValueToIDBKey(&requestState, lowerValue);
    116     RefPtr<IDBKey> upper = scriptValueToIDBKey(&requestState, upperValue);
    117 
    118     if (!lower || !lower->isValid() || !upper || !upper->isValid()) {
    119         es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
    120         return 0;
    121     }
    122     if (upper->isLessThan(lower.get())) {
    123         es.throwDOMException(DataError, "The lower key is greater than the upper key.");
    124         return 0;
    125     }
    126     if (upper->isEqual(lower.get()) && (lowerOpen || upperOpen)) {
    127         es.throwDOMException(DataError, "The lower key and upper key are equal and one of the bounds is open.");
    128         return 0;
    129     }
    130 
    131     return IDBKeyRange::create(lower, upper, lowerOpen ? LowerBoundOpen : LowerBoundClosed, upperOpen ? UpperBoundOpen : UpperBoundClosed);
    132 }
    133 
    134 bool IDBKeyRange::isOnlyKey() const
    135 {
    136     if (m_lowerType != LowerBoundClosed || m_upperType != UpperBoundClosed)
    137         return false;
    138 
    139     ASSERT(m_lower);
    140     ASSERT(m_upper);
    141     return m_lower->isEqual(m_upper.get());
    142 }
    143 
    144 } // namespace WebCore
    145