Skip to content

Commit

Permalink
Add option -g, --no-grouping and CGI corresponding checkbox. By default
Browse files Browse the repository at this point in the history
pgFormatter groups all statements when they are in a transaction:

	BEGIN;
	INSERT INTO foo VALUES (1, 'text 1');
	INSERT INTO foo VALUES (2, 'text 2');
	...
	COMMIT;

By disabling grouping of statement pgFormatter will always add an extra
newline characters between statements just like outside a transaction:

	BEGIN;

	INSERT INTO foo VALUES (1, 'text 1');

	INSERT INTO foo VALUES (2, 'text 2');
	...

	COMMIT;

This might add readability to not DML transactions.
Thanks to Francisco Puga for the feature request.
  • Loading branch information
darold committed Sep 20, 2019
1 parent 0dec727 commit 8766c27
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ DESCRIPTION
unchanged: 0. Values: 0=>unchanged, 1=>lowercase,
2=>uppercase, 3=>capitalize.
-F | --format STR : output format: text or html. Default: text.
-g | --nogrouping : add a newline between statements in transaction
regroupement. Default is to group statements.
-h | --help : show this message and exit.
-m | --maxlength SIZE : maximum length of a query, it will be cutted above
the given size. Default: no truncate.
Expand Down Expand Up @@ -141,6 +143,30 @@ SPECIAL FORMATTING

Expect this list grow following alternative thoughts.

Option -g, --no-grouping
By default pgFormatter groups all statements when they are in a
transaction:

BEGIN;
INSERT INTO foo VALUES (1, 'text 1');
INSERT INTO foo VALUES (2, 'text 2');
...
COMMIT;

By disabling grouping of statement pgFormatter will always add an extra
newline characters between statements just like outside a transaction:

BEGIN;

INSERT INTO foo VALUES (1, 'text 1');

INSERT INTO foo VALUES (2, 'text 2');
...

COMMIT;

This might add readability to not DML transactions.

HINTS
Formatting from VI
With pgFormatter, you can just add the following line to your ~/.vimrc
Expand Down
28 changes: 28 additions & 0 deletions doc/pg_format.pod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Options:
unchanged: 0. Values: 0=>unchanged, 1=>lowercase,
2=>uppercase, 3=>capitalize.
-F | --format STR : output format: text or html. Default: text.
-g | --nogrouping : add a newline between statements in transaction
regroupement. Default is to group statements.
-h | --help : show this message and exit.
-m | --maxlength SIZE : maximum length of a query, it will be cutted above
the given size. Default: no truncate.
Expand Down Expand Up @@ -147,6 +149,32 @@ This option activate an alternative formatting that adds:

Expect this list grow following alternative thoughts.

=head2 Option -g, --no-grouping

By default pgFormatter groups all statements when they are in a
transaction:

BEGIN;
INSERT INTO foo VALUES (1, 'text 1');
INSERT INTO foo VALUES (2, 'text 2');
...
COMMIT;

By disabling grouping of statement pgFormatter will always add an
extra newline characters between statements just like outside a
transaction:

BEGIN;

INSERT INTO foo VALUES (1, 'text 1');

INSERT INTO foo VALUES (2, 'text 2');
...

COMMIT;

This might add readability to not DML transactions.

=head1 HINTS

