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 "IDBKeyRange.h"
28
29#include "DOMRequestState.h"
30#include "IDBBindingUtilities.h"
31#include "IDBDatabaseException.h"
32#include "IDBKey.h"
33
34#if ENABLE(INDEXED_DATABASE)
35
36namespace WebCore {
37
38PassRefPtr<IDBKeyRange> IDBKeyRange::create(PassRefPtr<IDBKey> prpKey)
39{
40    RefPtr<IDBKey> key = prpKey;
41    return adoptRef(new IDBKeyRange(key, key, LowerBoundClosed, UpperBoundClosed));
42}
43
44IDBKeyRange::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}
51
52Deprecated::ScriptValue IDBKeyRange::lowerValue(ScriptExecutionContext* context) const
53{
54    DOMRequestState requestState(context);
55    return idbKeyToScriptValue(&requestState, m_lower);
56}
57
58Deprecated::ScriptValue IDBKeyRange::upperValue(ScriptExecutionContext* context) const
59{
60    DOMRequestState requestState(context);
61    return idbKeyToScriptValue(&requestState, m_upper);
62}
63
64PassRefPtr<IDBKeyRange> IDBKeyRange::only(PassRefPtr<IDBKey> prpKey, ExceptionCode& ec)
65{
66    RefPtr<IDBKey> key = prpKey;
67    if (!key || !key->isValid()) {
68        ec = IDBDatabaseException::DataError;
69        return 0;
70    }
71
72    return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed);
73}
74
75PassRefPtr<IDBKeyRange> IDBKeyRange::only(ScriptExecutionContext* context, const Deprecated::ScriptValue& keyValue, ExceptionCode& ec)
76{
77    DOMRequestState requestState(context);
78    RefPtr<IDBKey> key = scriptValueToIDBKey(&requestState, keyValue);
79    if (!key || !key->isValid()) {
80        ec = IDBDatabaseException::DataError;
81        return 0;
82    }
83
84    return IDBKeyRange::create(key, key, LowerBoundClosed, UpperBoundClosed);
85}
86
87PassRefPtr<IDBKeyRange> IDBKeyRange::lowerBound(ScriptExecutionContext* context, const Deprecated::ScriptValue& boundValue, bool open, ExceptionCode& ec)
88{
89    DOMRequestState requestState(context);
90    RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue);
91    if (!bound || !bound->isValid()) {
92        ec = IDBDatabaseException::DataError;
93        return 0;
94    }
95
96    return IDBKeyRange::create(bound, 0, open ? LowerBoundOpen : LowerBoundClosed, UpperBoundOpen);
97}
98
99PassRefPtr<IDBKeyRange> IDBKeyRange::upperBound(ScriptExecutionContext* context, const Deprecated::ScriptValue& boundValue, bool open, ExceptionCode& ec)
100{
101    DOMRequestState requestState(context);
102    RefPtr<IDBKey> bound = scriptValueToIDBKey(&requestState, boundValue);
103    if (!bound || !bound->isValid()) {
104        ec = IDBDatabaseException::DataError;
105        return 0;
106    }
107
108    return IDBKeyRange::create(0, bound, LowerBoundOpen, open ? UpperBoundOpen : UpperBoundClosed);
109}
110
111PassRefPtr<IDBKeyRange> IDBKeyRange::bound(ScriptExecutionContext* context, const Deprecated::ScriptValue& lowerValue, const Deprecated::ScriptValue& upperValue, bool lowerOpen, bool upperOpen, ExceptionCode& ec)
112{
113    DOMRequestState requestState(context);
114    RefPtr<IDBKey> lower = scriptValueToIDBKey(&requestState, lowerValue);
115    RefPtr<IDBKey> upper = scriptValueToIDBKey(&requestState, upperValue);
116
117    if (!lower || !lower->isValid() || !upper || !upper->isValid()) {
118        ec = IDBDatabaseException::DataError;
119        return 0;
120    }
121    if (upper->isLessThan(lower.get())) {
122        ec = IDBDatabaseException::DataError;
123        return 0;
124    }
125    if (upper->isEqual(lower.get()) && (lowerOpen || upperOpen)) {
126        ec = IDBDatabaseException::DataError;
127        return 0;
128    }
129
130    return IDBKeyRange::create(lower, upper, lowerOpen ? LowerBoundOpen : LowerBoundClosed, upperOpen ? UpperBoundOpen : UpperBoundClosed);
131}
132
133bool IDBKeyRange::isOnlyKey() const
134{
135    if (m_lowerType != LowerBoundClosed || m_upperType != UpperBoundClosed)
136        return false;
137
138    ASSERT(m_lower);
139    ASSERT(m_upper);
140    return m_lower->isEqual(m_upper.get());
141}
142
143} // namespace WebCore
144
145#endif // ENABLE(INDEXED_DATABASE)
146