DataGrip Autoformatting of WHERE clauses with comments

Answered

In WHERE clauses with multiple AND conditions, it is useful to move AND to the beginning of the next line, as documented in Settings / Editor / Code Style / SQL / SQL dialect: 'Place top-level AND/OR To begin'. But doing so also moves a related comment ( -- comment ) to the next line, while the comment in fact refers to the condition preceding the AND. This results in confusing scripts.

Example:

WHERE condition_1  -- comment on condition_1 AND
     condition_2  -- comment on condition 2 AND
      condition_3

after formatting becomes

WHERE condition_1 
AND                         -- comment on condition 1
      condition_2 
  AND                                                    
                              -- comment on condition 2      condition_3

The comments should not be moved to the next line, but be kept next to the AND condition that they refer to, as in

WHERE condition_1  -- comment on condition_1 
AND condition_2  -- comment on condition 2
 AND condition_3

This would avoid a lot of corrective editing.

 

0
3 comments

Actually, each function contains a certain function. It is understandable that making changes will have different results. I also learned the problem with sharing:

https://youtrack.jetbrains.com/issue/DBE-18246/Incorrect-indentation-of-comment-after-CTE-geometry dash lite

0

Well , the two instances you refer to look similar, but I am not sure they have identical underlying issues. Also, in my example the AND should precede the - - comment (of course).

0

Please sign in to leave a comment.