#include "config.h"
#include "IDBKeyRange.h"
#if ENABLE(INDEXED_DATABASE)
#include "IDBBindingUtilities.h"
#include "IDBKey.h"
#include "IDBKeyData.h"
#include "ScriptExecutionContext.h"
#include <JavaScriptCore/JSCJSValue.h>
namespace WebCore {
using namespace JSC;
Ref<IDBKeyRange> IDBKeyRange::create(RefPtr<IDBKey>&& lower, RefPtr<IDBKey>&& upper, bool isLowerOpen, bool isUpperOpen)
{
return adoptRef(*new IDBKeyRange(WTFMove(lower), WTFMove(upper), isLowerOpen, isUpperOpen));
}
Ref<IDBKeyRange> IDBKeyRange::create(RefPtr<IDBKey>&& key)
{
auto upper = key;
return create(WTFMove(key), WTFMove(upper), false, false);
}
IDBKeyRange::IDBKeyRange(RefPtr<IDBKey>&& lower, RefPtr<IDBKey>&& upper, bool isLowerOpen, bool isUpperOpen)
: m_lower(WTFMove(lower))
, m_upper(WTFMove(upper))
, m_isLowerOpen(isLowerOpen)
, m_isUpperOpen(isUpperOpen)
{
}
IDBKeyRange::~IDBKeyRange() = default;
ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::only(RefPtr<IDBKey>&& key)
{
if (!key || !key->isValid())
return Exception { DataError };
return create(WTFMove(key));
}
ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::only(ExecState& state, JSValue keyValue)
{
return only(scriptValueToIDBKey(state, keyValue));
}
ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::lowerBound(ExecState& state, JSValue boundValue, bool open)
{
auto bound = scriptValueToIDBKey(state, boundValue);
if (!bound->isValid())
return Exception { DataError };
return create(WTFMove(bound), nullptr, open, true);
}
ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::upperBound(ExecState& state, JSValue boundValue, bool open)
{
auto bound = scriptValueToIDBKey(state, boundValue);
if (!bound->isValid())
return Exception { DataError };
return create(nullptr, WTFMove(bound), true, open);
}
ExceptionOr<Ref<IDBKeyRange>> IDBKeyRange::bound(ExecState& state, JSValue lowerValue, JSValue upperValue, bool lowerOpen, bool upperOpen)
{
auto lower = scriptValueToIDBKey(state, lowerValue);
auto upper = scriptValueToIDBKey(state, upperValue);
if (!lower->isValid() || !upper->isValid())
return Exception { DataError };
if (upper->isLessThan(lower.get()))
return Exception { DataError };
if (upper->isEqual(lower.get()) && (lowerOpen || upperOpen))
return Exception { DataError };
return create(WTFMove(lower), WTFMove(upper), lowerOpen, upperOpen);
}
bool IDBKeyRange::isOnlyKey() const
{
return m_lower && m_upper && !m_isLowerOpen && !m_isUpperOpen && m_lower->isEqual(*m_upper);
}
ExceptionOr<bool> IDBKeyRange::includes(JSC::ExecState& state, JSC::JSValue keyValue)
{
auto key = scriptValueToIDBKey(state, keyValue);
if (!key->isValid())
return Exception { DataError, "Failed to execute 'includes' on 'IDBKeyRange': The passed-in value is not a valid IndexedDB key." };
if (m_lower) {
int compare = m_lower->compare(key.get());
if (compare > 0)
return false;
if (m_isLowerOpen && !compare)
return false;
}
if (m_upper) {
int compare = m_upper->compare(key.get());
if (compare < 0)
return false;
if (m_isUpperOpen && !compare)
return false;
}
return true;
}
}
#endif // ENABLE(INDEXED_DATABASE)