sql-formatter
sql-formatter copied to clipboard
[FORMATTING] Confusion between LIMIT keyword and column name
We have a table with a 'limit' column, when inserting into the table sql-formatter indents characters after the column name.
Input data Which SQL and options did you provide as input? options = { language: 'postgresql' };
Expected Output
INSERT INTO
search (
apples,
limit,
pears,
oranges
)
values
(
'__apples',
'__limit',
'__pears',
'__oranges'
);
Actual Output
INSERT INTO
search (
apples,
limit
, pears,
oranges
)
values
(
'__apples',
'__limit',
'__pears',
'__oranges'
);
Usage
- How are you calling / using the library? calling inside a Jest snapshot-serializer
- What SQL language(s) does this apply to? postgresql
- Which SQL Formatter version are you using? I'm using v8.0.2
Thanks for reporting. Related to #156
This is currently a general issue with how the formatter distinguishes language keywords from identifiers. For the time being there's no quick fix for this. We're working towards a more general solution, though no idea when we'll finally get there.
A workaround for the time being is to quote the column name, e.g. "limit".
@nene Yea in our case we insert using Knex.insert() so it builds the query based on the inputs... so wrapping limit in quotes would be messy.
By the way the latest v8.1.0 changed the result slightly...
INSERT INTO
search (
apples,
limit
, pears,
oranges,
apricots
)
-- etc...
to...
INSERT INTO
search (
apples,
limit
, pears, --shifted to the left
oranges, --one indent extra to right
apricots
)
-- etc...
This issue has gone worse in the latest releases. The above SQL will now produce a hard error:
Parse error at token: , Unexpected COMMA token
The problem being that we're now expecting see LIMIT expr ["," expr].