Skip to content

Instantly share code, notes, and snippets.

@jonjenkins
Forked from tomger/webdatabase.sjs
Created April 19, 2011 14:16
Show Gist options
  • Save jonjenkins/927981 to your computer and use it in GitHub Desktop.
Save jonjenkins/927981 to your computer and use it in GitHub Desktop.
/*
Original: A simple stratified wrapper over the asynchronous webdatabase api (webkit)
from http://gist.github.com/613526
Existing openDatabase and executeSql methods
Added executeBulkSql as a method to group sql statements prior to transaction-level commit;
this is much faster for bulk transactions as opposed to individual transaction executions
from http://gist.github.com/927981
*/
exports.openDatabase = function (name, version, desc, size) {
var db = openDatabase(name, version, desc, size);
return {
executeBulkSql: function (arrSql, params) {
var rv;
this.transaction(function(tx){
for (var i = 0, l = arrSql.length; i < l; i++)
rv = tx.executeSql(arrSql[i], params);
});
return rv;
},
executeSql: function (sql, params) {
var rv;
this.transaction(function(tx){
rv = tx.executeSql(sql, params);
});
return rv;
},
transaction: function (cb) {
var error;
waitfor() {
db.transaction(function(t) {
var tx = {
executeSql: function (sql, params) {
waitfor(var dummytx, rv) {
t.executeSql(sql, params, resume);
}
return rv;
}
}
cb(tx);
resume();
}, function (f) {
error = f;
resume();
});
}
if (error) throw error;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment