Skip to content

Commit b4941df

Browse files
committed
fix: Format inline CASE <expression> WHEN correctly
1 parent b8924af commit b4941df

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/formatter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ impl<'a> Formatter<'a> {
323323

324324
if !self.inline_block.is_active() {
325325
self.add_new_line(query);
326+
} else if token.value.to_lowercase() == "case" {
327+
query.push(' ');
326328
}
327329
}
328330

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,33 @@ mod tests {
15991599
assert_eq!(format(input, &QueryParams::None, &options), expected);
16001600
}
16011601

1602+
#[test]
1603+
fn it_formats_case_when_inside_an_order_by() {
1604+
let input = "SELECT a, created_at FROM b ORDER BY (CASE $3 WHEN 'created_at_asc' THEN created_at END) ASC, (CASE $3 WHEN 'created_at_desc' THEN created_at END) DESC;";
1605+
let max_line = 120;
1606+
let options = FormatOptions {
1607+
max_inline_block: max_line,
1608+
max_inline_arguments: Some(max_line),
1609+
joins_as_top_level: true,
1610+
uppercase: Some(true),
1611+
ignore_case_convert: Some(vec!["status"]),
1612+
..Default::default()
1613+
};
1614+
1615+
let expected = indoc!(
1616+
"
1617+
SELECT
1618+
a, created_at
1619+
FROM
1620+
b
1621+
ORDER BY
1622+
(CASE $3 WHEN 'created_at_asc' THEN created_at END) ASC,
1623+
(CASE $3 WHEN 'created_at_desc' THEN created_at END) DESC;"
1624+
);
1625+
1626+
assert_eq!(format(input, &QueryParams::None, &options), expected);
1627+
}
1628+
16021629
#[test]
16031630
fn it_recognizes_lowercase_case_end() {
16041631
let input = "case when option = 'foo' then 1 else 2 end;";

0 commit comments

Comments
 (0)