-
Notifications
You must be signed in to change notification settings - Fork 21
/
WebSQL.min.js
10 lines (10 loc) · 3.91 KB
/
WebSQL.min.js
1
2
3
4
5
6
7
8
9
10
/* WebSQL (v0.2) Paul Sayre */
(function(p){var l=p.WebSQL=function(a,c,b,h,m){var n=p.openDatabase&&p.openDatabase(a,c||"1.0",b||a,h||5E6,m),q=n&&{query:function(b){var a=l.Deferred(),h=e(b)?b:arguments;q.rawTx(function(b){var c=l.Deferred(),g,d,k,f,r,s,m,n=c.reject;f=0;for(r=h.length;f<r;f++)if(g=h[f],d=h[f+1],"function"===typeof g&&(g=g.toString(),g=g.substr(g.indexOf("/*!")+3),g=g.substr(0,g.lastIndexOf("*/"))),(k=/^\s*(?:INSERT|REPLACE)\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(g))&&k[1]&&(g+=" VALUES ("+Array(k[1].split(",").length).join("?,")+
"?)"),e(d))if(f+=1,e(d[0]))for(k=0,s=d.length;k<s;k++)f+1===r&&k+1===s&&(m=c.resolve),b.executeSql(g,d[k],m,n);else f+1===r&&(m=c.resolve),b.executeSql(g,d,m,n);else f+1===r&&(m=c.resolve),b.executeSql(g,[],m,n);c.fail(a.reject).done(function(b,g){var d=null,f,c;if(g){if(c=g.rows)for(d=[],f=0;f<c.length;f++)d[f]=c.item(f);if(d&&0===d.length)try{d.insertId=g.insertId}catch(h){d.insertId=null}else d.insertId=null}a.resolve(d)})});return a.promise()},rawTx:function(b){n.transaction(b)},getTableNames:function(){var b=
l.Deferred();q.query('SELECT tbl_name FROM sqlite_master WHERE type = "table" AND tbl_name NOT REGEXP "^(__|sqlite_).*"').fail(b.reject).done(function(c){var a,h=[];for(a=0;a<c.length;a++)h[a]=c[a].tbl_name;b.resolve(h)});return b.promise()},dump:function(b,a){var c=l.Deferred();a=!1!==a;switch(b){default:q.query('SELECT * FROM sqlite_master WHERE tbl_name NOT REGEXP "^(__|sqlite_).*" ORDER BY CASE type WHEN "table" THEN 1 WHEN "index" THEN 2 ELSE 3 END').fail(c.reject).done(function(b){var h={},
g=[],d,k;for(k=0;d=b[k];k++)if(d.sql)switch(d.type){case "table":h[d.tbl_name]={schema:{table:d.sql,indexes:[]}};a&&function(b){var a=l.Deferred();q.query("SELECT * FROM "+b).fail(a.reject).done(function(c){delete c.insertId;h[b].data=c;a.resolve()});g.push(a)}(d.tbl_name);break;case "index":h[d.tbl_name].schema.indexes.push(d.sql)}l.when.apply(l,g).fail(c.reject).done(function(){c.resolve(h)})});break;case "sql":q.dump("json",a).fail(c.reject).done(function(b){var a=[],h,d,k,f,m,e,n,l;for(l in b)if(Object.prototype.hasOwnProperty.call(b,
l)&&(h=b[l],a.push(h.schema.table),a=a.concat(h.schema.indexes),h.data&&0<h.data.length)){m=[];d=h.data[0];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&m.push(/\s/.test(f)?"`"+f+"`":f);m=m.join(", ");e=[];for(k=0;d=h.data[k];k++){e[k]=[];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&(n="number"===typeof d[f]?d[f]:null===d[f]||void 0===d[f]?"NULL":'"'+d[f]+'"',e[k].push(n));e[k]="("+e[k].join(", ")+")"}e=e.join(",\n\t");a.push("INSERT INTO "+l+" ("+m+") VALUES\n\t"+e)}a=a.join(";\n")+
";";c.resolve(a)})}return c.promise()}};return q};l.VERSION="0.2";var e=Array.isArray||function(a){return!(!a||a.constructor!==Array)}})(this);
(function(p){p.Deferred=function(){var e,a=l(),c=l(),b={promise:function(){var a={},c=function(b){return function(){b.apply(this,arguments);return a}};a.always=c(b.always);a.done=c(b.done);a.fail=c(b.fail);a.then=c(b.then);a.state=b.state;return a},state:function(){return e||"pending"},resolve:function(){e||(e="resolved",a.fire.apply(a,arguments));return b},reject:function(){e||(e="rejected",c.fire.apply(c,arguments));return b},always:function(a){return b.then(a,a)},done:function(c){a.add(c);return b},
fail:function(a){c.add(a);return b},then:function(a,c){return b.done(a).fail(c)}};return b};p.when=function(e){var a,c=Array(arguments.length),b=p.Deferred();for(a=0;a<arguments.length;a++)arguments[a].fail(function(){b.reject.apply(this,arguments);c=null});if(0<arguments.length)for(a=0;a<arguments.length;a++)(function(a,e){e.done(function(){c[a]=arguments;for(var e=0;e<c.length&&void 0!==c[e];e++);e===c.length&&b.resolve.apply(b,c)})})(a,arguments[a]);else b.resolve.apply(b,c);return b.promise()};
var l=function(){var e=[],a,c={hasFired:function(){return!!a},add:function(b){e.push(b);c.hasFired()&&c.fire.apply(c,a)},fire:function(){a=arguments;for(var b=0;b<e.length;b++)e[b].apply(c,a);e=[]}};return c}})(WebSQL);