Add helper for retrieving KeyPath value from JSON

This commit is contained in:
Ben Turner 2010-06-03 16:32:59 -07:00
parent 4b0375736e
commit 5e636aa26a
2 changed files with 62 additions and 0 deletions

View File

@ -433,6 +433,49 @@ IDBObjectStoreRequest::GetJSONFromArg0(/* jsval arg0, */
return NS_OK;
}
// static
nsresult
IDBObjectStoreRequest::GetKeyPathValueFromJSON(const nsAString& aJSON,
const nsAString& aKeyPath,
JSContext** aCx,
nsAString& aValue)
{
NS_ASSERTION(!aJSON.IsEmpty(), "Empty JSON!");
NS_ASSERTION(!aKeyPath.IsEmpty(), "Empty keyPath!");
NS_ASSERTION(aCx, "Null pointer!");
nsresult rv;
if (!*aCx) {
rv = nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(aCx);
NS_ENSURE_SUCCESS(rv, rv);
}
JSAutoRequest ar(*aCx);
js::AutoValueRooter clone(*aCx);
nsCOMPtr<nsIJSON> json(new nsJSON());
rv = json->DecodeToJSVal(aJSON, *aCx, clone.addr());
NS_ENSURE_SUCCESS(rv, rv);
JSObject* obj = JSVAL_TO_OBJECT(clone.value());
const jschar* keyPathChars =
reinterpret_cast<const jschar*>(aKeyPath.BeginReading());
const size_t keyPathLen = aKeyPath.Length();
js::AutoValueRooter value(*aCx);
JSBool ok = JS_GetUCProperty(*aCx, obj, keyPathChars, keyPathLen,
value.addr());
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
rv = json->EncodeFromJSVal(value.addr(), *aCx, aValue);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
ObjectStoreInfo*
IDBObjectStoreRequest::GetObjectStoreInfo()
{
@ -1544,6 +1587,17 @@ CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
// Get the id of this object store, and store it for future use.
(void)aConnection->GetLastInsertRowID(&mId);
/* Populate
JSContext* cx = nsnull;
for (i = 0; i < dataCount; i++) {
nsString value;
rv = IDBObjectStoreRequest::GetKeyPathValueFromJSON(data[dataCount],
mKeyPath, &cx, value);
NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR);
// Add value to index
}
return OK;
}

View File

@ -46,6 +46,8 @@
#include "nsIIDBObjectStoreRequest.h"
struct JSContext;
BEGIN_INDEXEDDB_NAMESPACE
struct ObjectStoreInfo;
@ -223,6 +225,12 @@ public:
GetJSONFromArg0(/* jsval arg0, */
nsAString& aJSON);
static nsresult
GetKeyPathValueFromJSON(const nsAString& aJSON,
const nsAString& aKeyPath,
JSContext** aCx,
nsAString& aValue);
bool TransactionIsOpen() const
{
return mTransaction->TransactionIsOpen();