taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
798 stars 180 forks source link

MySQL Create Tables Script failed for certain names likely to be reserved words #1979

Closed ralphtq closed 3 months ago

ralphtq commented 3 months ago

Describe the bug Using a script generated by MDM ACCDB Viewer I encountered parsing failures on what are likely to be reserved tokens. For example a field called 'Order' , or 'Table' or 'Row'. In the examples attached the fields have been appended with 'Fix'. With these edits the parse of ~580 lines of SQL was successful.

I also had to place single quotes around a CREATE TABLE for 'Table':

CREATE TABLE 'Table' ( TableID INTEGER PRIMARY KEY, IsAbstract BOOLEAN, HasOpenColumns BOOLEAN, HasOpenRows BOOLEAN, HasOpenSheets BOOLEAN, IsNormalised BOOLEAN, IsFlat BOOLEAN, RowGUID VARCHAR(16) ) ;

The original code generated from MDB ACCDB for the above was:

CREATE TABLE "Table" ( "TableID" INTEGER PRIMARY KEY, "IsAbstract" BOOLEAN, "HasOpenColumns" BOOLEAN, "HasOpenRows" BOOLEAN, "HasOpenSheets" BOOLEAN, "IsNormalised" BOOLEAN, "IsFlat" BOOLEAN, "RowGUID" VARCHAR(16) ) CHARACTER SET 'UTF8';

I had to remove all quotes and use single quote for 'Table'

One final issue CHARACTER SET 'UTF8'; failed to parse.

Database Engine mySQL

To Reproduce

Remove 'Fix' on this CREATE TABLE

CREATE TABLE OperandReferenceLocation ( OperandReferenceID INTEGER PRIMARY KEY, CellID INTEGER, TableFix VARCHAR(255), RowFix VARCHAR(255), Column VARCHAR(255), Sheet VARCHAR(255) ) ;

ralphtq commented 3 months ago

I just discovered I could use single quotes ` for reserved name such as 'Order'.

So that part of the issue is solved for table names also.

Only remaining issue is CHARACTER SET 'UTF8'

taozhi8833998 commented 3 months ago

@ralphtq Yes, you are correct. You should enclose the reserved name in ` when using it as a table or column name. By the way, I resolved the character set issue.

ralphtq commented 3 months ago

Good