Skip to content

Commit

Permalink
Add formatting of CREATE PARTITION STATEMENT.
Browse files Browse the repository at this point in the history
  • Loading branch information
darold committed Nov 20, 2018
1 parent bdbcefe commit 9ba9d4d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
52 changes: 42 additions & 10 deletions lib/pgFormatter/Beautify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ sub beautify {
$self->{ '_is_in_filter' } = 0;
$self->{ '_is_in_within' } = 0;
$self->{ '_is_in_grouping' } = 0;
$self->{ '_is_in_partition' } = 0;

my $last = '';
my @token_array = $self->tokenize_sql();
Expand Down Expand Up @@ -535,7 +536,7 @@ sub beautify {
# Control case where we have to add a newline, go back and
# reset indentation after the last ) in the WITH statement
####
if (!$self->{ '_is_in_publication' } && $token =~ /^WITH$/i && (!defined $last || $last ne ')'))
if (!$self->{ '_is_in_partition' } && !$self->{ '_is_in_publication' } && $token =~ /^WITH$/i && (!defined $last || $last ne ')'))
{
$self->{ '_is_in_with' } = 1;
}
Expand Down Expand Up @@ -801,13 +802,24 @@ sub beautify {
$self->_new_line if (uc($token) ne 'SECURITY' or (defined $last and uc($last) ne 'LEVEL'));
$self->_add_token( $token );
}
elsif ($token =~ /^PARTITION$/i)
{
$self->{ '_is_in_partition' } = 1;
if ($self->{ '_is_in_create' } && defined $last and $last eq ')')
{
$self->_new_line;
$self->_add_token( $token );
}
}

elsif ( $token eq '(' )
{
$self->{ '_is_in_create' }++ if ($self->{ '_is_in_create' });
$self->{ '_is_in_constraint' }++ if ($self->{ '_is_in_constraint' });
$self->_add_token( $token, $last );
if ( !$self->{ '_is_in_index' } && !$self->{ '_is_in_publication' } && !$self->{ '_is_in_distinct' } && !$self->{ '_is_in_filter' } && !$self->{ '_is_in_grouping' }) {
if ( !$self->{ '_is_in_index' } && !$self->{ '_is_in_publication' }
&& !$self->{ '_is_in_distinct' } && !$self->{ '_is_in_filter' }
&& !$self->{ '_is_in_grouping' } && !$self->{ '_is_in_partition' }) {
if (uc($last) eq 'AS' || $self->{ '_is_in_create' } == 2 || uc($self->_next_token) eq 'CASE') {
$self->_new_line;
}
Expand Down Expand Up @@ -836,15 +848,15 @@ sub beautify {
$self->_add_token( $token );
next;
}
if ($self->{ '_is_in_index' } || $self->{ '_is_in_alter' }) {
if ($self->{ '_is_in_index' } || $self->{ '_is_in_alter' } || $self->{ '_is_in_partition' }) {
$self->_add_token( '' );
$self->_add_token( $token );
$last = $token;
next;
}
if (defined $self->_next_token && uc($self->_next_token) ne 'FILTER') {
$self->_new_line if ($self->{ '_is_in_create' } > 1
and (not defined $self->_next_token or $self->_next_token eq ';')
and (not defined $self->_next_token or $self->_next_token =~ /^PARTITION|;$/i)
);
$self->_new_line if ($self->{ '_is_in_type' } == 1
and (not defined $self->_next_token or $self->_next_token eq ';')
Expand Down Expand Up @@ -910,6 +922,7 @@ sub beautify {
&& !$self->{ '_is_in_publication' }
&& !$self->{ '_is_in_call' }
&& !$self->{ '_is_in_grouping' }
&& !$self->{ '_is_in_partition' }
&& ($self->{ '_is_in_constraint' } <= 1)
&& $self->{ '_current_sql_stmt' } !~ /^(GRANT|REVOKE)$/
&& $self->_next_token !~ /^('$|\s*\-\-)/i
Expand Down Expand Up @@ -949,6 +962,7 @@ sub beautify {
$self->{ '_is_in_distinct' } = 0;
$self->{ '_is_in_array' } = 0;
$self->{ '_is_in_filter' } = 0;
$self->{ '_is_in_partition' } = 0;
$self->_add_token($token);
if ( $self->{ '_insert_values' } )
{
Expand Down Expand Up @@ -993,7 +1007,7 @@ sub beautify {
}
elsif ($token =~ /^FOR$/i)
{
if ($self->_next_token =~ /^(UPDATE|KEY|NO)$/i)
if ($self->_next_token =~ /^(UPDATE|KEY|NO|VALUES)$/i)
{
$self->_back;
$self->_new_line;
Expand All @@ -1011,6 +1025,20 @@ sub beautify {

$self->{ 'no_break' } = 0;

# special cases for create partition statement
if ($token =~ /^VALUES$/i && defined $last and uc($last) eq 'FOR')
{
$self->_add_token( $token );
$last = $token;
next;
} elsif ($token =~ /^FROM$/i && defined $last and uc($last) eq 'VALUES')
{
$self->_add_token( $token );
$last = $token;
next;
}

# Case of DISTINCT FROM clause
if ($token =~ /^FROM$/i)
{
if (uc($last) eq 'DISTINCT' || $self->{ '_is_in_alter' })
Expand All @@ -1020,13 +1048,14 @@ sub beautify {
next;
}
}

if (($token =~ /^FROM$/i) && $self->{ '_has_from' } && !$self->{ '_is_in_function' })
{
$self->{ '_has_from' } = 0;
}
if ($token =~ /^FROM$/i)
{
$self->{ '_is_in_from' }++ if (!$self->{ '_is_in_function' });
$self->{ '_is_in_from' }++ if (!$self->{ '_is_in_function' } && !$self->{ '_is_in_partition' });
}
if ($token =~ /^WHERE$/i && !$self->{ '_is_in_filter' })
{
Expand Down Expand Up @@ -1171,7 +1200,7 @@ sub beautify {
$self->{ '_is_in_where' }-- if ($self->{ '_is_in_where' });
}

elsif ( $token =~ /^(?:BY)$/i and $last !~ /^(INCREMENT|OWNED)$/ )
elsif ( $token =~ /^(?:BY)$/i and $last !~ /^(INCREMENT|OWNED|PARTITION)$/ )
{
$self->_add_token( $token );
$self->_new_line;
Expand Down Expand Up @@ -1418,7 +1447,7 @@ sub beautify {
$self->_add_token($token);
}

elsif (uc($last) ne 'NO' and $token =~ /^(MINVALUE|MAXVALUE)$/i)
elsif ($last !~ /^(\(|NO)$/i and $token =~ /^(MINVALUE|MAXVALUE)$/i)
{
$self->_new_line;
$self->_add_token($token);
Expand Down Expand Up @@ -1514,7 +1543,7 @@ sub _add_token {
my $sp = $self->_indent;

if ( !$self->_is_punctuation( $token ) and !$last_is_dot) {
if ( (!defined($last_token) || $last_token ne '(') && $token ne ')' && ($token !~ /^::/) ) {
if ( (!defined($last_token) || $last_token ne '(') && $token ne ')' && $token !~ /^::/ ) {
$self->{ 'content' } .= $sp if ($token ne ')'
&& defined($last_token)
&& $last_token ne '::'
Expand All @@ -1523,7 +1552,10 @@ sub _add_token {
);
$self->{ 'content' } .= $sp if (!defined($last_token) && $token);
} elsif ( $self->{ '_is_in_create' } == 2 && defined($last_token)) {
$self->{ 'content' } .= $sp if ($last_token ne '::' and ($last_token ne '(' || !$self->{ '_is_in_index' }));
$self->{ 'content' } .= $sp if ($last_token ne '::'
and !$self->{ '_is_in_partition' }
and ($last_token ne '(' || !$self->{ '_is_in_index' })
);
} elsif (defined $last_token) {
$self->{ 'content' } .= $sp if ($last_token eq '(' && $self->{ '_is_in_type' });
}
Expand Down
15 changes: 15 additions & 0 deletions samples/ex35.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE kbln (
id integer NOT NULL,
blank_series varchar(50) NOT NULL,
company_id varchar(8)) PARTITION BY
RANGE ( id);

CREATE TABLE kbln_p0 PARTITION OF kbln FOR
VALUES FROM ( MINVALUE) TO ( 500000) PARTITION BY HASH ( blank_series);

CREATE TABLE kbln_p0_1 PARTITION OF kbln_p0 FOR
VALUES WITH ( MODULUS 2, REMAINDER 0);

CREATE TABLE kbln_p0_2 PARTITION OF kbln_p0 FOR
VALUES WITH ( MODULUS 2, REMAINDER 1);

17 changes: 17 additions & 0 deletions samples/expected/ex35.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE kbln (
id integer NOT NULL,
blank_series varchar(50) NOT NULL,
company_id varchar(8)
)
PARTITION BY RANGE (id);

CREATE TABLE kbln_p0 OF kbln
FOR VALUES FROM (MINVALUE) TO (500000)
PARTITION BY HASH (blank_series);

CREATE TABLE kbln_p0_1 OF kbln_p0
FOR VALUES WITH (MODULUS 2, REMAINDER 0);

CREATE TABLE kbln_p0_2 OF kbln_p0
FOR VALUES WITH (MODULUS 2, REMAINDER 1);

0 comments on commit 9ba9d4d

Please sign in to comment.