=head2 Formatting from VI
Expand Down
9 changes: 7 additions & 2 deletions lib/pgFormatter/Beautify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Takes options as hash. Following options are recognized:
=item * no_comments - if set to true comments will be removed from query
=item * no_grouping - if set to true statements will not be grouped in a transaction, an extra newline character will be added between statements like outside a transaction.
=item * placeholder - use the specified regex to find code that must not be changed in the query.
=item * query - query to beautify
Expand Down Expand Up @@ -148,7 +150,7 @@ sub new {
my $self = bless {}, $class;
$self->set_defaults();

for my $key ( qw( query spaces space break wrap keywords functions rules uc_keywords uc_functions no_comments placeholder separator comma comma_break format colorize format_type wrap_limit wrap_after) ) {
for my $key ( qw( query spaces space break wrap keywords functions rules uc_keywords uc_functions no_comments no_grouping placeholder separator comma comma_break format colorize format_type wrap_limit wrap_after) ) {
$self->{ $key } = $options{ $key } if defined $options{ $key };
}

Expand Down Expand Up @@ -1021,7 +1023,7 @@ sub beautify {
$self->_over($token,$last);
$self->{ '_is_in_block' }++;
}
$self->{ '_is_in_work' } = 1 if (defined $self->_next_token && $self->_next_token =~ /^(WORK|TRANSACTION|ISOLATION|;)$/i);
$self->{ '_is_in_work' } = 1 if (!$self->{ 'no_grouping' } and defined $self->_next_token && $self->_next_token =~ /^(WORK|TRANSACTION|ISOLATION|;)$/i);
$last = $token;
next;
}
Expand Down Expand Up @@ -2647,6 +2649,8 @@ Currently defined defaults:
=item no_comments => 0
=item no_grouping => 0
=item placeholder => ''
=item separator => ''
Expand Down Expand Up @@ -2681,6 +2685,7 @@ sub set_defaults {
$self->{ 'uc_keywords' } = 0;
$self->{ 'uc_functions' } = 0;
$self->{ 'no_comments' } = 0;
$self->{ 'no_grouping' } = 0;
$self->{ 'placeholder' } = '';
$self->{ 'keywords' } = $self->{ 'dict' }->{ 'pg_keywords' };
$self->{ 'types' } = $self->{ 'dict' }->{ 'pg_types' };
Expand Down
9 changes: 8 additions & 1 deletion lib/pgFormatter/CGI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ sub set_config {
$self->{ 'help' } = '';
$self->{ 'version' } = '';
$self->{ 'nocomment' } = 0;
$self->{ 'nogrouping' } = 0;
$self->{ 'colorize' } = 1;
$self->{ 'uc_keyword' } = 2;
$self->{ 'uc_function' } = 0;
Expand Down Expand Up @@ -157,7 +158,7 @@ sub get_params {
# shortcut
my $cgi = $self->{ 'cgi' };

for my $param_name ( qw( colorize spaces uc_keyword uc_function content nocomment show_example anonymize separator comma comma_break format_type wrap_after original_content) ) {
for my $param_name ( qw( colorize spaces uc_keyword uc_function content nocomment nogrouping show_example anonymize separator comma comma_break format_type wrap_after original_content) ) {
$self->{ $param_name } = $cgi->param( $param_name ) if defined $cgi->param( $param_name );
}

Expand Down Expand Up @@ -200,6 +201,7 @@ sub sanitize_params {
$self->{ 'uc_keyword' } = 2 if $self->{ 'uc_keyword' } && ( $self->{ 'uc_keyword' } !~ /^(0|1|2|3)$/ );
$self->{ 'uc_function' } = 0 if $self->{ 'uc_function' } && ( $self->{ 'uc_function' } !~ /^(0|1|2|3)$/ );
$self->{ 'nocomment' } = 0 if $self->{ 'nocomment' } !~ /^(0|1)$/;
$self->{ 'nogrouping' } = 0 if $self->{ 'nogrouping' } !~ /^(0|1)$/;
$self->{ 'show_example' } = 0 if $self->{ 'show_example' } !~ /^(0|1)$/;
$self->{ 'separator' } = '' if ($self->{ 'separator' } eq "'" or length($self->{ 'separator' }) > 6);
$self->{ 'comma' } = 'end' if ($self->{ 'comma' } ne 'start');
Expand Down Expand Up @@ -248,6 +250,7 @@ sub beautify_query {
$args{ 'comma_break' } = $self->{ 'comma_break' };
$args{ 'format_type' } = 1 if ($self->{ 'format_type' });
$args{ 'wrap_after' } = $self->{ 'wrap_after' };
$args{ 'no_grouping' } = 1 if $self->{ 'nogrouping' };

$self->{ 'content' } = &remove_extra_parenthesis($self->{ 'content' } ) if ($self->{ 'content' } );

Expand Down Expand Up @@ -286,6 +289,7 @@ sub print_body {
my $chk_comma = $self->{ 'comma' } eq 'start' ? 'checked="checked" ' : '';
my $chk_comma_break = $self->{ 'comma_break' } ? 'checked="checked" ' : '';
my $chk_format_type = $self->{ 'format_type' } ? 'checked="checked" ' : '';
my $chk_nogrouping = $self->{ 'nogrouping' } ? 'checked="checked" ' : '';

my %kw_toggle = ( 0 => '', 1 => '', 2 => '', 3 => '' );
$kw_toggle{ $self->{ 'uc_keyword' } } = ' selected="selected"';
Expand Down Expand Up @@ -318,6 +322,9 @@ sub print_body {
<br />
<input type="checkbox" id="id_format_type" name="format_type" value="1" onchange="document.forms[0].original_content.value != ''; document.forms[0].submit();" $chk_format_type/>
<label for="id_format_type">Alternate formatting</label>
<br />
<input type="checkbox" id="id_no_grouping" name="nogrouping" value="1" onchange="document.forms[0].original_content.value != ''; document.forms[0].submit();" $chk_nogrouping/>
<label for="id_no_grouping">No transaction grouping</label>
</div>
</fieldset>
<br />
Expand Down
4 changes: 4 additions & 0 deletions lib/pgFormatter/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ sub beautify {
$args{ 'wrap_limit' } = $self->{ 'cfg' }->{ 'wrap-limit' };
$args{ 'wrap_after' } = $self->{ 'cfg' }->{ 'wrap-after' };
$args{ 'space' } = $self->{ 'cfg' }->{ 'space' };
$args{ 'no_grouping' } = $self->{ 'cfg' }->{ 'nogrouping' };

if ($self->{ 'query' } && ($args{ 'maxlength' } && length($self->{ 'query' }) > $args{ 'maxlength' })) {
$self->{ 'query' } = substr($self->{ 'query' }, 0, $args{ 'maxlength' })
Expand Down Expand Up @@ -195,6 +196,8 @@ Options:
unchanged: 0. Values: 0=>unchanged, 1=>lowercase,
2=>uppercase, 3=>capitalize.
-F | --format STR : output format: text or html. Default: text.
-g | --nogrouping : add a newline between statements in transaction
regroupement. Default is to group statements.
-h | --help : show this message and exit.
-m | --maxlength SIZE : maximum length of a query, it will be cutted above
the given size. Default: no truncate.
Expand Down Expand Up @@ -268,6 +271,7 @@ sub get_command_line_args {
'debug|d!',
'format|F=s',
'function-case|f=i',
'nogrouping|g!',
'help|h!',
'maxlength|m=i',
'nocomment|n!',
Expand Down

0 comments on commit 8766c27

Please sign in to comment.