Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added REPLACE to auto included ?

  • Loading branch information...
commit 3fb631da2fe1491aaf57220157d8f61e5d8dbe15 1 parent 0a9a883
@psayre23 authored
View
8 WebSQL.js
@@ -1,6 +1,6 @@
/* WebSQL (v0.1) Paul Sayre */
(function (context) {
-
+ var VERSION = '0.2';
// Public object
var pub = context.WebSQL = function (name, ver, desc, size, cb) {
@@ -38,7 +38,7 @@
}
// Add ? for fields in insert
- parts = /^\s*INSERT\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
+ parts = /^\s*(?:INSERT|REPLACE)\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
if(parts && parts[1]) {
sql += ' VALUES ('+(new Array(parts[1].split(',').length)).join('?,')+'?)';
}
@@ -251,6 +251,8 @@
};
+ pub.VERSION = VERSION;
+
// Test if an argument is an array
var isArray = Array.isArray || function (arg) {
@@ -263,7 +265,7 @@
};
-})(window);
+})(this);
(function (pub) {
View
16 WebSQL.min.js
@@ -1,10 +1,10 @@
/* WebSQL (v0.1) Paul Sayre */
-(function(m){var k=m.WebSQL=function(a,c,b,h,j){var l=m.openDatabase&&m.openDatabase(a,c||"1.0",b||a,h||5E6,j),n=l&&{query:function(b){var a=k.Deferred(),h=e(b)?b:arguments;n.rawTx(function(b){var c=k.Deferred(),g,d,i,f,o,p,j,l=c.reject;f=0;for(o=h.length;f<o;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("*/"))),(i=/^\s*INSERT\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(g))&&i[1]&&(g+=" VALUES ("+Array(i[1].split(",").length).join("?,")+
-"?)"),e(d))if(f+=1,e(d[0])){i=0;for(p=d.length;i<p;i++)f+1===o&&i+1===p&&(j=c.resolve),b.executeSql(g,d[i],j,l)}else f+1===o&&(j=c.resolve),b.executeSql(g,d,j,l);else f+1===o&&(j=c.resolve),b.executeSql(g,[],j,l);c.fail(a.reject).done(function(b,g){var d=null,f,c;if(g){if(c=g.rows){d=[];for(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){l.transaction(b)},getTableNames:function(){var b=
-$.Deferred();n.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=k.Deferred(),a=!1!==a;switch(b){default:n.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,i;for(i=0;d=b[i];i++)if(d.sql)switch(d.type){case "table":h[d.tbl_name]={schema:{table:d.sql,indexes:[]}};a&&function(b){var a=k.Deferred();n.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)}k.when.apply(k,g).fail(c.reject).done(function(){c.resolve(h)})});break;case "sql":n.dump("json",a).fail(c.reject).done(function(b){var a=[],h,d,i,f,j,e,l,k;for(k in b)if(Object.prototype.hasOwnProperty.call(b,
-k)&&(h=b[k],a.push(h.schema.table),a=a.concat(h.schema.indexes),h.data&&0<h.data.length)){j=[];d=h.data[0];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&j.push(/\s/.test(f)?"`"+f+"`":f);j=j.join(", ");e=[];for(i=0;d=h.data[i];i++){e[i]=[];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&(l="number"===typeof d[f]?d[f]:null===d[f]||void 0===d[f]?"NULL":'"'+d[f]+'"',e[i].push(l));e[i]="("+e[i].join(", ")+")"}e=e.join(",\n\t");a.push("INSERT INTO "+k+" ("+j+") VALUES\n\t"+e)}a=a.join(";\n")+
-";";c.resolve(a)})}return c.promise()}};return n},e=Array.isArray||function(a){return!!(a&&a.constructor===Array)}})(window);
-(function(m){m.Deferred=function(){var e,a=k(),c=k(),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},
+(function(m){var j=m.WebSQL=function(a,c,b,h,k){var l=m.openDatabase&&m.openDatabase(a,c||"1.0",b||a,h||5E6,k),n=l&&{query:function(b){var a=j.Deferred(),h=e(b)?b:arguments;n.rawTx(function(b){var c=j.Deferred(),g,d,i,f,o,p,k,l=c.reject;f=0;for(o=h.length;f<o;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("*/"))),(i=/^\s*(?:INSERT|REPLACE)\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(g))&&i[1]&&(g+=" VALUES ("+Array(i[1].split(",").length).join("?,")+
+"?)"),e(d))if(f+=1,e(d[0])){i=0;for(p=d.length;i<p;i++)f+1===o&&i+1===p&&(k=c.resolve),b.executeSql(g,d[i],k,l)}else f+1===o&&(k=c.resolve),b.executeSql(g,d,k,l);else f+1===o&&(k=c.resolve),b.executeSql(g,[],k,l);c.fail(a.reject).done(function(b,g){var d=null,f,c;if(g){if(c=g.rows){d=[];for(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){l.transaction(b)},getTableNames:function(){var b=
+$.Deferred();n.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=j.Deferred(),a=!1!==a;switch(b){default:n.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,i;for(i=0;d=b[i];i++)if(d.sql)switch(d.type){case "table":h[d.tbl_name]={schema:{table:d.sql,indexes:[]}};a&&function(b){var a=j.Deferred();n.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)}j.when.apply(j,g).fail(c.reject).done(function(){c.resolve(h)})});break;case "sql":n.dump("json",a).fail(c.reject).done(function(b){var a=[],h,d,i,f,k,e,l,j;for(j in b)if(Object.prototype.hasOwnProperty.call(b,
+j)&&(h=b[j],a.push(h.schema.table),a=a.concat(h.schema.indexes),h.data&&0<h.data.length)){k=[];d=h.data[0];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&k.push(/\s/.test(f)?"`"+f+"`":f);k=k.join(", ");e=[];for(i=0;d=h.data[i];i++){e[i]=[];for(f in d)Object.prototype.hasOwnProperty.call(d,f)&&(l="number"===typeof d[f]?d[f]:null===d[f]||void 0===d[f]?"NULL":'"'+d[f]+'"',e[i].push(l));e[i]="("+e[i].join(", ")+")"}e=e.join(",\n\t");a.push("INSERT INTO "+j+" ("+k+") VALUES\n\t"+e)}a=a.join(";\n")+
+";";c.resolve(a)})}return c.promise()}};return n};j.VERSION="0.2";var e=Array.isArray||function(a){return!!(a&&a.constructor===Array)}})(this);
+(function(m){m.Deferred=function(){var e,a=j(),c=j(),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};m.when=function(e){var a,c=Array(arguments.length),b=m.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 k=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);
+var j=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);
View
8 jquery.websql.js
@@ -1,6 +1,6 @@
/* WebSQL (v0.1) Paul Sayre */
(function (context) {
-
+ var VERSION = '0.2';
// Public object
var pub = context.WebSQL = function (name, ver, desc, size, cb) {
@@ -38,7 +38,7 @@
}
// Add ? for fields in insert
- parts = /^\s*INSERT\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
+ parts = /^\s*(?:INSERT|REPLACE)\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
if(parts && parts[1]) {
sql += ' VALUES ('+(new Array(parts[1].split(',').length)).join('?,')+'?)';
}
@@ -251,6 +251,8 @@
};
+ pub.VERSION = VERSION;
+
// Test if an argument is an array
var isArray = Array.isArray || function (arg) {
@@ -263,7 +265,7 @@
};
-})(window);
+})(this);
(function ($, pub) {
pub.when = $.when;
View
12 jquery.websql.min.js
@@ -1,7 +1,7 @@
/* WebSQL (v0.1) Paul Sayre */
-(function(l){var h=l.WebSQL=function(q,s,t,u,v){var r=l.openDatabase&&l.openDatabase(q,s||"1.0",t||q,u||5E6,v),m=r&&{query:function(e){var g=h.Deferred(),i=p(e)?e:arguments;m.rawTx(function(n){var f=h.Deferred(),c,a,d,b,o,j,e,k=f.reject;b=0;for(o=i.length;b<o;b++)if(c=i[b],a=i[b+1],"function"===typeof c&&(c=c.toString(),c=c.substr(c.indexOf("/*!")+3),c=c.substr(0,c.lastIndexOf("*/"))),(d=/^\s*INSERT\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(c))&&d[1]&&(c+=" VALUES ("+Array(d[1].split(",").length).join("?,")+
-"?)"),p(a))if(b+=1,p(a[0])){d=0;for(j=a.length;d<j;d++)b+1===o&&d+1===j&&(e=f.resolve),n.executeSql(c,a[d],e,k)}else b+1===o&&(e=f.resolve),n.executeSql(c,a,e,k);else b+1===o&&(e=f.resolve),n.executeSql(c,[],e,k);f.fail(g.reject).done(function(c,a){var b=null,d,f;if(a){if(f=a.rows){b=[];for(d=0;d<f.length;d++)b[d]=f.item(d)}if(b&&0===b.length)try{b.insertId=a.insertId}catch(e){b.insertId=null}else b.insertId=null}g.resolve(b)})});return g.promise()},rawTx:function(e){r.transaction(e)},getTableNames:function(){var e=
-$.Deferred();m.query('SELECT tbl_name FROM sqlite_master WHERE type = "table" AND tbl_name NOT REGEXP "^(__|sqlite_).*"').fail(e.reject).done(function(g){var i,n=[];for(i=0;i<g.length;i++)n[i]=g[i].tbl_name;e.resolve(n)});return e.promise()},dump:function(e,g){var i=h.Deferred(),g=!1!==g;switch(e){default:m.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(i.reject).done(function(e){var f={},
-c=[],a,d;for(d=0;a=e[d];d++)if(a.sql)switch(a.type){case "table":f[a.tbl_name]={schema:{table:a.sql,indexes:[]}};g&&function(b){var a=h.Deferred();m.query("SELECT * FROM "+b).fail(a.reject).done(function(c){delete c.insertId;f[b].data=c;a.resolve()});c.push(a)}(a.tbl_name);break;case "index":f[a.tbl_name].schema.indexes.push(a.sql)}h.when.apply(h,c).fail(i.reject).done(function(){i.resolve(f)})});break;case "sql":m.dump("json",g).fail(i.reject).done(function(e){var f=[],c,a,d,b,g,j,h,k;for(k in e)if(Object.prototype.hasOwnProperty.call(e,
-k)&&(c=e[k],f.push(c.schema.table),f=f.concat(c.schema.indexes),c.data&&0<c.data.length)){g=[];a=c.data[0];for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&g.push(/\s/.test(b)?"`"+b+"`":b);g=g.join(", ");j=[];for(d=0;a=c.data[d];d++){j[d]=[];for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&(h="number"===typeof a[b]?a[b]:null===a[b]||void 0===a[b]?"NULL":'"'+a[b]+'"',j[d].push(h));j[d]="("+j[d].join(", ")+")"}j=j.join(",\n\t");f.push("INSERT INTO "+k+" ("+g+") VALUES\n\t"+j)}f=f.join(";\n")+
-";";i.resolve(f)})}return i.promise()}};return m},p=Array.isArray||function(h){return!!(h&&h.constructor===Array)}})(window);(function(l,h){h.when=l.when;h.Deferred=l.Deferred;l.WebSQL=h})(jQuery,WebSQL);
+(function(l){var f=l.WebSQL=function(q,s,t,u,v){var r=l.openDatabase&&l.openDatabase(q,s||"1.0",t||q,u||5E6,v),m=r&&{query:function(e){var j=f.Deferred(),h=p(e)?e:arguments;m.rawTx(function(n){var g=f.Deferred(),c,a,d,b,o,i,e,k=g.reject;b=0;for(o=h.length;b<o;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("?,")+
+"?)"),p(a))if(b+=1,p(a[0])){d=0;for(i=a.length;d<i;d++)b+1===o&&d+1===i&&(e=g.resolve),n.executeSql(c,a[d],e,k)}else b+1===o&&(e=g.resolve),n.executeSql(c,a,e,k);else b+1===o&&(e=g.resolve),n.executeSql(c,[],e,k);g.fail(j.reject).done(function(c,a){var b=null,d,g;if(a){if(g=a.rows){b=[];for(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}j.resolve(b)})});return j.promise()},rawTx:function(e){r.transaction(e)},getTableNames:function(){var e=
+$.Deferred();m.query('SELECT tbl_name FROM sqlite_master WHERE type = "table" AND tbl_name NOT REGEXP "^(__|sqlite_).*"').fail(e.reject).done(function(f){var h,n=[];for(h=0;h<f.length;h++)n[h]=f[h].tbl_name;e.resolve(n)});return e.promise()},dump:function(e,j){var h=f.Deferred(),j=!1!==j;switch(e){default:m.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:[]}};j&&function(b){var a=f.Deferred();m.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":m.dump("json",j).fail(h.reject).done(function(e){var g=[],c,a,d,b,f,i,j,k;for(k in e)if(Object.prototype.hasOwnProperty.call(e,
+k)&&(c=e[k],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(", ");i=[];for(d=0;a=c.data[d];d++){i[d]=[];for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&(j="number"===typeof a[b]?a[b]:null===a[b]||void 0===a[b]?"NULL":'"'+a[b]+'"',i[d].push(j));i[d]="("+i[d].join(", ")+")"}i=i.join(",\n\t");g.push("INSERT INTO "+k+" ("+f+") VALUES\n\t"+i)}g=g.join(";\n")+
+";";h.resolve(g)})}return h.promise()}};return m};f.VERSION="0.2";var p=Array.isArray||function(f){return!!(f&&f.constructor===Array)}})(this);(function(l,f){f.when=l.when;f.Deferred=l.Deferred;l.WebSQL=f})(jQuery,WebSQL);
View
8 src/core.js
@@ -1,5 +1,5 @@
(function (context) {
-
+ var VERSION = '0.2';
// Public object
var pub = context.WebSQL = function (name, ver, desc, size, cb) {
@@ -37,7 +37,7 @@
}
// Add ? for fields in insert
- parts = /^\s*INSERT\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
+ parts = /^\s*(?:INSERT|REPLACE)\s+INTO\s+\w+\s*\(([^\)]+)\)\s*$/i.exec(sql);
if(parts && parts[1]) {
sql += ' VALUES ('+(new Array(parts[1].split(',').length)).join('?,')+'?)';
}
@@ -250,6 +250,8 @@
};
+ pub.VERSION = VERSION;
+
// Test if an argument is an array
var isArray = Array.isArray || function (arg) {
@@ -262,5 +264,5 @@
};
-})(window);
+})(this);
View
21 test/WebSQL.html
@@ -31,6 +31,7 @@
ok(WebSQL, 'global object exists');
equal(typeof WebSQL.Deferred, 'function', 'Deferred is function');
equal(typeof WebSQL.when, 'function', 'when is function');
+ equal(typeof WebSQL.VERSION, 'string', 'Version number is exposed');
});
@@ -196,6 +197,23 @@
});
+ asyncTest('single replace', function () {
+ var db = WebSQL('test');
+ db.query(
+ 'DROP TABLE IF EXISTS tests',
+ 'CREATE TABLE tests (id INTEGER PRIMARY KEY AUTOINCREMENT, num INTEGER)'
+ );
+
+ var promise = db.query('REPLACE INTO tests (num)', [6]);
+
+ testPromise(promise);
+ promise.done(function (res) {
+ strictEqual(res.insertId, 1, 'Insert ID matched');
+ start();
+ });
+ });
+
+
asyncTest('multi insert', function () {
var db = WebSQL('test');
db.query(
@@ -224,9 +242,7 @@
[ [6], [7], [8], [9] ]
);
-
testPromise(promise);
-
promise.done(function () {
db.dump('json', true)
.fail(failCB('dump failed'))
@@ -234,6 +250,7 @@
equal(typeof json, 'object', 'Results came back');
equal(typeof json['tests'], 'object', 'Tests table exists');
equal(json.tests.data.constructor, Array, 'Data is an array');
+ equal(json.tests.data[1].num, 7, 'Value exported correctly');
equal(json.tests.schema.table, createTableSQL, 'Create table SQL matches');
equal(json.tests.schema.indexes.length, 1, 'One index');
equal(json.tests.schema.indexes[0], createIndexSQL, 'Create index SQL matches');
View
184 test/jquery.websql.html
@@ -1,184 +0,0 @@
-<!doctype html>
-<html>
-<head>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
- <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
-
- <script src="../jquery.websql.js"></script>
-
- <script>
- $(function () {
-
-
- test('browser support', function () {
- ok(!!window.openDatabase, 'openDatabase exists');
-
- var db = openDatabase('test', '1.0', 'database test', 5e6);
- ok(db, 'open database connection');
-
- equal(typeof db.transaction, 'function', 'transaction function exists');
- });
-
-
- test('WebSQL object', function () {
- ok(WebSQL, 'global object exists');
- equal(typeof WebSQL.Deferred, 'function', 'Deferred is function');
- equal(typeof WebSQL.when, 'function', 'when is function');
- });
-
-
- test('WebSQL jQuery object', function () {
- ok(WebSQL, 'global object exists');
- equal(WebSQL, $.WebSQL, 'WebSQL is on jQuery object');
- equal(typeof $.WebSQL.Deferred, 'function', 'Deferred is function');
- equal(typeof $.WebSQL.when, 'function', 'when is function');
- });
-
-
- test('WebSQL instance', function () {
- var db = $.WebSQL('test');
- ok(db, 'create websql object');
-
- equal(typeof db.query, 'function', 'query is function');
- equal(typeof db.rawTx, 'function', 'rawTx is function');
- });
-
-
- var testPromise = function (promise) {
- ok(promise, 'query returned promise');
- equal(typeof promise.fail, 'function', 'promise has fail function');
- equal(typeof promise.done, 'function', 'promise has done function');
- equal(typeof promise.then, 'function', 'promise has then function');
- equal(typeof promise.always, 'function', 'promise has always function');
-
- var failPromise = promise.fail(function () {
- ok(false, 'query failed');
- start();
- });
- strictEqual(promise, failPromise, 'fail returned promise for chaining');
-
- var donePromise = promise.done(function (res) {
- ok($.isArray(res), 'results are array');
- });
- strictEqual(promise, donePromise, 'done returned promise for chaining');
- };
-
-
- asyncTest('rawTx', function () {
- var db = $.WebSQL('test');
- db.rawTx(function (tx) {
- ok(tx, 'transaction exists');
- start();
- });
- });
-
-
- asyncTest('single query', function () {
- var db = $.WebSQL('test');
- var promise = db.query('SELECT 1 AS num');
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.length, 1, 'result includes 1 row');
- strictEqual(res[0].num, 1, 'results match query');
- start();
- });
- });
-
-
- asyncTest('single query with params', function () {
- var db = $.WebSQL('test');
- var promise = db.query('SELECT ? AS num', [1]);
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.length, 1, 'result includes 1 row');
- strictEqual(res[0].num, 1, 'results match query');
- start();
- });
- });
-
-
- asyncTest('multi query', function () {
- var db = $.WebSQL('test');
- var promise = db.query(
- 'DROP TABLE IF EXISTS tests',
- 'CREATE TABLE tests (num INTEGER)',
- 'SELECT * FROM tests'
- );
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.length, 0, 'result includes 0 row');
- strictEqual(res[0], void null, 'results match query');
- start();
- });
- });
-
-
- asyncTest('multi query inserts', function () {
- var db = $.WebSQL('test');
- var promise = db.query(
- 'DROP TABLE IF EXISTS tests',
- 'CREATE TABLE tests (id INTEGER PRIMARY KEY AUTOINCREMENT, num INTEGER)',
- 'INSERT INTO tests (num) VALUES (?)',
- [
- [1], [2], [3]
- ],
- 'SELECT * FROM tests'
- );
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.length, 3, 'result includes 3 rows');
- equal(typeof res[1], 'object', 'results are objects');
- strictEqual(res[0].num, 1, 'results match query');
- start();
- });
- });
-
-
- asyncTest('multiline query', function () {
- var db = $.WebSQL('test');
- var promise = db.query(function(){/*!
- SELECT *
- FROM tests
- WHERE num > 1
- */});
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.length, 2, 'result includes 2 rows');
- equal(typeof res[1], 'object', 'results are objects');
- strictEqual(res[0].num, 2, 'results match query');
- start();
- });
- });
-
-
- asyncTest('single insert', function () {
- var db = $.WebSQL('test');
- var promise = db.query('INSERT INTO tests (num) VALUES (?)', [4]);
-
- testPromise(promise);
- promise.done(function (res) {
- strictEqual(res.insertId, 4, 'Insert ID matched');
- start();
- });
- });
-
-
- });
- </script>
-
-</head>
-<body>
- <h1 id="qunit-header">QUnit example</h1>
- <h2 id="qunit-banner"></h2>
- <div id="qunit-testrunner-toolbar"></div>
- <h2 id="qunit-userAgent"></h2>
- <ol id="qunit-tests"></ol>
- <div id="qunit-fixture">test markup, will be hidden</div>
-</body>
-</html>
Please sign in to comment.
Something went wrong with that request. Please try again.