You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Statement cache serves stale statements after DROP TABLE and CREATE TABLE #2603
The statement cache is not cleared when a table is dropped and created again. A cached statement keeps the column list and column types of the old table. The result is wrong and no error is raised.
Reproduced on develop (v4.20.0):
alasql('CREATE TABLE x (a INT)');alasql('INSERT INTO x VALUES (1)');alasql('SELECT * FROM x');// [{a:1}]alasql('DROP TABLE x');alasql('CREATE TABLE x (a INT, b INT)');alasql('INSERT INTO x (a,b) VALUES (1,2)');alasql('SELECT * FROM x');// [{a:1}] expected [{a:1, b:2}]
With alasql.options.cache = false the last query returns [{a:1, b:2}].
INSERT is hit in the same way. It keeps the column types of the old table:
alasql('CREATE TABLE y (b INT)');alasql('INSERT INTO y VALUES (NULL)');// stores NaN (INT path, see #2590)alasql('DROP TABLE y');alasql('CREATE TABLE y (b STRING)');alasql('INSERT INTO y VALUES (NULL)');// stores NaN again, expected undefined
Cause
alasql.dexec (src/17alasql.js) serves a cached statement only if statement.dbversion === db.dbversion. db.dbversion changes on ALTER TABLE, on transaction rollback and in the localStorage and file storage engines. CREATE TABLE and DROP TABLE do not change it. So a statement compiled before a DROP/CREATE stays valid in the cache.
Direction
Change db.dbversion in every statement that changes the schema. As a minimum: CREATE TABLE, DROP TABLE, CREATE INDEX, DROP INDEX, CREATE VIEW, DROP VIEW. Check the other CREATE/DROP statements too.
Tests: the two examples above. Expected results are the same with the cache on and off.
Activity
Invalidate cache after table change close AlaSQL#2603 (AlaSQL#2609)