| 1 | package Pg::SQL::PrettyPrinter::Node::DeleteStmt; | ||||||||||
| 2 | |||||||||||
| 3 | # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/ | ||||||||||
| 4 | 3 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use warnings qw( FATAL utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use open qw( :std :utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Unicode::Normalize qw( NFC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Unicode::Collate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Encode qw( decode ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | if ( grep /\P{ASCII}/ => @ARGV ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | # If there is __DATA__,then uncomment next line: | ||||||||||
| 19 | # binmode( DATA, ':encoding(UTF-8)' ); | ||||||||||
| 20 | # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/ | ||||||||||
| 21 | |||||||||||
| 22 | # Useful common code | ||||||||||
| 23 | 3 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 3 | ▶ | use Carp qw( carp croak confess cluck ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 3 | ▶ | use English qw( -no_match_vars ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 3 | ▶ | use Data::Dumper qw( Dumper ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | # give a full stack dump on any untrapped exceptions | ||||||||||
| 29 | 0 | ▶ | local $SIG{ __DIE__ } = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 30 | 0 | ▶ | confess "Uncaught exception: @_" unless $^S; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 3 | }; | |||||||||
| 32 | |||||||||||
| 33 | # now promote run-time warnings into stackdumped exceptions | ||||||||||
| 34 | # *unless* we're in an try block, in which | ||||||||||
| 35 | # case just generate a clucking stackdump instead | ||||||||||
| 36 | 0 | ▶ | local $SIG{ __WARN__ } = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 37 | 0 | ▶ | if ( $^S ) { cluck "Trapped warning: @_" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | else { confess "Deadly warning: @_" } | |||||||||
| 39 | 3 | }; | |||||||||
| 40 | |||||||||||
| 41 | # Useful common code | ||||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | use parent qw( Pg::SQL::PrettyPrinter::Node ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 7 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 7 | my $class = shift; | |||||||||
| 47 | 7 | my $self = $class->SUPER::new( @_ ); | |||||||||
| 48 | 7 | bless $self, $class; | |||||||||
| 49 | |||||||||||
| 50 | 7 | $self->objectify( | |||||||||
| 51 | qw( returningList usingClause whereClause ), | ||||||||||
| 52 | [ 'withClause', 'ctes' ], | ||||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | 7 | return $self; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 7 | ▶ | sub as_text { | ||||||||
|
Subroutine
as_text: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 59 | 7 | my $self = shift; | |||||||||
| 60 | 7 | my @elements = (); | |||||||||
| 61 | 7 | push @elements, 'DELETE FROM'; | |||||||||
| 62 | 7 | push @elements, $self->relname; | |||||||||
| 63 | 7 | ▶ | if ( exists $self->{ 'usingClause' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 3 | push @elements, 'USING'; | |||||||||
| 65 | 3 | push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'usingClause' } } ); | |||||||||
| 66 | } | ||||||||||
| 67 | 7 | ▶ | if ( exists $self->{ 'whereClause' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | push @elements, 'WHERE'; | |||||||||
| 69 | 4 | push @elements, $self->{ 'whereClause' }->as_text; | |||||||||
| 70 | } | ||||||||||
| 71 | 7 | ▶ | if ( exists $self->{ 'returningList' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 2 | push @elements, 'RETURNING'; | |||||||||
| 73 | 2 | push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } ); | |||||||||
| 74 | } | ||||||||||
| 75 | 7 | my $prefix = ''; | |||||||||
| 76 | 7 | ▶ | if ( exists $self->{ 'withClause' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 1 | $prefix = 'WITH '; | |||||||||
| 78 | 1 | ▶ | $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 1 | $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' '; | |||||||||
| 80 | } | ||||||||||
| 81 | 7 | return $prefix . join( ' ', @elements ); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 7 | ▶ | sub pretty_print { | ||||||||
|
Subroutine
pretty_print: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 85 | 7 | my $self = shift; | |||||||||
| 86 | 7 | my @lines = (); | |||||||||
| 87 | 7 | push @lines, 'DELETE FROM ' . $self->relname; | |||||||||
| 88 | |||||||||||
| 89 | 7 | ▶ | if ( exists $self->{ 'usingClause' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 3 | push @lines, 'USING'; | |||||||||
| 91 | 3 | push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'usingClause' } }; | |||||||||
| 92 | |||||||||||
| 93 | # Remove unnecessary trailing , in last element | ||||||||||
| 94 | 3 | $lines[ -1 ] =~ s/,\z//; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 7 | ▶ | if ( exists $self->{ 'whereClause' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 4 | push @lines, 'WHERE'; | |||||||||
| 99 | 4 | push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print ); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 7 | ▶ | if ( exists $self->{ 'returningList' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 2 | push @lines, 'RETURNING '; | |||||||||
| 104 | 2 | $lines[ -1 ] .= join( ', ', map { $_->pretty_print } @{ $self->{ 'returningList' } } ); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 7 | my $main_body = join( "\n", @lines ); | |||||||||
| 108 | 7 | ▶ | return $main_body unless exists $self->{ 'withClause' }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 1 | my @cte_def = (); | |||||||||
| 111 | |||||||||||
| 112 | 1 | push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } }; | |||||||||
| 113 | |||||||||||
| 114 | # Remove unnecessary trailing , in last element | ||||||||||
| 115 | 1 | $cte_def[ -1 ] =~ s/,\z//; | |||||||||
| 116 | 1 | ▶ | if ( $self->{ 'withClause' }->{ 'recursive' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ]; | |||||||||
| 118 | } | ||||||||||
| 119 | else { | ||||||||||
| 120 | 1 | $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ]; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 1 | @lines = (); | |||||||||
| 124 | 1 | push @lines, join( ' ', @cte_def ); | |||||||||
| 125 | 1 | push @lines, $main_body; | |||||||||
| 126 | 1 | return join( "\n", @lines ); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 14 | ▶ | sub relname { | ||||||||
|
Subroutine
relname: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 130 | 14 | my $self = shift; | |||||||||
| 131 | 14 | ▶ | if ( !$self->{ '_relname' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 7 | my $R = $self->{ 'relation' }; | |||||||||
| 133 | 7 | my @elements = map { $self->quote_ident( $R->{ $_ } ) } | |||||||||
| 134 | 7 | grep { exists $R->{ $_ } } qw{ catalogname schemaname relname }; | |||||||||
| 135 | 7 | $self->{ '_relname' } = join( '.', @elements ); | |||||||||
| 136 | 7 | ▶ | if ( $R->{ 'alias' }->{ 'aliasname' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 1 | $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } ); | |||||||||
| 138 | } | ||||||||||
| 139 | } | ||||||||||
| 140 | 14 | return $self->{ '_relname' }; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 3 | 1; | |||||||||
| 144 | |||||||||||
| 145 | # vim: set ft=perl: | ||||||||||