Skip to content

Commit

Permalink
Check insertId after UPDATE & update doc
Browse files Browse the repository at this point in the history
other advanced rowsAffected test updates to check insertId

closes storesafe/cordova-sqlite-storage#802
  • Loading branch information
Christopher J. Brody committed Aug 14, 2018
1 parent cb30c1e commit e748166
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ As "strongly recommended" by [Web SQL Database API 8.5 SQL injection](https://ww
- If the SQL arguments are passed in an `Array` subclass object where the `constructor` does not point to `Array` then the SQL arguments are ignored by the plugin.
- The results data objects are not immutable as specified/implied by [Web SQL (DRAFT) API section 4.5](https://www.w3.org/TR/webdatabase/#database-query-results).
- This plugin supports use of numbered parameters (`?1`, `?2`, etc.) as documented in <https://www.sqlite.org/c3ref/bind_blob.html>, not supported by HTML5/[Web SQL (DRAFT) API](http://www.w3.org/TR/webdatabase/) ref: [Web SQL (DRAFT) API section 4.2](https://www.w3.org/TR/webdatabase/#parsing-and-processing-sql-statements).
- In case of UPDATE this plugin reports `insertId` with the result of `sqlite3_last_insert_rowid()` (except for Android with `androidDatabaseImplementation: 2` setting) while attempt to access `insertId` on the result set database opened by HTML5/[Web SQL (DRAFT) API](http://www.w3.org/TR/webdatabase/) results in an exception.

### Security of deleted data

Expand Down
97 changes: 81 additions & 16 deletions spec/www/spec/tx-semantics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,52 +216,117 @@ var mytests = function() {
})
});

it(suiteName + 'test rowsAffected [advanced]', function (done) {
var db = openDatabase("RowsAffectedAdvanced", "1.0", "Demo", DEFAULT_SIZE);
it(suiteName + 'test insertId & rowsAffected [advanced] - plugin vs (WebKit) Web SQL', function (done) {
var db = openDatabase('test-rowsAffected-advanced.db');

db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS characters');
tx.executeSql('CREATE TABLE IF NOT EXISTS characters (name unique, creator, fav tinyint(1))');
tx.executeSql('DROP TABLE IF EXISTS companies');
tx.executeSql('CREATE TABLE IF NOT EXISTS companies (name unique, fav tinyint(1))');

// INSERT or IGNORE with the real thing:
tx.executeSql('INSERT or IGNORE INTO characters VALUES (?,?,?)', ['Sonic', 'Sega', 0], function (tx, res) {
expect(res.rowsAffected).toBe(1);
tx.executeSql('INSERT INTO characters VALUES (?,?,?)', ['Tails', 'Sega', 0], function (tx, res) {
expect(res.rowsAffected).toBe(1);
tx.executeSql('INSERT INTO companies VALUES (?,?)', ['Sega', 1], function (tx, res) {
expect(res.rowsAffected).toBe(1);
tx.executeSql('INSERT or IGNORE INTO characters VALUES (?,?,?)', ['Sonic', 'Sega', 0], function (txIgnored, rs1) {
expect(rs1.rowsAffected).toBe(1);
expect(rs1.insertId).toBe(1);

tx.executeSql('INSERT INTO characters VALUES (?,?,?)', ['Tails', 'Sega', 0], function (txIgnored, rs2) {
expect(rs2.rowsAffected).toBe(1);
expect(rs2.insertId).toBe(2);

tx.executeSql('INSERT INTO companies VALUES (?,?)', ['Sega', 1], function (txIgnored, rs3) {
expect(rs3.rowsAffected).toBe(1);
expect(rs3.insertId).toBe(1);

// query with subquery
var sql = 'UPDATE characters ' +
' SET fav=(SELECT fav FROM companies WHERE name=?)' +
' WHERE creator=?';
tx.executeSql(sql, ['Sega', 'Sega'], function (tx, res) {
expect(res.rowsAffected).toBe(2);
tx.executeSql(sql, ['Sega', 'Sega'], function (txIgnored, rs4) {
expect(rs4.rowsAffected).toBe(2);
try {
// defined on plugin (except for Android with androidDatabaseImplementation: 2);
// throws on (WebKit) Web SQL:
if (!isWebSql && isAndroid && isImpl2)
expect(rs4.insertId).not.toBeDefined();
else
expect(rs4.insertId).toBeDefined();

// NOT EXPECTED to get here on (WebKit) Web SQL:
if (isWebSql) expect('(WebKit) Web SQL behavior changed').toBe('--');

if (!(isAndroid && isImpl2))
expect(rs4.insertId).toBe(1);
} catch(ex) {
// SHOULD NOT CATCH EXCEPTION on plugin:
if (!isWebSql) expect('EXCEPTION NOT EXPECTED on plugin with message: ' + e.message).toBe('--');
expect(ex).toBeDefined();
expect(ex.message).toBeDefined();
// FUTURE TBD check message
}

// query with 2 subqueries
var sql = 'UPDATE characters ' +
' SET fav=(SELECT fav FROM companies WHERE name=?),' +
' creator=(SELECT name FROM companies WHERE name=?)' +
' WHERE creator=?';
tx.executeSql(sql, ['Sega', 'Sega', 'Sega'], function (tx, res) {
expect(res.rowsAffected).toBe(2);
tx.executeSql(sql, ['Sega', 'Sega', 'Sega'], function (txIgnored, rs5) {
expect(rs5.rowsAffected).toBe(2);
try {
// defined on plugin (except for Android with androidDatabaseImplementation: 2);
// throws on (WebKit) Web SQL:
if (!isWebSql && isAndroid && isImpl2)
expect(rs5.insertId).not.toBeDefined();
else
expect(rs5.insertId).toBeDefined();

// EXPECTED to get here on plugin only:
if (isWebSql) expect('(WebKit) Web SQL behavior changed').toBe('--');

if (!(isAndroid && isImpl2))
expect(rs5.insertId).toBe(1);
} catch(e) {
// SHOULD NOT CATCH EXCEPTION on plugin:
if (!isWebSql) expect('EXCEPTION NOT EXPECTED on plugin').toBe('--');
// XXX TODO CHECK message, etc.
}

// knockoffs shall be ignored:
tx.executeSql('INSERT or IGNORE INTO characters VALUES (?,?,?)', ['Sonic', 'knockoffs4you', 0], function (tx, res) {
tx.executeSql('INSERT or IGNORE INTO characters VALUES (?,?,?)', ['Sonic', 'knockoffs4you', 0], function (txIgnored, rs6) {
// EXPECTED RESULT:
expect(res.rowsAffected).toBe(0);
expect(rs6.rowsAffected).toBe(0);

// insertId plugin vs (WebKit) Web SQL:
if (isWebSql)
expect(rs6.insertId).toBe(1);
else
expect(rs6.insertId).not.toBeDefined();

done();
}, function(tx, error) {
// ERROR NOT EXPECTED here:
}, function(txIgnored, error) {
// ERROR NOT EXPECTED here - knockoff should have been ignored:
logError('knockoff should have been ignored');
expect(error.message).toBe('--');

done.fail();
});

});

});

});

});

});

}, function(error) {
// NOT EXPECTED:
expect(false).toBe(true);
expect(error.message).toBe('--');
// Close (plugin only) & finish:
(isWebSql) ? done() : db.close(done, done);
});
});

Expand Down

0 comments on commit e748166

Please sign in to comment.