sql-formatter icon indicating copy to clipboard operation
sql-formatter copied to clipboard

Prettier JOINs and other formatting improvements

Open rinu opened this issue 1 year ago • 8 comments

Fixed lots of issues with formatting non-trivial SQL queries. This is NOT backwards compatible, there are many breaking changes. This is more in line with what looks good to me and what prettier is doing.

Highlights include:

  • generalized blocks with indented contents, no longer hard-coded just for parenthesis
  • for all sorts of JOINs the ON part and other conditions are on a new line and indented
  • ON DUPLICATE KEY UPDATE is one token
  • CASE WHEN and the like are blocks with indented contents

rinu avatar Aug 15 '23 14:08 rinu

Thank you. This is a feature, so we should target the 1.2.x branch. 1.1.x is for bugfixes.

derrabus avatar Aug 16 '23 07:08 derrabus

Thank you. This is a feature, so we should target the 1.2.x branch. 1.1.x is for bugfixes.

There was a mention of breaking changes in the description. Can you please detail what they are and why they should be considered breaking? Also, please read the contributing guide, especially the part about commit messages.

greg0ire avatar Aug 16 '23 10:08 greg0ire

There was a mention of breaking changes in the description.

The public interface of the Token class was changed: methods have been added and removed from it.

derrabus avatar Aug 16 '23 10:08 derrabus

Conflicts with 1.2.x are now resolved. The main breaking change is the formatted output, especially for all types of JOINs. In my app I rely on the exact output of format.

rinu avatar Aug 16 '23 12:08 rinu

In my app I rely on the exact output of format.

Can you elaborate on this?

greg0ire avatar Aug 16 '23 15:08 greg0ire

I use this formatter to lint SQL in my project. My linter will look at every string in the project and if it looks like SQL it will apply this formatting. There are probably thousands of these so a consistent style is very useful.

rinu avatar Aug 16 '23 16:08 rinu

I had no idea this project was used that way… in that particular case, any change does seem like a breaking change, but I'm not sure this means that's the case for most consumers of this project.

greg0ire avatar Aug 16 '23 20:08 greg0ire

I think this is ready now.

This logic of blocks could be used in the future to give better context for both formatting and highligting. For example in the case of VALUES that is both a function and a keyword, you could one day have an INSERT INTO block and VALUES would only be teated as a keyword if it's in the INSERT INTO block but as a function in the ON DUPLICATE KEY UPDATE block. Then there are lots of reserved words that should not be treated as such in places where field or table names are expected.

rinu avatar Aug 17 '23 06:08 rinu