AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.01k stars 653 forks source link

SQL Parser breaks on columns named "value", "matrix" or "query" (possibly more) #1155

Open SanderVocke opened 4 years ago

SanderVocke commented 4 years ago

I noticed this when trying to attach to an SQLite database that has a TEXT column named value. It seems this is seen as some kind of keyword by the parser, even though in this context it is just a string literal.

The following command produces a SyntaxError regardless of the type of database used:

CREATE TABLE table(thing TEXT, value TEXT)

It seems this has been an issue (and solved) before in #246, but may have regressed. I don't know how to work around it in the use-case of importing an external database that already has this column in it.

Error:

Unhandled Rejection (SyntaxError): Parse error on line 1:
...hing(property TEXT, value TEXT);
-----------------------^
Expecting 'LITERAL', 'BRALITERAL', 'UNIQUE', 'INDEX', 'CONSTRAINT', 'CHECK', 'PRIMARY', 'KEY', 'FOREIGN', got 'VALUE'
▼ 9 stack frames were expanded.
Parser.parser.parseError
node_modules/alasql/dist/alasql.min.js:2211
Parser.parse
node_modules/alasql/dist/alasql.min.js:2085
Function.alasql.parse
node_modules/alasql/dist/alasql.min.js:4425
Function.alasql.dexec
node_modules/alasql/dist/alasql.min.js:4626
Function.alasql.exec
node_modules/alasql/dist/alasql.min.js:4603
alasql
node_modules/alasql/dist/alasql.min.js:138
(anonymous function)
node_modules/alasql/dist/alasql.min.js:5228
promiseExec
node_modules/alasql/dist/alasql.min.js:5227
Function.alasql.promise
node_modules/alasql/dist/alasql.min.js:5276
SanderVocke commented 4 years ago

Adding to this, I've seen the same behavior for the column names "query" and "matrix".

SanderVocke commented 4 years ago

Sorry to spam this. But I learned now that this would work if those literals were escaped using backquotes. Could the parser or SQLite attach function be somehow fixed to escape these literals?

mathiasrw commented 4 years ago

As you said, you will need to use

`...`

or

[...]

to escape the keywords. PR welcome to fix this.

SanderVocke commented 4 years ago

Feel free to keep this closed if you disagree, but I do see it as an issue in the built-in SQLite importer, where users of AlaSQL have no way to modify the table creation queries to add these characters.

It should be possible to fix the SQLite importer without having to support unescaped literals in general.