Bug 706068: Make complex keys work on auto-increment object stores. Patch by khuey and sicking. r=sicking on parts by khuey and r=bent on parts by sicking.

This commit is contained in:
Jonas Sicking 2011-12-04 09:39:02 -08:00
Родитель 4d2f4d4ad2
Коммит 5ed415d612
3 изменённых файлов: 190 добавлений и 38 удалений

Просмотреть файл

@ -438,8 +438,13 @@ IDBDatabase::CreateObjectStore(const nsAString& aName,
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
}
if (!keyPath.IsVoid() && !IDBObjectStore::IsValidKeyPath(aCx, keyPath)) {
return NS_ERROR_DOM_SYNTAX_ERR;
if (!keyPath.IsVoid()) {
if (keyPath.IsEmpty() && autoIncrement) {
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
}
if (!IDBObjectStore::IsValidKeyPath(aCx, keyPath)) {
return NS_ERROR_DOM_SYNTAX_ERR;
}
}
nsAutoPtr<ObjectStoreInfo> newInfo(new ObjectStoreInfo());

Просмотреть файл

@ -915,7 +915,7 @@ IDBObjectStore::GetAddInfo(JSContext* aCx,
rv = aKey.SetFromJSVal(aCx, aKeyVal);
NS_ENSURE_SUCCESS(rv, rv);
}
else {
else if (!mAutoIncrement) {
// Inline keys live on the object. Make sure that the value passed in is an
// object.
rv = GetKeyFromValue(aCx, aValue, mKeyPath, aKey);
@ -943,32 +943,107 @@ IDBObjectStore::GetAddInfo(JSContext* aCx,
indexInfo.unique, indexInfo.multiEntry,
aCx, aValue, aUpdateInfoArray);
NS_ENSURE_SUCCESS(rv, rv);
}
const jschar* keyPathChars =
reinterpret_cast<const jschar*>(mKeyPath.get());
const size_t keyPathLen = mKeyPath.Length();
JSBool ok = JS_FALSE;
nsString targetObjectPropName;
JSObject* targetObject = nsnull;
rv = NS_OK;
if (mAutoIncrement && HasKeyPath()) {
NS_ASSERTION(aKey.IsUnset(), "Shouldn't have gotten the key yet!");
if (HasKeyPath() && aKey.IsUnset()) {
NS_ASSERTION(mAutoIncrement, "Should have bailed earlier!");
if (JSVAL_IS_PRIMITIVE(aValue)) {
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
}
JSObject* obj = JS_NewObject(aCx, &gDummyPropClass, nsnull, nsnull);
NS_ENSURE_TRUE(obj, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
KeyPathTokenizer tokenizer(mKeyPath, '.');
NS_ASSERTION(tokenizer.hasMoreTokens(),
"Shouldn't have empty keypath and autoincrement");
jsval key = OBJECT_TO_JSVAL(obj);
ok = JS_DefineUCProperty(aCx, JSVAL_TO_OBJECT(aValue), keyPathChars,
keyPathLen, key, nsnull, nsnull,
JSPROP_ENUMERATE);
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
JSObject* obj = JSVAL_TO_OBJECT(aValue);
while (tokenizer.hasMoreTokens()) {
const nsDependentSubstring& token = tokenizer.nextToken();
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
const jschar* keyPathChars = token.BeginReading();
const size_t keyPathLen = token.Length();
JSBool hasProp;
if (!targetObject) {
// We're still walking the chain of existing objects
// From this point on we have to try to remove the property.
JSBool ok = JS_HasUCProperty(aCx, obj, keyPathChars, keyPathLen,
&hasProp);
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
if (hasProp) {
// Get if the property exists...
jsval intermediate;
JSBool ok = JS_GetUCProperty(aCx, obj, keyPathChars, keyPathLen,
&intermediate);
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
if (tokenizer.hasMoreTokens()) {
// ...and walk to it if there are more steps...
if (JSVAL_IS_PRIMITIVE(intermediate)) {
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
}
obj = JSVAL_TO_OBJECT(intermediate);
}
else {
// ...otherwise use it as key
aKey.SetFromJSVal(aCx, intermediate);
if (aKey.IsUnset()) {
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
}
}
}
else {
// If the property doesn't exist, fall into below path of starting
// to define properties
targetObject = obj;
targetObjectPropName = token;
}
}
if (targetObject) {
// We have started inserting new objects or are about to just insert
// the first one.
if (tokenizer.hasMoreTokens()) {
// If we're not at the end, we need to add a dummy object to the chain.
JSObject* dummy = JS_NewObject(aCx, nsnull, nsnull, nsnull);
if (!dummy) {
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
break;
}
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
token.Length(),
OBJECT_TO_JSVAL(dummy), nsnull, nsnull,
JSPROP_ENUMERATE)) {
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
break;
}
obj = dummy;
}
else {
JSObject* dummy = JS_NewObject(aCx, &gDummyPropClass, nsnull, nsnull);
if (!dummy) {
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
break;
}
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
token.Length(), OBJECT_TO_JSVAL(dummy),
nsnull, nsnull, JSPROP_ENUMERATE)) {
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
break;
}
}
}
}
}
JSStructuredCloneCallbacks callbacks = {
@ -986,13 +1061,15 @@ IDBObjectStore::GetAddInfo(JSContext* aCx,
rv = NS_ERROR_DOM_DATA_CLONE_ERR;
}
if (ok) {
if (targetObject) {
// If this fails, we lose, and the web page sees a magical property
// appear on the object :-(
jsval succeeded;
ok = JS_DeleteUCProperty2(aCx, JSVAL_TO_OBJECT(aValue), keyPathChars,
keyPathLen, &succeeded);
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
if (!JS_DeleteUCProperty2(aCx, targetObject,
targetObjectPropName.get(),
targetObjectPropName.Length(), &succeeded)) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
NS_ASSERTION(JSVAL_IS_BOOLEAN(succeeded), "Wtf?");
NS_ENSURE_TRUE(JSVAL_TO_BOOLEAN(succeeded),
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);

Просмотреть файл

@ -50,9 +50,10 @@
{ keyPath: ".bar", exception: true },
];
let request = mozIndexedDB.open(name, 1);
request.onerror = errorHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
let openRequest = mozIndexedDB.open(name, 1);
openRequest.onerror = errorHandler;
openRequest.onupgradeneeded = grabEventAndContinueHandler;
openRequest.onsuccess = unexpectedSuccessHandler;
let event = yield;
let db = event.target.result;
@ -61,15 +62,15 @@
// Test creating object stores and inserting data
for (let i = 0; i < keyPaths.length; i++) {
let info = keyPaths[i];
test = " for " + JSON.stringify(info);
let test = " for objectStore test " + JSON.stringify(info);
if (!stores[info.keyPath]) {
try {
let objectStore = db.createObjectStore(info.keyPath, { keyPath: info.keyPath });
ok(!("exception" in info), "expected exception behavior observed" + test);
is(objectStore.keyPath, info.keyPath, "objectStore has correct keyPath property" + test);
ok(!("exception" in info), "shouldn't throw" + test);
is(objectStore.keyPath, info.keyPath, "correct keyPath property" + test);
stores[info.keyPath] = objectStore;
} catch (e) {
ok("exception" in info, "expected exception behavior observed" + test);
ok("exception" in info, "should throw" + test);
ok(e instanceof DOMException, "Got a DOM Exception" + test);
is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
continue;
@ -104,17 +105,18 @@
let store = db.createObjectStore("indexStore");
let indexes = {};
for (let i = 0; i < keyPaths.length; i++) {
let test = " for index test " + JSON.stringify(info);
let info = keyPaths[i];
if (!indexes[info.keyPath]) {
try {
let index = store.createIndex(info.keyPath, info.keyPath);
ok(!("exception" in info), "expected exception behavior observed");
is(index.keyPath, info.keyPath, "index has correct keyPath property");
ok(!("exception" in info), "shouldn't throw" + test);
is(index.keyPath, info.keyPath, "index has correct keyPath property" + test);
indexes[info.keyPath] = index;
} catch (e) {
ok("exception" in info, "expected exception behavior observed");
ok(e instanceof DOMException, "Got a DOM Exception");
is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error");
ok("exception" in info, "should throw" + test);
ok(e instanceof DOMException, "Got a DOM Exception" + test);
is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
continue;
}
}
@ -125,18 +127,86 @@
if ("key" in info) {
index.getKey(info.key).onsuccess = grabEventAndContinueHandler;
e = yield;
is(e.target.result, 1, "found value when reading from index");
is(e.target.result, 1, "found value when reading" + test);
}
else {
index.count().onsuccess = grabEventAndContinueHandler;
e = yield;
is(e.target.result, 0, "index should be empty");
is(e.target.result, 0, "should be empty" + test);
}
store.clear().onsuccess = grabEventAndContinueHandler;
yield;
}
// Autoincrement and complex key paths
let aitests = [{ v: {}, k: 1, res: { foo: { id: 1 }} },
{ v: { value: "x" }, k: 2, res: { value: "x", foo: { id: 2 }} },
{ v: { value: "x", foo: {} }, k: 3, res: { value: "x", foo: { id: 3 }} },
{ v: { v: "x", foo: { x: "y" } }, k: 4, res: { v: "x", foo: { x: "y", id: 4 }} },
{ v: { value: 2, foo: { id: 10 }}, k: 10 },
{ v: { value: 2 }, k: 11, res: { value: 2, foo: { id: 11 }} },
{ v: true, },
{ v: { value: 2, foo: 12 }, },
{ v: { foo: { id: true }}, },
{ v: { foo: { x: 5, id: {} }}, },
{ v: undefined, },
{ v: { foo: undefined }, },
{ v: { foo: { id: undefined }}, },
{ v: null, },
{ v: { foo: null }, },
{ v: { foo: { id: null }}, },
];
store = db.createObjectStore("gen", { keyPath: "foo.id", autoIncrement: true });
for (let i = 0; i < aitests.length; ++i) {
let info = aitests[i];
let test = " for autoIncrement test " + JSON.stringify(info);
let preValue = JSON.stringify(info.v);
if ("k" in info) {
store.add(info.v).onsuccess = grabEventAndContinueHandler;
is(JSON.stringify(info.v), preValue, "put didn't modify value" + test);
}
else {
try {
store.add(info.v);
ok(false, "should throw" + test);
}
catch(e) {
ok(true, "did throw" + test);
ok(e instanceof IDBDatabaseException, "Got a IDBDatabaseException" + test);
is(e.code, IDBDatabaseException.DATA_ERR, "expect a DATA_ERR" + test);
is(JSON.stringify(info.v), preValue, "failing put didn't modify value" + test);
continue;
}
}
let e = yield;
is(e.target.result, info.k, "got correct return key" + test);
store.get(info.k).onsuccess = grabEventAndContinueHandler;
e = yield;
is(JSON.stringify(e.target.result), JSON.stringify(info.res || info.v),
"expected value stored" + test);
}
// Can't handle autoincrement and empty keypath
try {
store = db.createObjectStore("storefail", { keyPath: "", autoIncrement: true });
ok(false, "Should have thrown when creating empty-keypath autoincrement store");
}
catch(e) {
ok(true, "Did throw when creating empty-keypath autoincrement store");
ok(e instanceof DOMException, "Got a DOMException when creating empty-keypath autoincrement store");
is(e.code, DOMException.INVALID_ACCESS_ERR, "expect a INVALID_ACCESS_ERR when creating empty-keypath autoincrement store");
}
openRequest.onsuccess = grabEventAndContinueHandler;
yield;
finishTest();
yield;
}