#include "config.h"
#include "IDBIndex.h"
#if ENABLE(INDEXED_DATABASE)
#include "IDBCursorBackendInterface.h"
#include "IDBDatabaseException.h"
#include "IDBIndexBackendInterface.h"
#include "IDBKey.h"
#include "IDBKeyRange.h"
#include "IDBObjectStore.h"
#include "IDBRequest.h"
#include "IDBTracing.h"
#include "IDBTransaction.h"
namespace WebCore {
static const unsigned short defaultDirection = IDBCursor::NEXT;
IDBIndex::IDBIndex(PassRefPtr<IDBIndexBackendInterface> backend, IDBObjectStore* objectStore, IDBTransaction* transaction)
: m_backend(backend)
, m_objectStore(objectStore)
, m_transaction(transaction)
{
ASSERT(m_backend);
ASSERT(m_objectStore);
ASSERT(m_transaction);
}
IDBIndex::~IDBIndex()
{
}
PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::openCursor");
if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
ec = IDBDatabaseException::CONSTRAINT_ERR;
return 0;
}
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
request->setCursorType(IDBCursorBackendInterface::IndexCursor);
m_backend->openCursor(keyRange, direction, request, m_transaction->backend(), ec);
if (ec) {
request->markEarlyDeath();
return 0;
}
return request;
}
PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
{
IDB_TRACE("IDBObjectStore::openCursor");
RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
if (ec)
return 0;
return openCursor(context, keyRange.release(), ec);
}
PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::count");
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
m_backend->count(keyRange, request, m_transaction->backend(), ec);
if (ec) {
request->markEarlyDeath();
return 0;
}
return request;
}
PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::count");
RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
if (ec)
return 0;
return count(context, keyRange.release(), ec);
}
PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::openKeyCursor");
if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
ec = IDBDatabaseException::CONSTRAINT_ERR;
return 0;
}
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
request->setCursorType(IDBCursorBackendInterface::IndexKeyCursor);
m_backend->openKeyCursor(keyRange, direction, request, m_transaction->backend(), ec);
if (ec) {
request->markEarlyDeath();
return 0;
}
return request;
}
PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
{
IDB_TRACE("IDBObjectStore::openKeyCursor");
RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
if (ec)
return 0;
return openKeyCursor(context, keyRange.release(), ec);
}
PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::get");
if (key && (key->type() == IDBKey::InvalidType)) {
ec = IDBDatabaseException::DATA_ERR;
return 0;
}
RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
if (ec)
return 0;
return get(context, keyRange.release(), ec);
}
PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::get");
if (!keyRange) {
ec = IDBDatabaseException::DATA_ERR;
return 0;
}
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
m_backend->get(keyRange, request, m_transaction->backend(), ec);
if (ec) {
request->markEarlyDeath();
return 0;
}
return request;
}
PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::getKey");
if (key && (key->type() == IDBKey::InvalidType)) {
ec = IDBDatabaseException::DATA_ERR;
return 0;
}
RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
if (ec)
return 0;
return getKey(context, keyRange.release(), ec);
}
PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
{
IDB_TRACE("IDBIndex::getKey");
if (!keyRange) {
ec = IDBDatabaseException::DATA_ERR;
return 0;
}
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
m_backend->getKey(keyRange, request, m_transaction->backend(), ec);
if (ec) {
request->markEarlyDeath();
return 0;
}
return request;
}
}
#endif // ENABLE(INDEXED_DATABASE)