sqlparse
sqlparse copied to clipboard
Feature request: Add line breaks in long expressions
Suppose I have a query like this:
SELECT CASE
WHEN condition_1 = 1
AND condition_2 = 2
AND condition_3 = 3
AND condition_4 = 4
AND condition_5 = 5
THEN '5 conditions satisfied'
WHEN condition_6_with_a_long_name = 6
THEN 'Condition 6 satisfied'
ELSE 'Uh-oh'
END AS long_case_field,
field_with_long_name_1 + field_with_long_name_2 + field_with_long_name_3 + field_with_long_name_4 + field_with_long_name_5 + field_with_long_name_6 AS sum_of_fields
FROM my_table
When I run sqlparse.format(sql, reindent_aligned=True)
, I get this:
SELECT CASE WHEN condition_1 = 1 AND condition_2 = 2 AND condition_3 = 3 AND condition_4 = 4 AND condition_5 = 5 THEN '5 conditions satisfied'
WHEN condition_6_with_a_long_name = 6 THEN 'Condition 6 satisfied'
ELSE 'Uh-oh'
END AS long_case_field,
field_with_long_name_1 + field_with_long_name_2 + field_with_long_name_3 + field_with_long_name_4 + field_with_long_name_5 + field_with_long_name_6 AS sum_of_fields
FROM my_table
I think CASE
statements are generally more readable when there's a line break before every AND
/OR
, so I'd prefer the formatter not to collapse these all to one line. For the long function expression, having a max_line_length after which it would add a line break before every +
or other operator would be very nice to have also, so the output would be like
SELECT CASE WHEN condition_1 = 1
AND condition_2 = 2
AND condition_3 = 3
AND condition_4 = 4
AND condition_5 = 5 THEN '5 conditions satisfied'
WHEN condition_6_with_a_long_name = 6 THEN 'Condition 6 satisfied'
ELSE 'Uh-oh'
END AS long_case_field,
field_with_long_name_1
+ field_with_long_name_2
+ field_with_long_name_3
+ field_with_long_name_4
+ field_with_long_name_5
+ field_with_long_name_6 AS sum_of_fields
FROM my_table