File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/DeleteStmt.pm
Criterion Covered Total %
statement 111 112 99.1
branch 22 24 91.6
condition n/a
subroutine 18 18 100.0
pod 4 4 100.0
total 155 158 98.1


line stmt bran cond sub pod time code
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 2     2   1567 use v5.26;
  2         43  
5 2     2   11 use strict;
  2         10  
  2         45  
6 2     2   14 use warnings;
  2         6  
  2         56  
7 2     2   9 use warnings qw( FATAL utf8 );
  2         4  
  2         63  
8 2     2   10 use utf8;
  2         4  
  2         10  
9 2     2   63 use open qw( :std :utf8 );
  2         4  
  2         9  
10 2     2   302 use Unicode::Normalize qw( NFC );
  2         4  
  2         127  
11 2     2   39 use Unicode::Collate;
  2         5  
  2         95  
12 2     2   13 use Encode qw( decode );
  2         4  
  2         157  
13              
14             if ( grep /\P{ASCII}/ => @ARGV ) {
15             @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 2     2   457 use autodie;
  2         5  
  2         12  
24 2     2   10797 use Carp qw( carp croak confess cluck );
  2         5  
  2         157  
25 2     2   15 use English qw( -no_match_vars );
  2         4  
  2         12  
26 2     2   776 use Data::Dumper qw( Dumper );
  2         6  
  2         446  
27              
28             # give a full stack dump on any untrapped exceptions
29             local $SIG{ __DIE__ } = sub {
30             confess "Uncaught exception: @_" unless $^S;
31             };
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             local $SIG{ __WARN__ } = sub {
37             if ( $^S ) { cluck "Trapped warning: @_" }
38             else { confess "Deadly warning: @_" }
39             };
40              
41             # Useful common code
42              
43 2     2   18 use parent qw( Pg::SQL::PrettyPrinter::Node );
  2         13  
  2         11  
44              
45             sub new {
46 7     7 1 237 my $class = shift;
47 7         36 my $self = $class->SUPER::new( @_ );
48 7         16 bless $self, $class;
49              
50 7         35 $self->objectify(
51             qw( returningList usingClause whereClause ),
52             [ 'withClause', 'ctes' ],
53             );
54              
55 7         24 return $self;
56             }
57              
58             sub as_text {
59 7     7 1 37 my $self = shift;
60 7         11 my @elements = ();
61 7         17 push @elements, 'DELETE FROM';
62 7         18 push @elements, $self->relname;
63 7 100       21 if ( exists $self->{ 'usingClause' } ) {
64 3         8 push @elements, 'USING';
65 3         11 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'usingClause' } } );
  3         12  
  3         9  
66             }
67 7 100       24 if ( exists $self->{ 'whereClause' } ) {
68 4         9 push @elements, 'WHERE';
69 4         39 push @elements, $self->{ 'whereClause' }->as_text;
70             }
71 7 100       20 if ( exists $self->{ 'returningList' } ) {
72 2         4 push @elements, 'RETURNING';
73 2         3 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'returningList' } } );
  4         11  
  2         7  
74             }
75 7         16 my $prefix = '';
76 7 100       18 if ( exists $self->{ 'withClause' } ) {
77 1         3 $prefix = 'WITH ';
78 1 50       5 $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
79 1         4 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
  1         5  
  1         3  
80             }
81 7         48 return $prefix . join( ' ', @elements );
82             }
83              
84             sub pretty_print {
85 7     7 1 6007 my $self = shift;
86 7         19 my @lines = ();
87 7         18 push @lines, 'DELETE FROM ' . $self->relname;
88              
89 7 100       26 if ( exists $self->{ 'usingClause' } ) {
90 3         8 push @lines, 'USING';
91 3         8 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'usingClause' } };
  3         18  
  3         8  
92              
93             # Remove unnecessary trailing , in last element
94 3         34 $lines[ -1 ] =~ s/,\z//;
95             }
96              
97 7 100       21 if ( exists $self->{ 'whereClause' } ) {
98 4         9 push @lines, 'WHERE';
99 4         19 push @lines, $self->increase_indent( $self->{ 'whereClause' }->pretty_print );
100             }
101              
102 7 100       23 if ( exists $self->{ 'returningList' } ) {
103 2         4 push @lines, 'RETURNING ';
104 2         5 $lines[ -1 ] .= join( ', ', map { $_->pretty_print } @{ $self->{ 'returningList' } } );
  4         11  
  2         7  
105             }
106              
107 7         24 my $main_body = join( "\n", @lines );
108 7 100       39 return $main_body unless exists $self->{ 'withClause' };
109              
110 1         4 my @cte_def = ();
111              
112 1         91 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
  1         5  
  1         9  
113              
114             # Remove unnecessary trailing , in last element
115 1         5 $cte_def[ -1 ] =~ s/,\z//;
116 1 50       6 if ( $self->{ 'withClause' }->{ 'recursive' } ) {
117 0         0 $cte_def[ 0 ] = 'WITH RECURSIVE ' . $cte_def[ 0 ];
118             }
119             else {
120 1         4 $cte_def[ 0 ] = 'WITH ' . $cte_def[ 0 ];
121             }
122              
123 1         3 @lines = ();
124 1         3 push @lines, join( ' ', @cte_def );
125 1         2 push @lines, $main_body;
126 1         5 return join( "\n", @lines );
127             }
128              
129             sub relname {
130 14     14 1 25 my $self = shift;
131 14 100       35 if ( !$self->{ '_relname' } ) {
132 7         13 my $R = $self->{ 'relation' };
133 7         34 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
134 7         13 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
  21         52  
135 7         33 $self->{ '_relname' } = join( '.', @elements );
136 7 100       28 if ( $R->{ 'alias' }->{ 'aliasname' } ) {
137 1         4 $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } );
138             }
139             }
140 14         46 return $self->{ '_relname' };
141             }
142              
143             1;
144              
145             # vim: set ft=perl: