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