Skip to content

Commit

Permalink
Added Index get, getKey and Count
Browse files Browse the repository at this point in the history
  • Loading branch information
Parashuram committed Jun 4, 2012
1 parent 2e1c460 commit cb3d262
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
31 changes: 28 additions & 3 deletions polyfill/src/IDBIndex.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion polyfill/src/IDBObjectStore.js
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions polyfill/test/IndexTests.js
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
};
});

0 comments on commit cb3d262

Please sign in to comment.