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   1580 use v5.26;
  2         9  
5 2     2   11 use strict;
  2         5  
  2         41  
6 2     2   9 use warnings;
  2         4  
  2         60  
7 2     2   9 use warnings qw( FATAL utf8 );
  2         4  
  2         68  
8 2     2   11 use utf8;
  2         4  
  2         11  
9 2     2   52 use open qw( :std :utf8 );
  2         5  
  2         10  
10 2     2   289 use Unicode::Normalize qw( NFC );
  2         7  
  2         138  
11 2     2   43 use Unicode::Collate;
  2         4  
  2         80  
12 2     2   14 use Encode qw( decode );
  2         5  
  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   485 use autodie;
  2         4  
  2         15  
24 2     2   11174 use Carp qw( carp croak confess cluck );
  2         6  
  2         186  
25 2     2   15 use English qw( -no_match_vars );
  2         4  
  2         14  
26 2     2   795 use Data::Dumper qw( Dumper );
  2         6  
  2         486  
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   24 use parent qw( Pg::SQL::PrettyPrinter::Node );
  2         8  
  2         33  
44              
45             sub new {
46 7     7 1 242 my $class = shift;
47 7         35 my $self = $class->SUPER::new( @_ );
48 7         14 bless $self, $class;
49              
50 7         37 $self->objectify(
51             qw( returningList usingClause whereClause ),
52             [ 'withClause', 'ctes' ],
53             );
54              
55 7         22 return $self;
56             }
57              
58             sub as_text {
59 7     7 1 34 my $self = shift;
60 7         15 my @elements = ();
61 7         15 push @elements, 'DELETE FROM';
62 7         18 push @elements, $self->relname;
63 7 100       19 if ( exists $self->{ 'usingClause' } ) {
64 3         7 push @elements, 'USING';
65 3         8 push @elements, join( ', ', map { $_->as_text } @{ $self->{ 'usingClause' } } );
  3         10  
  3         8  
66             }
67 7 100       21 if ( exists $self->{ 'whereClause' } ) {
68 4         9 push @elements, 'WHERE';
69 4         15 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         12  
  2         7  
74             }
75 7         16 my $prefix = '';
76 7 100       16 if ( exists $self->{ 'withClause' } ) {
77 1         4 $prefix = 'WITH ';
78 1 50       5 $prefix .= 'RECURSIVE ' if $self->{ 'withClause' }->{ 'recursive' };
79 1         3 $prefix .= join( ', ', map { $_->as_text } @{ $self->{ 'withClause' }->{ 'ctes' } } ) . ' ';
  1         5  
  1         3  
80             }
81 7         42 return $prefix . join( ' ', @elements );
82             }
83              
84             sub pretty_print {
85 7     7 1 5827 my $self = shift;
86 7         15 my @lines = ();
87 7         19 push @lines, 'DELETE FROM ' . $self->relname;
88              
89 7 100       26 if ( exists $self->{ 'usingClause' } ) {
90 3         8 push @lines, 'USING';
91 3         5 push @lines, map { $self->increase_indent( $_->pretty_print ) . ',' } @{ $self->{ 'usingClause' } };
  3         18  
  3         9  
92              
93             # Remove unnecessary trailing , in last element
94 3         15 $lines[ -1 ] =~ s/,\z//;
95             }
96              
97 7 100       19 if ( exists $self->{ 'whereClause' } ) {
98 4         9 push @lines, 'WHERE';
99 4         16 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         12  
  2         6  
105             }
106              
107 7         21 my $main_body = join( "\n", @lines );
108 7 100       34 return $main_body unless exists $self->{ 'withClause' };
109              
110 1         4 my @cte_def = ();
111              
112 1         97 push @cte_def, map { $_->pretty_print . ',' } @{ $self->{ 'withClause' }->{ 'ctes' } };
  1         6  
  1         10  
113              
114             # Remove unnecessary trailing , in last element
115 1         6 $cte_def[ -1 ] =~ s/,\z//;
116 1 50       5 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         4 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       40 if ( !$self->{ '_relname' } ) {
132 7         13 my $R = $self->{ 'relation' };
133 7         33 my @elements = map { $self->quote_ident( $R->{ $_ } ) }
134 7         15 grep { exists $R->{ $_ } } qw{ catalogname schemaname relname };
  21         50  
135 7         25 $self->{ '_relname' } = join( '.', @elements );
136 7 100       23 if ( $R->{ 'alias' }->{ 'aliasname' } ) {
137 1         5 $self->{ '_relname' } .= ' AS ' . $self->quote_ident( $R->{ 'alias' }->{ 'aliasname' } );
138             }
139             }
140 14         42 return $self->{ '_relname' };
141             }
142              
143             1;
144              
145             # vim: set ft=perl: