diff --git a/polyfill/src/IDBIndex.js b/polyfill/src/IDBIndex.js index 180bbb9..7baebd9 100644 --- a/polyfill/src/IDBIndex.js +++ b/polyfill/src/IDBIndex.js @@ -95,16 +95,41 @@ return cursorRequest; }; - IDBIndex.prototype.get = function(key){ + IDBIndex.prototype.__fetchIndexData = function(key, opType){ + var me = this; + return me.__idbObjectStore.transaction.__addToTransactionQueue(function(tx, args, success, error){ + var sql = ["SELECT * FROM ", me.__idbObjectStore.name, " WHERE", me.indexName, "NOT NULL"]; + var sqlValues = []; + if (typeof key !== "undefined") { + sql.push("AND", me.indexName, " = ?"); + sqlValues.push(idbModules.Key.encode("key")); + } + tx.executeSql(sql.join(" "), sqlValues, function(tx, data){ + var d; + if (typeof opType === "count") { + d = data.rows.length; + } else if (data.rows.length === 0) { + d = undefined; + } else if (opType === "key") { + d = idbModules.Key.decode(data.rows.item(0).key); + } else { // when opType is value + d = idbModules.Sca.decode(data.rows.item(0).value); + } + success(d); + }, error); + }); + } + IDBIndex.prototype.get = function(key){ + return this.__fetchIndexData(key, "value"); }; IDBIndex.prototype.getKey = function(key){ - + return this.__fetchIndexData(key, "key"); }; IDBIndex.prototype.count = function(key){ - + return this.__fetchIndexData(key, "count"); }; idbModules["IDBIndex"] = IDBIndex; diff --git a/polyfill/src/IDBObjectStore.js b/polyfill/src/IDBObjectStore.js index 1349c4e..34008f6 100644 --- a/polyfill/src/IDBObjectStore.js +++ b/polyfill/src/IDBObjectStore.js @@ -207,7 +207,7 @@ tx.executeSql("SELECT * FROM " + me.name + " where key = ?", [primaryKey], function(tx, data){ console.log("Fetched data", data.rows.item(0)); try { - success(JSON.parse(data.rows.item(0).value)); + success(idbModules.Sca.decode(data.rows.item(0).value)); } catch (e) { console.log(e) // If no result is returned, or error occurs when parsing JSON diff --git a/polyfill/test/IndexTests.js b/polyfill/test/IndexTests.js index e99ece7..becbac7 100644 --- a/polyfill/test/IndexTests.js +++ b/polyfill/test/IndexTests.js @@ -49,9 +49,10 @@ function openObjectStore(name, storeName, callback){ }); } +var key = sample.integer(); +var value = sample.obj() openObjectStore("Adding data after index is created", DB.OBJECT_STORE_1, function(objectStore){ - var key = sample.integer(); - var addReq = objectStore.add(sample.obj(), key); + var addReq = objectStore.add(value, key); addReq.onsuccess = function(e){ equal(key, addReq.result, "Data successfully added"); _("Added to datastore with index " + key); @@ -64,7 +65,6 @@ openObjectStore("Adding data after index is created", DB.OBJECT_STORE_1, functio nextTest(); }; }); - openObjectStore("Index Cursor", DB.OBJECT_STORE_1, function(objectStore){ var index = objectStore.index("IntIndex"); var indexCursorReq = index.openCursor(); @@ -86,7 +86,6 @@ openObjectStore("Index Cursor", DB.OBJECT_STORE_1, function(objectStore){ }; }); - openObjectStore("Index Key Cursor", DB.OBJECT_STORE_1, function(objectStore){ var index = objectStore.index("IntIndex"); var indexCursorReq = index.openKeyCursor(); @@ -107,3 +106,20 @@ openObjectStore("Index Key Cursor", DB.OBJECT_STORE_1, function(objectStore){ nextTest(); }; }); + +openObjectStore("Index Get", DB.OBJECT_STORE_1, function(objectStore){ + var index = objectStore.index("IntIndex"); + var req = index.get(value.Int); + req.onsuccess = function(){ + equal(req.result, value, "Got key from Index Get"); + _("Got " + req.result + " from index get"); + start(); + nextTest(); + }; + req.onerror = function(){ + _("Error on cursor request") + ok(false, "Could not continue opening cursor"); + start(); + nextTest(); + }; +});