Skip to content
Browse files

Added more tests, started work on transactions.

  • Loading branch information...
1 parent 7b50cf2 commit b7a2d94ad79eba3c783340514c9b8c99d59bd0d1 Parashuram committed
Showing with 157 additions and 30 deletions.
  1. +27 −0 polyfill/index.html
  2. +103 −17 polyfill/indexeddb.sqlshim.js
  3. +27 −13 polyfill/test.html
View
27 polyfill/index.html
@@ -31,6 +31,7 @@
};
deleteReq.onerror = function(e){
console.log("Could not delete database");
+ startQUnitTests();
};
};
@@ -135,6 +136,32 @@
stop();
};
});
+
+ module("Transactions")
+ asyncTest("Put object in a transaction", function(){
+ var dbOpenRequest = window.indexedDB.open(DB.NAME);
+ dbOpenRequest.onsuccess = function(e){
+ ok(true, "Database Opened successfully");
+ var db = dbOpenRequest.result;
+ var transaction = db.transaction([DB.OBJECT_STORE_1, DB.OBJECT_STORE_2]);
+ var objectStore = transaction.objectStore(DB.OBJECT_STORE_1);
+ var addReq = objectStore.add(sample.obj());
+ addReq.onsuccess = function(){
+
+ };
+ addReq.onerror = function(){
+
+ };
+
+ _("Database opened successfully with version");
+ start();
+ };
+ dbOpenRequest.onerror = function(e){
+ ok(false, "Database NOT Opened successfully");
+ _("Database NOT opened successfully");
+ start();
+ };
+ });
};
</script>
</head>
View
120 polyfill/indexeddb.sqlshim.js
@@ -86,18 +86,77 @@ window.indexedDB = (function(window, undefined){
*/
var IDBTransaction = function(storeNames, mode, db){
this.mode = mode;
- this.storeNames = storeNames;
+ this.storeNames = typeof storeNames === "string" ? [storeNames] : storeNames;
+ for (var i = 0; i < this.storeNames.length; i++) {
+ if (db.objectStoreNames.indexOf(storeNames[i]) === -1) {
+ throwDOMException(0, "The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.", storeNames);
+ }
+ }
+ this.__closePending = null;
+ this.__active = true;
+ this.__requests = [];
+ this.__aborted = false;
+ var me = this;
+ window.setTimeout(function(){
+ !me.__active && throwDOMException(0, "A request was placed against a transaction which is currently not active, or which is finished", me.__active);
+ me.db.transaction(function(tx){
+ me.__tx = tx;
+ try {
+ (function executeRequest(i){
+ if (i >= me.__requests.length) {
+ // All requests in the transaction is done
+ me.__active = false;
+ return;
+ }
+ var q = me.__requests(i)
+ q.op(q["args"], {
+ "success": function(res, e){
+ q.req.readyState = "done";
+ q.result = res
+ delete q.req.error;
+ callback("onsuccess", q.req, [e]);
+ executeRequest(i + 1)
+ },
+ "error": function(){
+
+ }
+ });
+ }(me.__requests[0]));
+ } catch (e) {
+
+ }
+ }, function(){
+ // Error callback
+ }, function(){
+ // Transaction completed
+ });
+
+ me.__active = false;
+ }, 1);
this.db = db;
this.error = null;
this.onabort = this.onerror = this.oncomplete = null;
};
+ IDBTransaction.prototype.__addToTransactionQueue = function(callback, args){
+ !this.__active && throwDOMException(0, "A request was placed against a transaction which is currently not active, or which is finished.", this.__active);
+ var request = new IDBRequest;
+ request.source = this.db;
+ this.__requests.push({
+ "op": callback,
+ "args": args,
+ "req": request
+ });
+ return request;
+ };
+
IDBTransaction.prototype.objectStore = function(objectStoreName){
return new IDBObjectStore(objectStoreName, this);
};
IDBTransaction.prototype.abort = function(){
-
+ !me.__active && throwDOMException(0, "A request was placed against a transaction which is currently not active, or which is finished", me.__active);
+
};
window.IDBTransaction = window.IDBTransaction || {};
@@ -117,17 +176,32 @@ window.indexedDB = (function(window, undefined){
this.__ready = (typeof ready === "undefined") ? true : false;
};
+ /**
+ * Gets (and optionally caches) the properties like keyPath, autoincrement, etc for this objectStore
+ * @param {Object} callback
+ */
IDBObjectStore.prototype.__getStoreProps = function(callback){
var me = this;
- this.transaction.db.__db.transaction(function(tx){
- tx.executeSql("SELECT * FROM __sys__ where name = ?", [me.name], function(tx, data){
- callback(data.rows.item(0));
- }, function(){
- callback();
+ if (me.__storeProps) {
+ callback(me.__storeProps);
+ } else {
+ this.transaction.db.__db.transaction(function(tx){
+ tx.executeSql("SELECT * FROM __sys__ where name = ?", [me.name], function(tx, data){
+ me.__storeProps = data.rows.item(0)
+ callback(me.__storeProps);
+ }, function(){
+ callback();
+ });
});
- });
+ }
};
+ /**
+ * From the store properties and object, extracts the value for the key in hte object Store
+ * @param {Object} props
+ * @param {Object} value
+ * @param {Object} key
+ */
IDBObjectStore.prototype.__getKey = function(props, value, key){
if (!props) throwDOMException(0, "Data Error - Could not locate defination for this table", props);
if (!props.keyPath && !key) throwDOMException(0, "Data Error - keyPath was defined as null, but a key was provided", props);
@@ -141,14 +215,26 @@ window.indexedDB = (function(window, undefined){
}
};
- IDBObjectStore.prototype.put = function(value, key){
- var idbRequest = new IDBRequest();
- var e = new Event();
+ /**
+ * Checks if an insert is allowed in this object
+ * @param {Object} props
+ * @param {Object} value
+ * @param {Object} key
+ */
+ IDBObjectStore.prototype.__getKey = function(props, value, key){
+ //TODO Check insert constraints
+ }
+
+ IDBObjectStore.prototype.add = function(value, key){
var me = this;
- this.__getStoreProps(function(props){
- me.__checkInsert(props, value, key);
- me.transaction.db.__db.transaction(function(tx){
- tx.executeSql("")
+ return me.transaction.__addToTransactionQueue(function(tx, args){
+ this.__getStoreProps(function(props){
+ me.__checkInsert(props, value, key);
+ tx.executeSql(function(){
+
+ }, function(){
+
+ });
});
});
};
@@ -221,8 +307,8 @@ window.indexedDB = (function(window, undefined){
};
IDBDatabase.prototype.transaction = function(storeNames, mode){
- this.transaction = new IDBTransaction(storeNames, mode, this);
- return this.transaction;
+ var transaction = new IDBTransaction(storeNames, mode, this);
+ return transaction;
};
return {
View
40 polyfill/test.html
@@ -9,19 +9,33 @@
<script type = "text/javascript" src = "sampleData.js">
</script>
<script type = "text/javascript">
- function _(msg){
- console.log(msg, arguments.callee.caller.arguments);
- }
-
- var db = window.openDatabase("sampleDB", 1, "Sample Database", 10000);
- db.transaction(function(tx){
- tx.executeSql("Drop table obj1");
- tx.executeSql("Create table obj1 (key PRIMARY KEY INTEGER AUTOINCREMENT NOT NULL UNIQUE, value varchar(200))", [], function(){
- console.log("Success", this, arguments);
- }, function(){
- console.log("Error", this, arguments);
- })
- });
+ function _(msg){
+ console.log(msg, arguments.callee.caller.arguments);
+ }
+
+ function a(tx){
+ tx.executeSql("INSERT INTO obj1 (value) values ('Sample" + Math.random() + "')", [], function(){
+ console.log("Added value");
+ }, function(){
+ console.log("Error", this, arguments);
+ });
+ }
+
+ function b(tx){
+ //throw "Aborting transaction";
+ }
+
+ var db = window.openDatabase("sampleDB", 1, "Sample Database", 10000);
+ db.transaction(function(tx){
+ a(tx);
+ console.log("A completed");
+ b(tx);
+ console.log("B completed");
+ }, function(){
+ console.log("Error Callback")
+ }, function(){
+ console.log("SUccess callback");
+ });
</script>
</body>
</html>

0 comments on commit b7a2d94

Please sign in to comment.
Something went wrong with that request. Please try again.