Skip to content

Commit 9cba19e

Browse files
committed
Fix bad formatting in CASE statements with variable assignments. Thanks to Serhii Khoma for the report.
1 parent 0f832c0 commit 9cba19e

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

lib/pgFormatter/Beautify.pm

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Takes options as hash. Following options are recognized:
160160
161161
=item * no_extra_line - do not add an extra empty line at end of the output
162162
163-
=item * keep_newline - preserve empty line in plpgsql code
163+
=item * keep_newline - preserve empty line in plpgsql code
164164
165165
=item * no_space_function - remove space before function call and open parenthesis
166166
@@ -2475,7 +2475,7 @@ sub beautify {
24752475
$add_newline
24762476
and $self->{'comma'} eq 'end'
24772477
and ( $self->{'comma_break'}
2478-
|| ($self->{'_current_sql_stmt'} ne 'INSERT'
2478+
|| ($self->{'_current_sql_stmt'} ne 'INSERT'
24792479
&& ($self->{'_current_sql_stmt'} ne 'DO UPDATE'
24802480
|| !$self->{'_parenthesis_level'})) )
24812481
)
@@ -2490,10 +2490,12 @@ sub beautify {
24902490

24912491
$self->_add_token($token);
24922492

2493-
next
2494-
if ( $token eq ';'
2493+
if ( $token eq ';'
24952494
and $#{ $self->{'_is_in_case'} } >= 0
2496-
and uc($last) ne 'CASE' );
2495+
and uc($last) ne 'CASE' ) {
2496+
$last = $self->_set_last( $token, $last );
2497+
next;
2498+
}
24972499

24982500
if ( $self->{'_is_in_type'} and $last eq ')' ) {
24992501
$self->_reset_level( $token, $last )
@@ -3683,6 +3685,10 @@ sub beautify {
36833685
$self->{'content'} =~ s/\s+$/\n/s;
36843686
}
36853687

3688+
if ($#{ $self->{'_is_in_case'} } >= 0 && defined $last && $last eq ';') {
3689+
$self->_new_line( $token, $last );
3690+
}
3691+
36863692
# Finally add the token without further condition
36873693
$self->_add_token( $token, $last );
36883694

@@ -3948,7 +3954,7 @@ sub _add_token {
39483954
if ($DEBUG_SP);
39493955
$self->{'content'} .= $sp;
39503956
}
3951-
else {
3957+
else {
39523958
print STDERR "DEBUG_SPC: 7) last=", ( $last_token || '' ),
39533959
", token=$token\n"
39543960
if ($DEBUG_SP);

t/02_regress.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Test::Simple tests => 79;
1+
use Test::Simple tests => 80;
22
use File::Temp qw/ tempfile /;
33

44
my $pg_format = $ENV{PG_FORMAT} // './pg_format'; # set to the full path to 'pg_format' to test installed binary in /usr/bin
@@ -43,10 +43,9 @@ foreach my $f (@files)
4343
} elsif ($f =~ m#/ex67.sql$#) {
4444
my @ret = `grep "confirmed|hello|'Y'|'N'" /tmp/output.sql`;
4545
ok( $#ret < 0, "Test anonymize");
46-
} else {
46+
} else {
4747
my @diff = `diff -u /tmp/output.sql $f | grep "^[+-]" | grep -v "^[+-]\t\$" | grep -v "^[+-][+-][+-]"`;
4848
ok( $#diff < 0, "Test file $f");
4949
}
5050
unlink("/tmp/output.sql");
5151
}
52-

t/test-files/ex78.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE OR REPLACE FUNCTION myf ()
2+
RETURNS json
3+
LANGUAGE plpgsql
4+
AS $$
5+
DECLARE
6+
BEGIN
7+
foo := 0.5;
8+
bar := 1;
9+
baz := GREATEST (baz, 1.0);
10+
CASE result.attribute_value
11+
WHEN 'asdf' THEN
12+
foo := 0.5; bar := 1; baz := GREATEST (baz, 1.0);
13+
END CASE;
14+
END;
15+
$$;

t/test-files/expected/ex78.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE OR REPLACE FUNCTION myf ()
2+
RETURNS json
3+
LANGUAGE plpgsql
4+
AS $$
5+
DECLARE
6+
BEGIN
7+
foo := 0.5;
8+
bar := 1;
9+
baz := GREATEST (baz, 1.0);
10+
CASE result.attribute_value
11+
WHEN 'asdf' THEN
12+
foo := 0.5;
13+
bar := 1;
14+
baz := GREATEST (baz, 1.0);
15+
END CASE;
16+
END;
17+
$$;
18+

0 commit comments

Comments
 (0)