Skip to content

Commit

Permalink
Avoid caching dirty data to fix #2027
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrutter authored Jan 11, 2025
1 parent 0dd3a6f commit a4c1a42
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/17alasql.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ alasql.exec = function (sql, params, cb, scope) {
}
};

/**
* Clears any unneeded properties from a given AST statement closure used for caching
* @param {Object} statement the statement to cleanup
*/
function cleanupCache(statement) {
if (!statement) {
return;
}
if (!alasql.options.cache) {
return;
}
// cleanup the table data to prevent storing this information in the SQL cache
if (statement && statement.query && statement.query.data) {
statement.query.data = [];
}
}

/**
Run SQL statement on specific database
*/
Expand All @@ -259,7 +276,9 @@ alasql.dexec = function (databaseid, sql, params, cb, scope) {
let statement = db.sqlCache[hh];
// If database structure was not changed since last time return cache
if (statement && db.dbversion === statement.dbversion) {
return statement(params, cb);
var res = statement(params, cb);
cleanupCache(statement);
return res;
}
}

Expand Down Expand Up @@ -302,6 +321,7 @@ alasql.dexec = function (databaseid, sql, params, cb, scope) {
db.sqlCache[hh] = statement;
}
var res = (alasql.res = statement(params, cb, scope));
cleanupCache(statement);
return res;
}
alasql.precompile(ast.statements[0], alasql.useid, params);
Expand Down
42 changes: 42 additions & 0 deletions test/test2027.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const alasql = require('../dist/alasql.js');

if (typeof exports === 'object') {
var assert = require('assert');
}

describe('Test 2007 - SQL cache', function () {
before(function () {
alasql('create database test');
alasql('use test');
});

after(function () {
alasql('drop database test');
});

it('A) Execute query and assert cache for `data` afterwards', () => {
alasql('CREATE TABLE osoby (id INT, meno STRING)');
alasql('INSERT INTO osoby VALUES (1, "John"), (2, "Jane"), (3, "Jake")');
var res = alasql('SELECT * FROM osoby');

assert.deepEqual(alasql.databases["test"].sqlCache["-169125189"].query.data, []);
assert.equal(res.length, 3);

// Delete all rows
alasql('DELETE FROM osoby');

// Assert that the cache is still empty for "data"
// Without the fix, the cache would still contain the data from the previous query even though all rows were deleted
assert.deepEqual(alasql.databases["test"].sqlCache["-169125189"].query.data, []);

// Insert more rows
alasql('INSERT INTO osoby VALUES (4, "Jack"), (5, "Paul")');

// Execute same query from cache again, the cache is hit now
var res2 = alasql('SELECT * FROM osoby');

// Cache should still be empty for "data"
assert.deepEqual(alasql.databases["test"].sqlCache["-169125189"].query.data, []);
assert.equal(res2.length, 2);
});
});

0 comments on commit a4c1a42

Please sign in to comment.