Skip to content

Commit

Permalink
Fix indentation of boolean expressions in SELECT's targets
Browse files Browse the repository at this point in the history
This fixes issue #3.
  • Loading branch information
lelit committed Oct 12, 2017
1 parent 3fbef04 commit 605a9ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pg_query/printers/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,14 @@ def bool_expr(node, output):
bet = enums.BoolExprType
outer_exp_level = output.expression_level
with output.expression():
in_res_target = node.parent_node.node_tag == 'ResTarget'
if node.boolop == bet.AND_EXPR:
relindent = -4 if outer_exp_level == 0 else None
relindent = -4 if not in_res_target and outer_exp_level == 0 else None
output.print_list(node.args, 'AND', relative_indent=relindent)
elif node.boolop == bet.OR_EXPR:
with output.expression():
relindent = -3 if outer_exp_level == 0 else None
output.print_list(node.args, 'OR')
relindent = -3 if not in_res_target and outer_exp_level == 0 else None
output.print_list(node.args, 'OR', relative_indent=relindent)
else:
output.writes('NOT')
output.print(node.args[0])
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sql_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,25 @@ def test_inserts(sql):
FROM b) AS b ON a.id = b.id""",
None
),
(
"""\
select a.one,
not a.bool_flag and a.something is null or a.other = 3 as foo,
a.value1 + b.value2 * b.value3 as bar
from sometable as a
where not a.bool_flag2 and a.something2 is null or a.other2 = 3""",
"""\
SELECT a.one
, ( ( (NOT a.bool_flag)
AND a.something IS NULL)
OR (a.other = 3)) AS foo
, a.value1 + (b.value2 * b.value3) AS bar
FROM sometable AS a
WHERE (( (NOT a.bool_flag2)
AND a.something2 IS NULL)
OR (a.other2 = 3))""",
None
),
)
@pytest.mark.parametrize('original, expected, options', EXAMPLES)
def test_prettification(original, expected, options):
Expand Down

0 comments on commit 605a9ef

Please sign in to comment.